sharepoint 2013更改CSOM中现有的发布页面布局

sharepoint 2013更改CSOM中现有的发布页面布局,sharepoint,csom,Sharepoint,Csom,我需要使用CSOM更改发布网站中现有发布页面的页面布局。我看到有CSOM object PublishingWeb,但它缺少获取发布页面的方法。请尝试以下操作: function updatePublishingPage(pageUrl,properties) { var context = SP.ClientContext.get_current(); var site = context.get_site(); var web = context.get_web(); var pa

我需要使用CSOM更改发布网站中现有发布页面的页面布局。我看到有CSOM object PublishingWeb,但它缺少获取发布页面的方法。

请尝试以下操作:

function updatePublishingPage(pageUrl,properties) {

var context = SP.ClientContext.get_current();
var site = context.get_site();    
var web = context.get_web();
var pageFile = web.getFileByServerRelativeUrl(pageUrl);
var pageItem = pageFile.get_listItemAllFields();
context.load(site);
context.load(pageItem);

context.executeQueryAsync(
    function () {

        for(var propName in properties) {
           var property = properties[propName]; 
           var itemValue = pageItem.get_item(propName);
           if(property.Type == "Url") {
               var pagelayoutUrl = site.get_url() + property.Value.split(',')[0].trim();
               itemValue.set_url(pagelayoutUrl);
               var pagelayoutDesc = property.Value.split(',')[1].trim();
               itemValue.set_description(pagelayoutDesc);
               pageItem.set_item(propName,itemValue);
           }

        }
        pageItem.update();
        context.load(pageItem);
        context.executeQueryAsync(
            function () {
               console.log(pageItem);
            },
            function (sender, args) {
               console.log('Failed: ' + args.get_message());
            }
        );    
    },
    function (sender, args) {
        console.log('Failed: ' + args.get_message());
    }
   );

}
请拨打以下电话:

var properties = {'PublishingPageLayout' : {'Type': 'Url','Value': '/_catalogs/masterpage/ArticleLeft.aspx, Image on left'}};  //Image on Left page layout
updatePublishingPage('/news/Pages/Latest-News.aspx',properties);

尽管在SharePoint 2013中引入了CSOM new,但与SSOM对应的API相比,它仍然相对有限。例如,CSOM不公开SSOM
PublishingPage
这样的
Layout
属性

如何使用SharePoint CSOM设置发布页面的页面布局 以下示例允许在SharePoint 2010/2013中使用CSOM设置页面布局:

pageItem["PublishingPageLayout"] = new FieldUrlValue() { Url = "<page layout server relative url>", Description = "<page layout name>" };
如何设置列表项的内容类型 要为列表项设置内容类型,内容类型必须在列表中可用。以下方法用于此目的:

public static void SetListItemContentType(ListItem item, ContentType contentType)
{
    var ctx = item.Context;
    //1. Ensure Content Type is available in List
    var list = item.ParentList;
    var result = ctx.LoadQuery(list.ContentTypes.Where(ct => ct.Name == contentType.Name));
    ctx.ExecuteQuery();
    var listContentType = result.FirstOrDefault();
    if (listContentType == null)
    {
        list.ContentTypes.AddExistingContentType(contentType);
        list.Update();
        ctx.ExecuteQuery();
    }
    //2. Set Content Type for List Item 
    item["ContentTypeId"] = contentType.Id;
    item.Update();
    ctx.ExecuteQuery();
}
用法


我尝试了这个,页面布局被更改为我们的自定义布局,但是内容类型仍然是OOB类型,我尝试更新“ContentTypeId”字段,但失败。这是因为自定义内容类型必须首先添加到列表中。答案已更新,请参阅“如何设置列表项的内容类型”一节
public static void SetListItemContentType(ListItem item, ContentType contentType)
{
    var ctx = item.Context;
    //1. Ensure Content Type is available in List
    var list = item.ParentList;
    var result = ctx.LoadQuery(list.ContentTypes.Where(ct => ct.Name == contentType.Name));
    ctx.ExecuteQuery();
    var listContentType = result.FirstOrDefault();
    if (listContentType == null)
    {
        list.ContentTypes.AddExistingContentType(contentType);
        list.Update();
        ctx.ExecuteQuery();
    }
    //2. Set Content Type for List Item 
    item["ContentTypeId"] = contentType.Id;
    item.Update();
    ctx.ExecuteQuery();
}
 var result = ctx.LoadQuery(ctx.Site.RootWeb.AvailableContentTypes.Where(ct => ct.Name == "Contoso Article Page"));
 ctx.ExecuteQuery();
 var pageContentType = result.FirstOrDefault();

 var pagesList = ctx.Web.Lists.GetByTitle("Pages");
 var pageItem = pagesList.GetItemById(7);
 SetListItemContentType(pageItem, pageContentType);