使用 Sharepoint 2013 REST Api/CSOM 检索发布图像字段 [英] Retrieve Publishing image field with Sharepoint 2013 REST Api / CSOM

查看:41
本文介绍了使用 Sharepoint 2013 REST Api/CSOM 检索发布图像字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

We're using the Sharepoint 2013 REST API to get all news items from the Sharepoint. We made a custom ContentType 'Newsitem' with several properties including a Publishing Image Field.

  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 当然会使调用失败.

Using standardUri for my REST call, I retrieve all properties but no TeaserImg. Explicitly selecting TeaserImg makes the call fail of course.

为什么我找不到 TeaserImg,这在 Sharepoint 2013 REST Api 中是不可能的,我应该改用 CSOM 吗?

Why can't I find the TeaserImg, isn't this possible with Sharepoint 2013 REST Api and should I use CSOM instead?

推荐答案

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

It does not seem possible to retrieve Publishing Image fields using List Item Collection endpoint.

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

There is a workaround, publishing fields could be retrieved using ListItem.FieldValuesAsHtml property via SharePoint REST endpoint as demonstrated below

限制:需要执行两个请求.

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:

The following example demonstrates how to retrieve page fields including publishing fields, such as 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


Probably the best solution here would be to utilize 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 2013 REST Api/CSOM 检索发布图像字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆