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 Ima
It does not seem possible to retrieve Publishing Image
fields using List Item Collection endpoint.
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.
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);
}
Usage
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));
}
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']);
}
}