2

Does REST service of SharePoint 2013 support us to get the content of HTML files of specified page? What I mean is request the HTML text of display form without navigate to there, like you clicked on list item and go to page 'view item'

2 Answers 2

2

The following request demonstrates how to retrieve page content using SharePoint REST:

/_api/Web/Lists/getByTitle('Pages')/items(<item id>)?$select=PublishingPageContent

where

  • Pages - the title of Pages library
  • item id - List Item Id

JavaScript example

The following example demonstrates how to retrieve Publishing Page content:

var getPageContent =  function (webUrl,itemId,result) {
    var listTitle = "Pages";
    var url = webUrl + "/_api/web/lists/GetByTitle('" + listTitle + "')/items(" + itemId + ")/PublishingPageContent";
    $.getJSON(url,function( data ) {
        result(data.value); 
    });
}

Usage

getPageContent('https://contoso.sharepoint.com/',1,function(pageContent){
   console.log(pageContent); 
});
4
  • Hi Vadim, It didn't work, its always return null, my code here var abc = function(){ var query = "/_api/web/lists/getByTitle('Company')/getItemById(1)?$select=PublishingPageContent"; $.ajax({ url: _spPageContextInfo.webAbsoluteUrl + query, type: "GET", headers: { "ACCEPT": "application/json;odata=verbose" }, success: function(data){ var d = data; alert(d); }, error: function(data){ console.log(JSON.stringify(data)); } }); }
    – tbbt
    Commented Oct 14, 2014 at 7:02
  • I assumed that you are trying to get content from Pages library, could you please update your question and specify where (what field) content is stored Commented Oct 14, 2014 at 18:51
  • See the updated answer for JavaScript example Commented Oct 24, 2014 at 7:12
  • Thanks you very much, because it's already work perfectly, but except in my list because the list has a custom autocomplete lookup field, it's doesn't work with that column
    – tbbt
    Commented Nov 4, 2014 at 8:04
0

Using late Pnp-core-js library you can do this like so:

pnp.sp.web.lists.getByTitle("News").items.getById(item.Id).fieldValuesAsHTML
 .select("PublishingRollupImage").get().then((response: any) => { console.dir(response); });

A example using es6 / Typescript - iterating a list of news - might look as follows:

public static getNewsFeed(options?: any): Promise<INewsFeedItem[]> {

return new Promise<INewsFeedItem[]>((resolve: any) => {
  pnp.setup({
    headers: {
      "Accept": "application/json; odata=verbose",
    },
  });

  pnp.sp.web.lists.getByTitle("Pages").items.get().then((response: any) => {

    let items: INewsFeedItem[] = [];
    let promises: Promise<any>[] = [];

    response.forEach(function (item: INewsFeedItem) {

      let promise = pnp.sp.web.lists.getByTitle("Pages").items.getById(item.Id).fieldValuesAsHTML.select("PublishingRollupImage").get();
      promises.push(promise);

      promise.then((response: any) => {
        items.push({ Title: item.Title, Id: item.Id, PublishingRollupImage: response.PublishingRollupImage, PublishingPageContent: item.PublishingPageContent });
      });
    });

    Promise.all(promises).then(() => {
      resolve(items);
    });
  });
});

}

To clarify: The procedure still requires extra requests to the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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