Add web part page from Xml node by client object model
Following XElement as xPage:
<Page Src="Pages\WebPartPage.aspx" FileName="MyPage.aspx" FolderName="Site Pages" SetAsDefault="TRUE"/>
WebpartPage.aspx file is in our case webpart page with three columns, header and footer (just saved source from Sharepoint Designer).
Its code is at the end of this post.
public static void AddPageToWeb(string webUrl, string folderName, string pageFileName, string pageFilePath, bool setAsDefault)
{
ClientContext context = new ClientContext(webUrl);
Web web = context.Web;
context.ExecuteQuery();
List rootFolder = web.Lists.GetByTitle(folderName);
context.Load(rootFolder);
context.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = @"<View>
<Query>
<Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>" + pageFileName + @"</Value></Eq></Where>
</Query>
</View>";
ListItemCollection existingPages = rootFolder.GetItems(query);
context.Load(existingPages);
context.ExecuteQuery();
if (existingPages.Count == 0)
{
//Load text file
string content;
using (StreamReader sr = new StreamReader(pageFilePath))
{
content = sr.ReadToEnd();
}
FileCreationInformation fileInfo = new FileCreationInformation();
fileInfo.Url = pageFileName;
fileInfo.Content = System.Text.Encoding.UTF8.GetBytes(content);
Microsoft.SharePoint.Client.File file = rootFolder.RootFolder.Files.Add(fileInfo);
context.ExecuteQuery();
}
if (setAsDefault)
{
Folder folder = web.RootFolder;
context.Load(folder);
folder.WelcomePage = "SitePages/" + pageFileName;
folder.Update();
context.ExecuteQuery();
}
}
public static void AddPageToWebFromXElement(string webUrl, XElement xPage, string rootWebpartDirectory)
{
//Read attributes from XElement 'Page'
string pageFilePath = rootWebpartDirectory + "\\" + xPage.Attribute("Src").Value;
//string pageTitle = xPage.Attribute("Title").Value;
string pageFileName = xPage.Attribute("FileName").Value;
string folderName = xPage.Attribute("FolderName").Value;
XAttribute aSetAsDefault = xPage.Attribute("SetAsDefault");
bool setAsDefault = false;
if (aSetAsDefault != null)
{
setAsDefault = Convert.ToBoolean(aSetAsDefault.Value);
}
AddPageToWeb(webUrl, folderName, pageFileName, pageFilePath, setAsDefault);
}
ASPX file source code:
<%-- _lcid="1051" _version="15.0.4420" _dal="1" --%>
<%-- _LocalBinding --%>
<%@ Page language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=15.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=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.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="WpPage" Title="Záhlavie stránky webovej časti" 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="Táto webová časť sa nedá importovať." ImportErrorMessage="Táto webová časť sa nedá importovať." PartImageLarge="" IsIncludedFilter="" ExportControlledProperties="True" ConnectionID="00000000-0000-0000-0000-000000000000" ID="g_02120f39_10e0_4d47_8d3a_61a53b965a84" AllowClose="False" ChromeType="None" ExportMode="All" __MarkupType="vsattributemarkup" __WebPartId="{02120F39-10E0-4D47-8D3A-61A53B965A84}" 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" colspan="3" valign="top" width="100%">
<WebPartPages:WebPartZone runat="server" Title="loc:Header" ID="Header" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
</tr>
<tr>
<td id="Td1" name="_invisibleIfEmpty" valign="top" height="100%">
<WebPartPages:WebPartZone runat="server" Title="loc:LeftColumn" ID="LeftColumn" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
<td id="Td2" name="_invisibleIfEmpty" valign="top" height="100%">
<WebPartPages:WebPartZone runat="server" Title="loc:MiddleColumn" ID="MiddleColumn" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
<td id="Td3" name="_invisibleIfEmpty" valign="top" height="100%">
<WebPartPages:WebPartZone runat="server" Title="loc:RightColumn" ID="RightColumn" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
</tr>
<tr>
<td id="Td4" name="_invisibleIfEmpty" colspan="3" valign="top" width="100%">
<WebPartPages:WebPartZone runat="server" Title="loc:Footer" ID="Footer" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
</tr>
<SharePoint:ScriptBlock runat="server">if(typeof(MSOLayout_MakeInvisibleIfEmpty) == "function") {MSOLayout_MakeInvisibleIfEmpty();}</SharePoint:ScriptBlock>
</table>
</asp:Content>