Udostępnij za pośrednictwem


Creating web part page using client object model (CSOM) in SharePoint 2013 or 2010

It is common scenario to create web part page and there are many ways to do that. In this post, I will give steps to create the web part page programmatically using client object model.

First we need to ensure the target destination for the page we are creating. In this example I will create the web part page in "SitePages". We need to get the object for accessing "Site Pages". We can use code similar to code snippet mentioned below.

 var sitePageLib = web.Lists.GetByTitle("Site Pages");
 clientContext.Load(sitePageLib);
 clientContext.ExecuteQuery();

 

Here I am assuming, we already have instantiated client context , web and other required object. In case, you need more detail on how to do that, I would recommend to read this on msdn.

To create the web part page we need to create object of FileCreationInformation with required details.

Mainly we need to enter the details of Url and Content. Use "Url" to gets or sets a value that specifies the URL of the file to be added and Content for specifying the content of the file to be added.

  FileCreationInformation objFileInfo = new FileCreationInformation();
 objFileInfo.Url = "MyPage.aspx";
 
 string content = @"<%@ Page language=""C#"" MasterPageFile=""~masterurl/default.master"" Inherits=""Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=16.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c"" meta:webpartpageexpansion=""full"" meta:progid=""SharePoint.WebPartPage.Document"" %>
 <%@ Register Tagprefix=""SharePoint"" Namespace=""Microsoft.SharePoint.WebControls"" Assembly=""Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" %> 
 <%@ Register Tagprefix=""Utilities"" Namespace=""Microsoft.SharePoint.Utilities"" Assembly=""Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" %> 
 <%@ Import Namespace=""Microsoft.SharePoint"" %> 
 <%@ Assembly Name=""Microsoft.Web.CommandUI, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" %> 
 <%@ Register Tagprefix=""WebPartPages"" Namespace=""Microsoft.SharePoint.WebPartPages"" Assembly=""Microsoft.SharePoint, Version=16.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"" %>
 <asp:Content ContentPlaceHolderId=""PlaceHolderPageTitle"" runat=""server"">
 <SharePoint:ListItemProperty Property=""BaseName"" maxlength=""40"" runat=""server""/>
 </asp:Content>
 <asp:Content ContentPlaceHolderId=""PlaceHolderAdditionalPageHead"" runat=""server"">
 <meta name=""GENERATOR"" content=""Microsoft SharePoint"" />
 <meta name=""ProgId"" content=""SharePoint.WebPartPage.Document"" />
 <meta http-equiv=""Content-Type"" content=""text/html; charset=utf-8"" />
 <meta name=""CollaborationServer"" content=""SharePoint Team Web Site"" />
 <SharePoint:ScriptBlock runat=""server"">
 var navBarHelpOverrideKey = ""WSSEndUser""; </SharePoint:ScriptBlock>
 <SharePoint:StyleBlock runat=""server"">
 body #s4-leftpanel {
 display:none;
 }
 .s4-ca {
 margin-left:0px;
 }</SharePoint:StyleBlock>
 </asp:Content>
 <asp:Content ContentPlaceHolderId=""PlaceHolderSearchArea"" runat=""server"">
 <SharePoint:DelegateControl runat=""server""
 ControlId=""SmallSearchInputBox""/>
 </asp:Content>
 <asp:Content ContentPlaceHolderId=""PlaceHolderPageDescription"" runat=""server"">
 <SharePoint:ProjectProperty Property=""Description"" runat=""server""/>
 </asp:Content>
 <asp:Content ContentPlaceHolderId=""PlaceHolderMain"" runat=""server"">
 <div class=""ms-hide"">
 <WebPartPages:WebPartZone runat=""server"" title=""loc:TitleBar"" id=""TitleBar"" AllowLayoutChange=""false"" AllowPersonalization=""false"" Style=""display:none;""><ZoneTemplate>
 <WebPartPages:TitleBarWebPart runat=""server"" HeaderTitle=""MyPage"" Title=""Web Part Page Title Bar"" FrameType=""None"" SuppressWebPartChrome=""False"" Description="""" 
 IsIncluded=""True"" ZoneID=""TitleBar"" PartOrder=""2"" FrameState=""Normal"" AllowRemove=""False"" AllowZoneChange=""True"" AllowMinimize=""False"" AllowConnect=""True"" 
 AllowEdit=""True"" AllowHide=""True"" IsVisible=""True"" DetailLink="""" HelpLink="""" HelpMode=""Modeless"" Dir=""Default"" PartImageSmall="""" 
 MissingAssembly=""Cannot import this Web Part."" PartImageLarge="""" IsIncludedFilter="""" ExportControlledProperties=""True"" 
 ConnectionID=""00000000-0000-0000-0000-000000000000"" ID=""g_0793ac9d_8f08_49c3_8829_46f162a4e619"" AllowClose=""False"" ChromeType=""None"" 
 ExportMode=""All"" __MarkupType=""vsattributemarkup"" __WebPartId=""{0793EC99-8F07-49C9-8823-46F162A4E614}"" WebPart=""true"" Height="""" Width=""""></WebPartPages:TitleBarWebPart>
 
 </ZoneTemplate></WebPartPages:WebPartZone>
 </div>
 <table class=""ms-core-tableNoSpace ms-webpartPage-root"" width=""100%"">
 <tr>
 <td id=""_invisibleIfEmpty"" name=""_invisibleIfEmpty"" valign=""top"" width=""100%""> 
 <WebPartPages:WebPartZone runat=""server"" Title=""loc:FullPage"" ID=""FullPage"" FrameType=""TitleBarOnly""><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
 </tr>
 <SharePoint:ScriptBlock runat=""server"">if(typeof(MSOLayout_MakeInvisibleIfEmpty) == ""function"") {MSOLayout_MakeInvisibleIfEmpty();}</SharePoint:ScriptBlock>
 </table>
 </asp:Content>
 ";
 
 objFileInfo.Content = System.Text.Encoding.ASCII.GetBytes(content); 
 File file = sitePageLib.RootFolder.Files.Add(objFileInfo); 
 clientContext.ExecuteQuery();

 

 

In the above code snippet, I am creating the FileCreationInformation with my page name as value in Url and Content being content of page. you can use SharePoint designer to get the content of page.

Next step is to add the file in "Site Pages" and execute the query.

 

  
  

Comments

  • Anonymous
    November 17, 2013
    Hi Kaushalendra, This is an excellent article. I was curious to see if we can add webparts as well through CSOM  ?? Thanks

  • Anonymous
    November 20, 2013
    Yes, we can add the web parts also using CSOM. For that you need to export the web part and add the web part xml using limitedwebpartmanager

  • Anonymous
    May 18, 2014
    Hi, Can you write an article on how to add the webpart into the defined zone using JSOM? Thanks