Apr 9, 2012

Adding Webpart to the Page Programatically in SharePoint

Hi All,

Here I am explaining you how to add a web part to the page programatically.  For this demo I have created a web part page called Demo.aspx  and I am adding a content editor web part to this page through programatically. 

For this first we need to get the reference of the SPLimitedWebPartManager  of the page where we are going to add the webpart.

Here is the code
protected void Addwebpart_Click(object sender, EventArgs e)
        {           
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;
                    //getting reference of SPLimitedWebpartManager of the page
                    SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);                   
                    //Creation of ContentEdiotr Webpart
                    ContentEditorWebPart myCEWP = new ContentEditorWebPart();
                    myCEWP.ID = "myCEWP";
                    myCEWP.Title = "Demo Content Editor";
                    //Adding Webpart to the page
                    wpManager.AddWebPart(myCEWP, "Main", 1);
                    wpManager.SaveChanges(myCEWP);
                }
            }

        }

If you want to add the Shared Documents (ListView webpart) to the page,  Here is the way..

                    web.AllowUnsafeUpdates = true;
                    SPList lstCDD = web.Lists.TryGetList("Shared Documents");
                    if (lstCDD != null)
                    {
                        SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager("http://sp2010:9999/SitePages/Demo.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);

                        XsltListViewWebPart webpartCDD = new XsltListViewWebPart();
                        webpartCDD.ListName = lstCDD.ID.ToString("B").ToUpper();
                        webpartCDD.ViewGuid = lstCDD.DefaultView.ID.ToString("B").ToUpper();
                        wpManager.AddWebPart(webpartCDD, "Main", 2);
                        wpManager.SaveChanges(webpartCDD);

                    }

Here the AddWebpart method expects three parameters, webpart, zoneid(string-Id of the webpartzone where the webpart is being added to like Main, Left etc), zone index(Int-Index of the zone like 1,2,3..)

Cheers,

5 comments:

  1. Thank you so much!

    ReplyDelete
  2. YetAnother SPUserJune 11, 2013 at 8:08 PM

    I am using the above code to add 10 list viewer web parts to a SharePoint web part page. The list viewer web parts are getting added, but the web part with zoneIndex=4 is shown at the bottom of the list, instead of at the 4th position. Would you be able to tell what may be causing the problem?

    ReplyDelete
    Replies
    1. Hi YetAnother SPUser,

      Is there any height specified for the container of this zone in the master page.? If yes it might restrict to add in to that.

      Alternative is try to add the webpart manually , if you can add, you need to cross check the code.

      Delete
  3. IS there any way using Client side code? Everywhere on google its Server side.

    ReplyDelete
    Replies
    1. I dont think so csom exposed these classes..Here is the request from the users for the same..
      https://officespdev.uservoice.com/forums/224641-general/suggestions/6413925-expose-microsoft-sharepoint-webpartpages-splimited

      But there may be any other alternatives like importing and exporting webpart as xml..

      Delete