0
votes

I tried to see if I can use JQGrid to display SharePoint search results by querying SharePoint result source with REST API.

The code to use REST API to call result source works in browser and Postman:

var apiUrl =_spPageContextInfo.webAbsoluteUrl +/_api/search/query?querytext='Mike'&rowlimit=100&selectproperties='RequestNameOWSTEXT, SubmittedByOWSUSER,ExaminiationNameOWSTEXT,PublishedDate1OWSDATE&sourceId='d90c19xx-7b3x-42bx-8fbx-d1dxxxx543ffa7', many results are returned.

But when I used it in JQGrid, got an error at line 44: Uncaught TypeError: Cannot read property 'length' of undefined

$.ajax({ url: apiUrl,type: "GET", async: false, headers: { "accept": "application/json;odata=verbose" }, success: function (data) {

line 44: $.each(data.d.results, function (index, value) { .............................

I guess the api call doesn't return result so the data is undefined. Wondering is something wrong with that api?

A different api like below works to return results:

var apiUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('documentrequest')/Folders?$expand=ListItemAllFields,AuthorID/id&$select=Name,ItemCount,ListItemAllFields/AuthorId,ListItemAllFields/ExaminiationName,ListItemAllFields/PublishedDate1&$filter=Name ne 'Forms' and ItemCount gt 0";

2
Please share the code that uses the length attribute mentioned in the error code.Amos
Also share the whole jqGrid setup and related functions to it.Tony Tomov
the code failed with api uses result source, returned undefined, guess no result for the api call. jqGrid setup is the same as previous api. Previous api works: var apiUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/web/GetFolderByServerRelativeUrl('documentrequest')/Folders?$expand=ListItemAllFields,AuthorID/id&$select=Name,ItemCount,ListItemAllFields/SubmitterName,ListItemAllFields/ExamName,ListItemAllFields/PublishedDate1,&$filter=Name ne 'Forms' and ItemCount gt 0";MWAN001
Need to use results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results to retrieve data from the search results document sets are displayed in the JGGrid just fine but the attachments not displayed in jqgridMWAN001
RequestNameOWSTEXT, SubmittedByOWSUSER,ExaminiationNameOWSTEXT,PublishedDate1OWSDATE are managed properties that are queryable but no value returned from the search api call. search api call returns system columns, like contenttypeId, filename, path but not the custom column values.MWAN001

2 Answers

0
votes

How to debug

  1. You should check if the rest api returns values correctly.
  2. Check whether the type of the variable using length has a length attribute.
0
votes

It works, I figured out.. SharePoint search api itself provides only Filename, ContentTypeID, and Path. I use those values to run another api call to get the meta data, then add the values to array, which is used to feed the jqgrid.

Just get all the data into array, then feed to jqgrid when the data is ready.