Deleting the Contents on deactivating the feature

17 October 2014

12:30

Delete the .webpart file on feature deactivation

The web part files will be stored in List template type SPListTemplateType.WebPartCatalog.

We can delete it by taking the web part name from list.

You can use the below code in feature deactivation to delete the .webpart file

SPSite site = properties.Feature.Parent as SPSite;

SPWeb web = site.RootWeb;

int itemToDelete = 0;

SPList list = site.GetCatalog(SPListTemplateType.WebPartCatalog);

foreach (SPListItem item in list.Items)

{

if (item[“Web Part”].ToString().ToLower() == (“Sample.webpart”).ToLower())

{

itemToDelete = item.ID;

if (itemToDelete > 0)

{

SPListItem listitem = list.GetItemById(itemToDelete);

listitem.Delete();

}

list.Update();

}

}

Deleting the files in Module on feature deactivation

private static void DeleteFilesinStyleLibrary(SPWeb curWeb)

{

List<string> lStyleLibrarySubDirs = new List<string>();

//add all style library sub directories.

lStyleLibrarySubDirs.Add(“Images”);//Images folder in style library or the folder in module

if (lStyleLibrarySubDirs.Count > 0)

{

//go through each list

for (int i = 0; i < curWeb.Lists.Count; i++)

{

if (curWeb.Lists[i].RootFolder.Name.Equals(“Style Library”))

{

SPDocumentLibrary styleLib = (SPDocumentLibrary)curWeb.Lists[i];

foreach (String sSubDir in lStyleLibrarySubDirs)

{

foreach (SPListItem subFolder in styleLib.Folders)

{

//if themable dir

if (subFolder.Name.Equals(sSubDir))

{

if (subFolder.Folder.ParentFolder.Name.Equals(“Style Library”))

{

SPFileCollection collFiles = subFolder.Folder.Files;

for (int intIndex = collFiles.Count – 1; intIndex > -1; intIndex–)

{

string strDelUrl = collFiles[intIndex].Url;

collFiles.Delete(strDelUrl);

}

}

}

}

}

}

}

}

}

Reference:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfilecollection.delete

http://blog.pixelmill.com/1123/housekeeping-deactivation-removing-what-you-added/

Delete the MasterPage file on feature deactivation

private static void DeleteMasterPageCss(SPWeb curWeb)

{

List<string> lFilestoDelete = new List<string>();

lFilestoDelete.Add(curWeb.Url + “/_catalogs/masterpage/Sample.master”);

lFilestoDelete.Add(curWeb.Url + “/Style Library/Sample.css”);

foreach (String fileUrl in lFilestoDelete)

{

SPFile file = curWeb.GetFile(fileUrl);

if (file != null)

{

file.Delete();

curWeb.Update();

}

}

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s