How to Add WebPart to a Page in SharePoint Office 365 Programmatically using C# Client Side Object Model (CSOM)

Sathish Nadarajan
Solution Architect
April 4, 2017
Rate this article
[Total: 0    Average: 0/5]

Sometime back in an article, we saw how to create pages programmatically in SharePoint Office 365. Now, as an extend, after creating the Page, we need to Add WebParts to the pages. That can be either the OOB webpart, or the custom webpart. Let us see, how to create them step by step. In the below sample, let us see how to Add a Content Editor Webpart to the Page Programmatically.

1. In our Solution Add a Folder.


2. Add the below XML under the Folder WebParts.

 <?xml version="1.0" encoding="utf-8"?>
 <WebPart xmlns:xsd="" xmlns:xsi="" xmlns="">
   <Title>CSOM Content Editor</Title>
   <Description>Allows authors to enter rich text content.</Description>
   <Height />
   <Width />
   <DetailLink />
   <HelpLink />
   <PartImageSmall />
   <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
   <IsIncludedFilter />
   <Assembly>Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
   <ContentLink xmlns=""></ContentLink>>
   <Content xmlns="">
    <div id='MyDiv'> Test WebPart
   <script src=''></script>
   <PartStorage xmlns="" />

3. The solution will looks like below.


4. Select the XML and Click on Properties.


5. Make the Build Action as Embedded Resource.


6. Now, let us come back to our Method.

 namespace Console.Office365
     using Microsoft.SharePoint.Client;
     using Microsoft.SharePoint.Client.Taxonomy;
     using Newtonsoft.Json.Linq;
     using OfficeDevPnP.Core.Entities;
     using System;
     using System.Collections.Generic;
     using System.IO;
     using System.Linq;
     using System.Reflection;
     using System.Threading.Tasks;
     class Program
         static void Main(string[] args)
         public static void AddWebParts()
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
             string siteUrl = "https://******";
             string userName = "Sathish@*********";
             string password = "*************";
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 Web web = clientContext.Web;
                 WebPartEntity myWebPart = new WebPartEntity();
                 myWebPart = new WebPartEntity { WebPartXml = MyWebPart_O365_WebPartSchemaXML };
                 myWebPart.WebPartTitle = "My WebPart";
                 myWebPart.WebPartIndex = 2;
                 clientContext.Web.AddWebPartToWikiPage("/sites/CommunitySite/SitePages/WikiPage1.aspx", myWebPart, 1, 1, false);
         private static string myWebPart_O365_WebPartSchemaXML = string.Empty;
         public static string MyWebPart_O365_WebPartSchemaXML
                 if (string.IsNullOrEmpty(myWebPart_O365_WebPartSchemaXML))
                     using (var stream = Assembly.GetAssembly(typeof(Program)).GetManifestResourceStream("Console.Office365.WebPart.MyWebPart.WebPartSchema.xml"))
                         using (var reader = new StreamReader(stream))
                             myWebPart_O365_WebPartSchemaXML = reader.ReadToEnd();
                 return myWebPart_O365_WebPartSchemaXML;

7. Now, after executing this code, let us come back to the screen and refresh.

8. We should be able to see the Content Editor Webpart on the page.


Happy Coding,

Sathish Nadarajan.

Author Info

Sathish Nadarajan
Solution Architect
Rate this article
[Total: 0    Average: 0/5]
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 13+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business more

Leave a comment