Written by

Jayakumar Balasubramaniam

Share

  

Category

see all categories
intranet as a service

How to read the SharePoint news using Rest API in SPFx

Sharing news between the employees are the big challenge between any organization, there comes Intranet as a solution. Companies develop digital intranet as a communication and collaboration tool with all the custom settings they prefer.

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

SharePoint Search:

SharePoint’s one of the most powerful tool is it’s search feature. It allows to look for the data across site/tenant. The same concept is going to help us to read the promoted news from the SharePoint sites.

There are already lot of REST API URLs available to search data in SharePoint like people search, 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 Promoted state of a news is set to 2 then it says that the news is 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 slight change in the URL. Let’s say we have site called Intranet and you want to search the promoted news only within that site, 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 siteurl 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 else 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.

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

Accept: application/json;odata.metadata=minimal

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"

  }

}

If is surprise that 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 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/Original Path of the News page and we do have the site URL in that, but that’s not the efficient way to do so. 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’

How to build SharePoint Framework Applications?

The following blog posts will help you for sure in detail about building SPFx applications,

                   Build SPFx webparts with Angular 4

                   Deep Dive into SPFx webparts configuration

                   Build SPFx extensions

                   Integrating SPFx apps with visual Studio

I hope you have get to know about SharePoint news, 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 comments.

Jayakumar Balasubramaniam

Written by Jayakumar Balasubramaniam

A technology evangelist working on Microsoft 365, SharePoint & Azure. Gets my hands dirty with latest and greatest of SharePoint, Azure & front-end frameworks. Currently working with “Hubfly – A unified digital workplace” as a Product Engineer. Getting in to the depth of the domain & technology, with my eyes for perfection, ensures that the product releases go right every time.