Add a new WebPart to a SharePoint Site using Client Object Model C#

We can also add a new WebPart to a SharePoint page using CSOM. Before I proceed further, let me first point out the three imp parameters that are needed to perform this action. They are:-

  • ZoneID
  • ZoneIndex
  • WebPartXml

I am going to confine this blog to the items of the two Lists only, Pages(created by default for publishing site) and SitePages(created by default for TeamSite). Both these List, have web page as listItems. However, there’s a contrasting different,
SitePages have only 1 container[Zone] for everything, RichContent (ZoneID=wpz)
whereas,
Pages have various containers[Zones], Header, Footer, Left, Right, RichContent (ZoneID=wpz), etc.

There’s one thing to note here that, the value of ZoneID, for RichContent, is always wpz.

Below is the code snippet for adding a WebPart to a SitePage/Pages

ClientContext ctx = new ClientContext(weburl);
ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);

SP.List list = ctx.Web.Lists.GetByTitle("Site Pages");
ctx.Load(list);
CamlQuery cQuery = new CamlQuery();
ListItemCollection ltItemCollection = list.GetItems(cQuery);

ctx.Load(ltItemCollection);
ctx.ExecuteQuery();

ListItem ltItemHome = ltItemCollection.FirstOrDefault(p => p.DisplayName == "Home");
SP.File file = ltItemHome.File;

ctx.Load(ltItemHome);
ctx.Load(file);
ctx.ExecuteQuery();

LimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
WebPartDefinitionCollection webPartDefCollection = limitedWebPartManager.WebParts;

WebPartDefinition webPartDef = limitedWebPartManager.ImportWebPart(webPartXml);
WebPartDefinition newWebPartDef = limitedWebPartManager.AddWebPart(webPartDef.WebPart, stringZoneId, intZoneIndex);

//ctx is the ClientContext
//Get the Guid of the newly added WebPart 
ctx.Load(newWebPartDef, w => w.Id);
ctx.ExecuteQuery();

So, if your stringZoneId is anything but ‘wpz’, your newly created WebPart will appear on the page at its appropriate zone. Now, if you’re attempting to add this WebPart to the zone, ‘wpz‘[RichContent] then, there’s one more step to go. For, RichContent, you have to specify the exact position of its appearance after adding it to the page. Since, a RichContent can contain text as well as WebParts, you have the liberty to position it accordingly inside the RichContent. For this ex, I will add it at the end of the RichContent, so that, if the RichContent contains some values(text, image, or WebParts) then, my newly added WebPart, will appear after them.

if (stringZoneId == "wpz")
{
    if (ltItemHome.FieldValuesAsHtml.FieldValues.ContainsKey("WikiField"))
    {
        //SitePage item
        ltItemHome["WikiField"] = String.Concat(ltItemHome["WikiField"], "
", GetEmbeddedWPString(newWebPartDef.Id), "
");
    }
    else if (ltItemHome.FieldValuesAsHtml.FieldValues.ContainsKey("PublishingPageContent"))
    {
        //Pages item
        ltItemHome["PublishingPageContent"] = String.Concat(ltItemHome["PublishingPageContent"], "
", GetEmbeddedWPString(newWebPartDef.Id), "
");
    }
    ltItemHome.Update();
    ctx.ExecuteQuery();
}

Here, I am appending, a constant string (GetEmbeddedWPString(Guid wpGuid)) with the new WebPartId to the field, WikiField, for SitePages and, PublishingPageContent, for the List Pages. The constant string format is

private string GetEmbeddedWPString(Guid wpGuid)
{
    // set the web part's ID as part of the ID-s of the div elements
    string wpForm = @"
<div class=""ms-rtestate-read ms-rte-wpbox"">

<div class=""ms-rtestate-notify ms-rtegenerate-notify ms-rtestate-read {0}"" id=""div_{0}"">
                            </div>


<div id=""vid_{0}"" style=""display:none"">
                            </div>

                       </div>

";
    
    return string.Format(wpForm, wpGuid);
}

Just for reference, I have posted the schemaXml of a Rss WebPart here, https://realmpksharepoint.wordpress.com/2014/04/10/sample-rss-webpart-schemaxml/

One thought on “Add a new WebPart to a SharePoint Site using Client Object Model C#

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 )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.