in

Dot Net Mafia

Group site for developer blogs dealing with (usually) .NET, SharePoint 2013, SharePoint 2010, Office 365, SharePoint Online, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for SharePoint 2013, Office 365 / SharePoint Online and Visual Studio 2013.

LINQ to XML and Deleting Web Parts from the Web Part Gallery

Yesterday, I posted on how to write a feature receiver to delete files it creates on feature deactivation.  This concept can easily apply to the deletion of web parts from the web part gallery as well (although the changes I describe here could work for anything deployed at the site collection level).  To make this happen, I created a new feature receiver with a FeatureDeactivating method which handles an SPSite object as opposed to an SPWeb object.  It looks like this now.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)

{

    using (SPSite currentSiteCollection = (SPSite)properties.Feature.Parent)

    {

        string elementsPath = string.Format(@"{0}\FEATURES\{1}\Elements.xml",

                SPUtility.GetGenericSetupPath("Template"), properties.Definition.DisplayName);

 

        DeleteFeatureFiles(currentSiteCollection, elementsPath);

    }

}

For the purpose of my example, I have an elements.xml file that adds a web part like the following.

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Module Name="WebPartPopulation" List="113" Url="_catalogs/wp" RootWebOnly="TRUE">

    <File Url="TestWebPart.webpart" Type="GhostableInLibrary" />

  </Module>

</Elements>

I changed all other methods to use an SPSite object as opposed to SPWeb and then made one minor change to the DeleteModuleFiles method to use the URL instead of Name attribute.

private void DeleteModuleFiles(string moduleUrl, IEnumerable<XElement> fileList, SPSite currentSiteCollection)

{

    // delete each file in the module

    foreach (var fileElement in fileList)

    {

        // use the name attribute if specified otherwise use Url attribute (since it is required)

        string filename = (fileElement.Attributes("Name").Any()) ? fileElement.Attribute("Name").Value

            : fileElement.Attribute("Url").Value;

 

        // pass the moduleUrl if it has a value

        if (!string.IsNullOrEmpty(moduleUrl))

            currentSiteCollection.RootWeb.GetFile(string.Format("{0}/{1}", moduleUrl, filename)).Delete();

        else

            currentSiteCollection.RootWeb.Files.Delete(filename);

    }

}

With these changes, you have a feature receiver that should work to delete any .webpart file (or .dwp)  you add to the gallery.

UPDATE: I updated the DeleteModuleFiles method to look for the file in the Name attribute first and then the Url attribute for the name of the file.

Comments

No Comments

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2012 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems