Retrieve Publishing Image column from SharePoint REST API

Recently, I utilized SharePoint REST API to retrieve list item and one of the column is Publishing Image. I’m half surprise and half confuse why the Publishing Image column is not returned by the REST API. Trying to create a new custom list with Out of the Box Publishing Image site column (Page Image) and I still cannot find the publishing image column from REST API call result.

http://siteurl/_api/web/lists/getbytitle(“listTitle”)/items?$filter=Active eq 1&$select=Title,PublishingPageImage

Quickly google it and found this post and I’m feeling devastated. Why would you want to make a second call just for the one column.

Below is the same code from the post and Thanks to Vadim Gremyachev

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

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

References

http://stackoverflow.com/questions/25852997/retrieve-publishing-image-field-with-sharepoint-2013-rest-api-csom

Advertisements

Tagged: , , , , ,

2 thoughts on “Retrieve Publishing Image column from SharePoint REST API

  1. […] Following my previous post with accessing SharePoint List items using REST API, I’m starting to use Client Side Object Model in the Script Editor Webpart. Then, I found another issue where my CSOM script is only works when the Page in Edit mode. Why would that be ? Why I found two strange things in a week ? […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: