How To Read The SharePoint News Using Rest API In SPFx

Sharing news among employees is a big challenge for any organization. The Intranet is a possible solution. Companies can develop digital intranet as a communication and collaboration tool with all the custom settings they prefer.

But still, we can’t avoid news from SharePoint sites. Let’s see how to read the published SharePoint news through Rest API using SPFx.

SharePoint Search

SharePoint is one of the most powerful tools in its search feature. It allows looking for the data across sites/tenants. The same concept is going to help us to read the promoted news from the SharePoint sites.

There are already a lot of REST API URLs available to search data in SharePoint like people search and document search using specific id's. Let’s jump into the implementation. Here the main key parameter to find the published news is with its promoted state. If the promoted state of a piece of news is set to 2 then that means the news will be published and available for everyone to read.

Below is the URL to get the promoted news from a site.

{SiteUrl}/_api/search/query?querytext='IsDocument:True AND FileExtension:aspx AND PromotedState:2'

Here you can define the search scope with a slight change in the URL. Let’s say we have a site called Intranet and you want to search the promoted news only within that site, the URL will be like below,

https://tenantName.sharepoint/com/sites/Intranet/_api/search/query?querytext='IsDocument:True AND FileExtension:aspx AND PromotedState:2'

If you want to get all the published news from all the sites across the tenant then your site URL in the REST API URL must point to SharePoint search site and the URL will look like below,

https://tenantName.sharepoint/com/search/_api/search/query?querytext='IsDocument:True AND FileExtension:aspx AND PromotedState:2'

Don’t worry that you need to authenticate two different sites as SPFx takes care of it internally, that’s the beauty of SharePoint Framework apps. If the current user has permission over the entity he can play around with it, otherwise he can’t.

Error - Microsoft.SharePoint.Client.UnknownError

Before going to the actual code sample, let me show you the expected error and solution for that.

We might end up the with weird error Microsoft.SharePoint.Client.UnknownError.

Office 365 API’s use OData v4.0 protocol for exchanging data between the client and the server like below,

  1. Accept: application/json;odata.metadata=minimal  
  2. OData-Version: 4.0  

If the same headers are used in SharePoint Search API in Search site then we will end up with the below error,

{  
    "error": {  
        "code": "-1,  
        Microsoft.SharePoint.Client.UnknownError ",  
        "message": "Unknown Error"  
    }  
}

The same protocol will work fine in all other SharePoint API’s except Search API. So, it’s better to use OData v3.0 protocol in SharePoint Search API like the below code snippet.

Code Snippet

let spOpts = {  
    headers: {  
        'odata-version': '3.0',  
        'Content-Type': 'application/json; odata=verbose'  
    }  
};  
this.context.spHttpClient.get(requestUrl, SPHttpClient.configurations.v1, spOpts).then((response: SPHttpClientResponse) => {  
    if (response.ok) {  
        response.json().then((responseJSON) => {  
            if (responseJSON != null) {  
                let searchResult: any = responseJSON;  
            }  
        });  
    }  
});  

How to get the name of the Site in SharePoint Search Result?

As we search the entire tenant for the news, how do we differentiate the news of different sites?

Yes, we do get the Path/OriginalPath of the news page and we do have the site URL in that, but that’s not the efficient way to do it. In SharePoint, there is a default Managed Metadata property called “SiteTitle, which will exactly give you the site name from which the news is from.

To get the SiteTitle you need to select it using the selectProperties in the REST Url like below,

https://tenantName.sharepoint/com/search/_api/search/query?querytext='IsDocument:True AND FileExtension:aspx AND PromotedState:2' &selectproperties='Title,Author,PictureThumbnailURL,SiteTitle,SPWebUrl,OriginalPath,LastModifiedTime,ViewsLifeTime,ViewsRecent,LikesCount,PromotedState’

I hope you have got to know about SharePoint news, and the search feature to crawl the published news. Follow my next blog to know about handling the SharePoint search result row limit.

If you have any questions/issues about this article, please let me know in the comments.