3

I've developed a newsfeed app part that currently gets its information from a list, however we're keen to have article pages that we can click through too.

So far I've not found anything in the REST API to access pages or their content, is this doable via REST or do we need to stick to lists?

Thanks, Ben

  • access pages? What do you need exactly? – Nk SP Sep 15 '14 at 14:21
  • 1
    We're looking to create article pages, so we'd want to be able to query the page to pull back the title of the page, as well as some other custom fields we would add. – Ben Short Sep 15 '14 at 14:23
4

For getting the pages you could utilize the same REST endpoint that is used for getting List Items, for example: http://<site url>/web/lists/getbytitle('<list title>')/items(<item id>)

JavaScript Example

function getListItem(webUrl,listName,listItemId, complete, failure) 
{    
    $.ajax({       
       url: webUrl + "/_api/web/lists/getbytitle('" + listName + "')/items(" + listItemId + ")",   
       type: "GET",   
       processData: false,  
       contentType: "application/json;odata=verbose",
       headers: {   
          "Accept": "application/json;odata=verbose"
       }, 
       success: function (data) {           
           complete(data.d);   
       },      
       error: function (data) {            
           failure(data);    
       }  
    });
}

Usage

The following example demonstrates how to retrieve the page and print its properties:

getListItem(_spPageContextInfo.webAbsoluteUrl,'Pages',2,printPageDetails,logError);

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

function logError(error){
    console.log(JSON.stringify(error));
}
  • Perfect, thanks! I'm having a small issue with the results of '/web/lists/getByTitle('Pages')/Items' whereby it's only pulling back two objects, one is a valid article and the other is returning 'Page not found'. However, there are five pages in the library - what would cause SharePoint to only return one? – Ben Short Sep 16 '14 at 8:32
  • Ignore the above, I'm being completely stupid, was looking at the 'Site Pages' library while querying the 'Pages' library. – Ben Short Sep 16 '14 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.