Saturday, October 11, 2014

SharePoint 2013 - Programmatically adding a custom webpart to a webpart page

In this post we will see how to add a custom webpart on a SharePoint page through server side code. We will use the SPLimitedWebPartManager Class class to manage the webpart.

SPLimitedWebPartManager Class
The SPLimitedWebPartManager class provides a limited set of Web Part operations that can be performed in object model scenarios when there is no HttpContext and no instantiated Page object. Using this class we create an instance of the web part manager through which we specify the webpart to be added and page and zone on which it should be added.

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 and create a feature receiver for MyVisualWPFeature.

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

4) 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 web = site.RootWeb)
            {
                web.AllowUnsafeUpdates = true;
                Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager webPartsManager = web.GetLimitedWebPartManager("SitePages/Demo.aspx", PersonalizationScope.Shared);

                //Create an instance of Custom Webpart and add in a Webpart zone
                MyVisualWP.MyVisualWP wp = new MyVisualWP.MyVisualWP();
                wp.Title = "WebPart Added Through Code";  
                webPartsManager.AddWebPart(wp, "Top", 0);
                webPartsManager.SaveChanges(wp);
            }
        }

5) 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 =
web.GetLimitedWebPartManager("SitePages/Demo.aspx",PersonalizationScope.Shared);

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

//Delete the Webpart
foreach (WebPart webPart in webPartList.ToList())
{
webPartsManager.DeleteWebPart(webPart);
}
web.Update();
}
}

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

7) You will see that webpart has been added to the page automatically

8) 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

Note: Only a member of this blog may post a comment.