Saturday, October 18, 2014

SharePoint 2013 - Programmatically adding WebPart to a page from Web Part Gallery

In this post, we will see how to programmatically add a webpart from webpart gallery and add it on to a SharePoint page.

We will access the webpart gallery using the SPWeb.GetCatalog() method and create a new webpart instance by reading the webpart file as XML. We will then use the SPLimitedWebPartManager Class class to manage the webpart.

Following are the Steps for adding the webpart programmatically
1) Create a new webpart page named Demo.aspx in the site as mentioned in this post.

2) Create a custom webpart named MyVisualWP as mentioned in this post. Go to Site Settings->Web Part Gallery and locate the webpart file.

3) Now, create a new feature receiver for MyVisualWPFeature which will include the code for adding and removing the webpart from the page.

4) Following additional namespaces will be required
using System.Linq;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using System.Xml;
using System.IO;

5) Write the code for  adding the MyVisualWP webpart to the Demo.aspx page in the FeatureActivated() Event as follows.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb oWeb = site.RootWeb)
//Get the Page Url where Webpart needs to be added.
string strTargetPage = oWeb.Url + "/SitePages/Demo.aspx";
string strWebPartName = "MyVisualWebpart_MyVisualWP.webpart";
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager Mgr = oWeb.GetLimitedWebPartManager(strTargetPage, PersonalizationScope.Shared);

//Get WebPart from the WebPart Gallery
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>", strWebPartName);
SPList WebPartGallery = oWeb.GetCatalog(SPListTemplateType.WebPartCatalog);
SPListItemCollection webparts = WebPartGallery.GetItems(query);

//Get the XML from the WebPart File
XmlReader xmlReader = null;
if (webparts != null || webparts.Count != 0)
    Stream xmlStream = webparts[0].File.OpenBinaryStream();
    StreamReader sReader = new StreamReader(xmlStream);
    StringReader strReader = new StringReader(sReader.ReadToEnd());
    xmlReader = XmlReader.Create(strReader);

//Add the WebPart to the Page
if (xmlReader != null)
    string errMsg = "";
    System.Web.UI.WebControls.WebParts.WebPart WP =
Mgr.ImportWebPart(xmlReader, out errMsg);
    WP.Title = "WebPart Added From Gallery";
    Mgr.AddWebPart(WP, "Left", 1);

6) Write the following code for  deleting the MyVisualWP webpart from the Demo.aspx page in the FeatureDeactivating() Event as follows.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
SPSite site = properties.Feature.Parent as SPSite;
using (SPWeb web = site.RootWeb)
web.AllowUnsafeUpdates = true;
Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager webPartsManager =

//Get the Webpart by title
IEnumerable<WebPart> webPartList =
from System.Web.UI.WebControls.WebParts.WebPart webPart
in webPartsManager.WebParts
where webPart.Title == "WebPart Added From Gallery"
select webPart;

//Delete the Webpart
foreach (WebPart webPart in webPartList.ToList())

7) Deploy the solution and Activate the My Visual WP Feature.

8) You will see that the WebPart has been added to the page automatically

9) Now deactivate the My Visual WP Feature.

9) You will notice that the WebPart has been removed from the Demo.aspx page.

Hope you enjoyed this post!

No comments:

Post a Comment