If your feature is going to install assets to your site, i.e. web parts (.webpart or .dwp files), master pages, page layouts, images, javascript files, custom stylesheet, even possibly custom content type, list definitions and list instances, it is normally considered best practices to remove what you added when the feature is deactivated. I say normally because sometime you may not be able to remove custom Content Types or List Instances if they are being used. Or possibly your assets may have been modified, so removing them even if the feature is deactived could be a problem. Features in of themselves do not offer a pre-built way to remove what was added, nor would uninstalling and deleting the solution remove assets a feature installed, but features do offer the function:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
By overriding this function we can add our own code that can use a similar method as we used when checking in files during the feature activation, that being we can take the Element Definition Collection from the feature itself, loop through it and look for what we know we added, possibly modules (don’t forget that .webpart files are added to _catalogs/wp via a module), or a List Instance, etc. Once we grab a list of assets, lists etc that were added we can remove them.
Let us look at feature deactivation code that could be used in a farm based solution’s feature event receiver.
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
using (SPSite spSite = properties.Feature.Parent as SPSite)
{
using (SPWeb web = spSite.RootWeb)
{
List<String> lModuleFiles = new List<String>();
List<String> lListInstances = new List<String>();
SPElementDefinitionCollection spFeatureElements = properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture);
foreach (SPElementDefinition spElementDefinition in spFeatureElements)
{
if (spElementDefinition.ElementType == "Module")
{
XmlElement xmlElementNode = (XmlElement)spElementDefinition.XmlDefinition;
String sModName = xmlElementNode.GetAttribute("Name");
String sModUrl = SPUtility.ConcatUrls(spSite.Url, xmlElementNode.GetAttribute("Url"));
foreach (XmlElement xmlChildElementNode in xmlElementNode.ChildNodes)
{
if (xmlChildElementNode.Name == "File")
{
String sFile = SPUtility.ConcatUrls(sModUrl, xmlChildElementNode.GetAttribute("Url"));
lModuleFiles.Add(sFile);
}
}
}
else if (spElementDefinition.ElementType == "ListInstance")
{
XmlElement xmlElementNode = (XmlElement)spElementDefinition.XmlDefinition;
String sModTitle = xmlElementNode.GetAttribute("Title");
String sModUrl = xmlElementNode.GetAttribute("Url");
lListInstances.Add(sModTitle);
}
}
#region Delete list instances added by this feature
try
{
if (lListInstances.Count > 0)
{
foreach (String listName in lListInstances)
{
try
{
SPList workList = spSite.RootWeb.Lists.TryGetList(listName);
if (workList != null)
{
Guid gd = workList.ID;
spSite.RootWeb.Lists.Delete(gd);
}
}
catch { }
}
}
}
catch { }
try
{
if (lModuleFiles.Count > 0)
{
foreach (String fileUrl in lModuleFiles)
{
try
{
SPFile file = web.GetFile(fileUrl);
if (file != null) {
SPFolder folder = file.ParentFolder;
file.Delete();
web.Update();
//attempt to delete the folder if it is now empty
if (folder.Files.Count < 1)
folder.Delete();
}
}
catch { }
}
}
}
catch { }
}
}
}
Reference Link :http://blog.pixelmill.com/1123/housekeeping-deactivation-removing-what-you-added/
Please follow and like us:
Technical Solutions Professional | Modern Workplace Architect Lead | Microsoft 365 | SharePoint | Power Platform | MS Teams | React
Leave a Reply