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

时间:2014-10-29 10:56:37

标签: sharepoint csom

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

2 个答案:

答案 0 :(得分:8)

尽管在SharePoint 2013 CSOM中引入了新的Microsoft.SharePoint.Client.Publishing namespace,但与SSOM相比,它仍然是相对有限的API。例如,CSOM PublishingPage class不会公开Layout属性,例如SSOM PublishingPage does

如何使用SharePoint CSOM

设置发布页面的页面布局

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

pageItem["PublishingPageLayout"] = new FieldUrlValue() { Url = "<page layout server relative url>", Description = "<page layout name>" };

示例

以下示例演示了如何为页面设置Blank Web Part page页面布局:

var pagesList = ctx.Web.Lists.GetByTitle("Pages");
var pageItem = pagesList.GetItemById(listItemId);
pageItem["PublishingPageLayout"] = new FieldUrlValue() { Url = "/_catalogs/masterpage/BlankWebPartPage.aspx", Description = "Blank Web Part page" };
pageItem.Update();
ctx.ExecuteQuery();

如何设置列表项的内容类型

要为列表项设置内容类型,必须在列表中提供内容类型。以下方法用于此目的:

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);

答案 1 :(得分:0)

尝试以下方法:

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);

Reference