I am using SharePoint 2013. Is there any way I can programmatically create a page using ONLY th Client Object Model? Also notice that the page should be editable for further use.
2 Answers
Use PublishingWeb.AddPublishingPage method to create a new PublishingPage object in the PublishingWeb from Microsoft.SharePoint.Client.Publishing namespace in SharePoint 2013 CSOM.
Example
public static PublishingPage CreatePublishingPage(ClientContext ctx, string pageName, string pageLayoutName)
{
var pubWeb = PublishingWeb.GetPublishingWeb(ctx, ctx.Web);
var pageInfo = new PublishingPageInformation
{
Name = pageName,
PageLayoutListItem = GetPageLayoutByName(ctx, pageLayoutName)
};
var publishingPage = pubWeb.AddPublishingPage(pageInfo);
ctx.ExecuteQuery();
return publishingPage;
}
public static ListItem GetPageLayoutByName(ClientContext ctx, string name)
{
var list = ctx.Site.GetCatalog((int)ListTemplateType.MasterPageCatalog);
var qry = new CamlQuery
{
ViewXml = string.Format("<View><Query><Where><Eq><FieldRef Name='FileLeafRef' /><Value Type='Text'>{0}</Value></Eq></Where></Query></View>",name)
};
var result = list.GetItems(qry);
ctx.Load(result);
ctx.ExecuteQuery();
var item = result.FirstOrDefault();
return item;
}
Usage
using (var ctx = new ClientContext(webUri))
{
var page = CreatePublishingPage(ctx, "Welcome.aspx", "BlankWebPartPage.aspx");
}
This seems to be the answer, but I haven't tested it!
-
3