1

I want to get the Author field from Discussion forum list in SharePoint online using REST API in POST method. I tried the below approach, it is not working.

var queryUrl =
  '?$select=ID,Title,Body,Author/Title,Folder/ItemCount,FieldValuesAsText/FileRef&$expand=Author,FieldValuesAsText,Folder/ItemCount';
var url =
  siteUrl +
  "/_api/Web/lists/GetByTitle('" +
  listName +
  "')/GetItems" +
  queryUrl;

var camlQuery =
  '<View><Query>' +
  getQueryParams() +
  "<OrderBy><FieldRef Name='DiscussionLastUpdated' Ascending='False'/></OrderBy></Query><RowLimit>12</RowLimit></View>";

var requestData = {
  query: { __metadata: { type: 'SP.CamlQuery' }, ViewXml: camlQuery }
};

jQuery.ajax({
  url: url,
  method: 'POST',
  data: JSON.stringify(requestData),
  headers: {
    'X-RequestDigest': $('#__REQUESTDIGEST').val(),
    Accept: 'application/json; odata=verbose',
    'Content-Type': 'application/json; odata=verbose'
  },
  success: function(data, status, jqXHR) {
    dfd.resolve(data);
  },
  error: function(xhr) {
    dfd.reject(xhr);
  }
});
return dfd.promise();

The output is like below.

enter image description here

James Z
  • 12,209
  • 10
  • 24
  • 44
Philip S
  • 21
  • 1
  • 7
  • This looks similar. Give it a try. https://stackoverflow.com/questions/22462352/sharepoint-rest-get-user-title-in-a-single-rest-query – Tany3450 Dec 20 '18 at 10:44
  • The article which you shared one is using GET method. I want to implement in POST method. Please suggesst me. It is working fine for GET but not for POST. – Philip S Dec 20 '18 at 10:56

1 Answers1

1

As I have explained in this post, $expand doesn't seem to work on some special columns (like People), when querying via POST. The best way will be to use the Items API.

However, if you only want to proceed with this approach then, try replacing the queryUrl value with the following.

var queryUrl ='?$select=ID,Title,Body,Folder/ItemCount,FieldValuesAsText/FileRef,FieldValuesAsText/Author&$expand=FieldValuesAsText,Folder/ItemCount';

Basically, what I am trying to do is that instead of directly querying the Author field, I am requesting it from under the FieldValuesAsText. Pretty much the way you have requested the FileRef property.

Piyush
  • 830
  • 8
  • 19