Home > ASP.NET, C#, MOSS 2007, SQL > Programmatically adding a Web Part to the Web Part Gallery.

Programmatically adding a Web Part to the Web Part Gallery.

I came across a problem whereby I needed to add a Web Part to the Web Part Gallery. The Web Part had been deployed successfully but as of yet it did not appear in the Web Part Gallery SPList. This needed to be done programmatically by using the SharePoint object model.

How do we programmatically add a Web Part (that has been deployed) to the Web Part Gallery. Using the SharePoint UI this is done via the NewDwp.aspx page.

The problem is that the list you see on the NewDwp.aspx page is not an SPList! Which makes life a bit tough! The other problem is that the .dwp files displayed in there do not exist until you select them and then click “Populate” by which time they are already in the Web Part Gallery.

I believe the NewDwp.aspx page builds the list of Web Parts, available to be added to the Gallery, from the web.config safe control enteries. Once you click “Populate” the page dynamically builds the .dwp file and adds it to the Gallery.

To add the Web Part to the Gallery programmatically you have to do the same i.e.

  1. Create the .dwp xml file dynamically
  2. Add it to the Web Part Gallery SPList

Below is the code to achieve this:

private static void AddWebPartToGallery()
    using (SPSite site = new SPSite("http://yoursite.com"))
        using (SPWeb web = site.OpenWeb())
FileInfo fInfo = new FileInfo("myFile.dwp");
            FileStream fStream = fInfo.Open(FileMode.Open, FileAccess.Read);    
            web.AllowUnsafeUpdates =
            site.AllowUnsafeUpdates =
            SPList list = web.Lists["Web Part Gallery"];
            SPFolder root = list.RootFolder;
            SPFile spFile = root.Files.Add("ContentEditor.dwp", s);

The CreateDwpFile() method dynamically creates the .dwp file. Then open the Web Part Gallery List and the newly created .dwp file and add it to the root folder of the list.

  1. Biju
    September 30, 2009 at 5:02 am


    Can you please tell how to Programmatically populate the WebParts (.webpart, not .wsp) into the gallery, which has been already deployed, and deleted from the “Webpart Gallery” list ?

    With regards

  2. Bhawant
    October 15, 2009 at 9:17 am

    But how to make CreateDwpFile() method

  3. drdentz
    March 3, 2010 at 4:28 pm

    Hi, thanks for the post.
    Can you explain what the CreatedwpFile() does?
    I see that you call it in you main AddWebPartToGallery() method.


  4. jasear
    March 30, 2010 at 9:43 pm

    Sorry for leaving that bit out unfortunately I dont have the code for that method anymore :(. What that method does is to create a file called “ContentEditor.dwp” in this scenario (you can call it whatever you want) and it just creates the xml for it in a typical format (just export a web part and look at the xml it generates).

    It could also be a .webPart file. What my method was doing in this scenario was to build this file on the fly. Ofcourse you dont have to do that you could always have this file already created and you simply add it using the method i described above.

  1. November 20, 2012 at 10:30 am

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: