Dismiss Notice

Register now to be one of the first members of this SharePoint Community! Click here it just takes seconds!

Dismiss Notice
Welcome Guest from Country Flag

How to Add WebPart to a Page in SharePoint Office 365 Programmatically using C# Client Side...

Discussion in 'SharePoint Blogs' started by sathishnadarajan,, Apr 4, 2017.

Thread Status:
Not open for further replies.
  1. sathishnadarajan,

    sathishnadarajan, Guest

    Blog Posts:
    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="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/WebPart/v2">
    <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="http://schemas.microsoft.com/WebPart/v2/ContentEditor"></ContentLink>>
    <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
    <div id='MyDiv'> Test WebPart
    <script src='https://sppalsmvp.sharepoint.com/sites/AssetSiteCollection/SiteAssets/JS/MyJS.js'></script>

    <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />

    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://******.sharepoint.com/sites/communitysite";
    string userName = "[email protected]*********.onmicrosoft.com";
    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.

    Continue reading...
Thread Status:
Not open for further replies.

Share This Page

LiveZilla Live Chat Software