restsharepointsharepoint-onlinespfxcaml

_LikeCount and _CommentCount field is missing on CAM Query Request


Hi i am creating spfx webpart to display most liked and commented news however _LikeCount and _CommentCount not appearing on the result.

See below code:

props.topCount = props.topCount === undefined ? 5: props.topCount;
  console.log("Count: " + props.topCount);
   
  const options: ISPHttpClientOptions = {
    headers: {'odata-version':'3.0'},
    body: `{'query': {
      '__metadata': {'type': 'SP.CamlQuery'},
      'ViewXml': '<View><Query><OrderBy><FieldRef Name="_CommentCount" Ascending="False" /></OrderBy><Where><Eq><FieldRef Name="PromotedState" /><Value Type="Number">2</Value></Eq></Where></Query><RowLimit>`+props.topCount+`</RowLimit></View>'
    }}`
  };

 
  const fetchData = async () => {
      
      let currentWebUrl  = props.sites === undefined ? props.origSiteURL : props.sites[0].url;
      console.log("Site URL: " + currentWebUrl);
      let requestUrl = currentWebUrl.concat("/_api/web/lists/GetByTitle('Site Pages')/GetItems");   
       console.log("Current URL: "+ requestUrl);
      props.spHttpClient.post(requestUrl, SPHttpClient.configurations.v1, options)  
      .then((response: SPHttpClientResponse) => {  
          if (response.ok) {  
              response.json().then((responseJSON) => {  
                  if (responseJSON!=null && responseJSON.value!=null){  
                    settop_news_like(responseJSON.value);  
                    console.log(settop_news_like);
                  }  
              });  
          }  
      });

  };

Tried also adding:

<ViewFields>
    <FieldRef Name="_LikeCount"></FieldRef>
    <FieldRef Name="_CommentCount"></FieldRef>
</ViewFields>

and this below but still no luck.

expand=fields($select=createdDateTime,BannerImageUrl,PromotedState,FileLeafRef,AuthorLookupId,Modified,_CommentCount,_LikeCount,Title,Description,LinkTitle)

 

Solution

  • There is a separate API to get like and comment count for the modern pages. The article below explains how to get these fields for pages using the RenderListDataAsStream api:

    https://www.eliostruyf.com/getting-likes-and-comments-from-modern-pages/