2016-01-19 51 views
0

我试图弄清楚如何获取用于特定页面的页面布局,但是没有使用vail。如何从SharePoint Online中的项目获取页面布局

我使用的代码是:

public void ItemAddedHandler(ClientContext clientContext, Guid listId, int listItemId) { 

    var web = clientContext.Web; 

    List list = clientContext.Web.Lists.GetById(listId); 
    ListItem item = list.GetItemById(listItemId); 
    clientContext.Load(item, i => i["PublishingPageLayout"]); 

    // item["PublishingPageLayout"] should contain the Page Layout used but is empty. 
} 

什么我错在这里做什么?

+0

我已经做了一个测试,并检查“PublishingPageLayout”属性上的删除也,然后我可以得到文件的页面布局。奇怪。似乎布局没有完全设置项目添加(我正在使用事件接收器触发创建该文件中的代码) –

回答

0

在尝试访问项目属性之前,请确保您在clientContext对象上致电ExecuteQuery/ExecuteQueryAsync

当与客户端对象模型,你执行了上述两种方法实际上是建筑查询什么检索之前做的,但要等到执行查询本身,什么都不会被发送一切工作/接收/从SharePoint 。

+0

实际的错误不是我忘记了ExecuteQuery,因为即使在添加ExecuteQuery后我什么都没有。在执行ItemUpdatedHandler之前,PublishingPageLayout的属性不适用。所以当实际添加文档时,该属性不存在,但在第二次更新时,UpdateItem上的事件接收器已使用pagelayout更新。 –

0

正确的代码如下。我必须检查updateitem事件处理程序中的pagelayout属性,而不是在itemadded处理程序中。

public void ItemUpdatedHandler(ClientContext clientContext, Guid listId, int listItemId, ContextInfo logContext) { 
    ListItem item = list.GetItemById(listItemId); 
    clientContext.Load(item, i => i["PublishingPageLayout"], i => i.DisplayName); 
    clientContext.ExecuteQuery(); 
    FieldUrlValue fuv = (FieldUrlValue)item["PublishingPageLayout"]; 
} 
相关问题