sharepoint - 使用Sharepoint 2013 REST Api/CSOM检索发布图像字段

标签 sharepoint sharepoint-2013 csom

我们正在使用Sharepoint 2013 REST API从Sharepoint获取所有新闻项。
我们制作了一个自定义ContentType'Newsitem',其中包含多个属性,包括发布图像字段。

  var contentTypeId = "0x01100018B03AC7E8312648AEA00851DEDBCAF802";
  var standardUri = "https://examplesite.com/blog/_api/lists/getbytitle('Messages')/items?$top=7&$filter=startswith(ContentTypeId,'" + contentTypeId + "')";
  var selectiveUri = "https://examplesite.com/blog/_api/lists/getbytitle('Messages')/items?$top=7&$filter=startswith(ContentTypeId,'" + contentTypeId + "')&$Select=Title,Teaser,Body,ShowAt,TeaserImg";

在我的REST调用中使用standardUri,我检索了所有属性,但没有检索到TeaserImg。
明确选择TeaserImg当然会使调用失败。

为什么找不到TeaserImg,Sharepoint 2013 REST Api无法做到这一点,我应该改用CSOM吗?

最佳答案

似乎无法使用“列表项集合”端点检索Publishing Image字段。

有一种解决方法,可以通过SharePoint REST终结点使用ListItem.FieldValuesAsHtml属性检索发布字段,如下所示

Limitation: it requires to perform two requests.



如何使用SharePoint 2013 REST检索发布字段
function getJson(endpointUri, success, error) 
{    
    $.ajax({       
       url: endpointUri,   
       type: "GET",   
       processData: false,  
       contentType: "application/json;odata=verbose",
       headers: {   
          "Accept": "application/json;odata=verbose"
       }, 
       success: success,
       error: error
    });
}


function getPublishingPage(webUrl,listName,listItemId,publishingProperties, success, failure) 
{
    var itemUri =  webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")";  
    getJson(itemUri,
       function(data){
           var pageItem = data.d;

           var selectProperties = [];  
           for(var idx in publishingProperties){
               if(!pageItem.hasOwnProperty(publishingProperties[idx])){
                   selectProperties.push(publishingProperties[idx]);
               }
           }
           if(selectProperties.length > 0) {
              //construct an additional query 
              var query = '/FieldValuesAsHtml?$select=' + selectProperties.join(',');
              var endpointUri = pageItem['__metadata'].uri + query;
              getJson(endpointUri,
                 function(data){
                    for(var property in data.d){
                       if(property == "__metadata") continue; 
                       pageItem[property] = data.d[property];   
                    }
                    success(pageItem);  
                 },
                 failure);
           } 
           else {
              success(pageItem);
           }   
        },
       failure);
}

用法

下面的示例演示如何检索页面字段,包括发布字段,例如PublishingRollupImage:
getPublishingPage(_spPageContextInfo.webAbsoluteUrl,'Pages',3,['PublishingRollupImage','PublishingPageImage'],printPageDetails,logError);

function printPageDetails(pageItem)
{
    console.log('Page Content: ' + pageItem.PublishingPageContent);
    console.log('Page Title: ' + pageItem.Title);
    console.log('Page Rollup Image ' + pageItem.PublishingRollupImage);
}

function logError(error){
    console.log(JSON.stringify(error));
}

可能最好的解决方案是利用CSOM
function getListItems(listTitle,success,error)
{
   var ctx = SP.ClientContext.get_current();
   var list = ctx.get_web().get_lists().getByTitle(listTitle);
   var items = list.getItems(SP.CamlQuery.createAllItemsQuery());
   ctx.load(items);
   ctx.executeQueryAsync(function() {
       success(items);
   },error);
}



getListItems('Pages',printPageItemsDetails,logError);

function printPageItemsDetails(pageItems)
{
    for(var i = 0; i < pageItems.get_count();i++) {
        var pageItem = pageItems.getItemAtIndex(i);
        console.log(pageItem.get_fieldValues()['PublishingPageContent']);
        console.log(pageItem.get_fieldValues()['PublishingRollupImage']);
    }
}

关于sharepoint - 使用Sharepoint 2013 REST Api/CSOM检索发布图像字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25852997/

相关文章:

excel - 如何将 .iqy 文件作为数据源加载到 Excel 中

c# - InputFormTextBox 在 SharePoint web 部件中设置焦点和 onblur 事件

sharepoint - 在 SharePoint 2013 中自定义 OOB Web 部件

javascript - 悬停时显示列描述

sharepoint-2013 - 使用 CSOM 安装 Sharepoint 应用程序

sharepoint - 仅将当前 View 中的项目与 outlook 同步列表

c# - 无法在共享点中部署额外的 ddl(Ajax 控制工具包)

c# - 通过 CSOM 动态加载项目联机属性

rest - 应用不同的 RowLimit 参数时,TotalRows 属性显示不同的结果(SharePoint 2013 Search REST 和 CSOM Api)

javascript - 阅读 SharePoint 分类术语库和 getDefaultLabel(lcid)