Can JQGrid work with SharePoint result source REST API - sharepoint

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";

How to debug
You should check if the rest api returns values correctly.
Check whether the type of the variable using length has a length attribute.

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.

Related

Do I require permissions to run SPServices on Sharepoint?

I require assistance with Sharepoint. I've tried multiple ways to retrieve data from a list and have had little to no success, after much reading and searching I'm still no further ahead.
I am using a list made by another user, which I can add,edit and delete items from. When calling this list using SPServices I seem to hitting a wall. Here is my third attempt at trying to access the list and now I have received a 404 response and responsetext is null.
The URL is correct, cause it actually loads the list of values.
If i have an empty webURL parameter, the parameter of responsetext has a helpful SOAP response stating the following:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown.</faultstring><detail><errorstring xmlns="http://schemas.microsoft.com/sharepoint/soap/">
List does not exist.
The page you selected contains a list that does not exist. It may have been deleted by another user.
</errorstring><errorcode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x82000006</errorcode></detail></soap:Fault></soap:Body></soap:Envelope>
Here is my call which when I define the webURL to point to the list, it always returns a http 404 with responseText=null no matter what the url is. This is not very helpful. The Url I am pointing to loads the list.
function getListItems_RFC(){
var url = $().SPServices.SPGetCurrentSite() +
"/_vti_bin/listdata.svc/RFCExtract";
console.log("getListItems_RFC() "+ url);
$().SPServices({
operation: "GetListItems",
webURL: url,
async: false,
listName: "RFC Extract",
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
completefunc:
function (xData, Status) {
console.log(Status); //outputs error
console.log(xData); //outputs array responseText:null and status:404
$(xData.responseXML).SPFilterNode("m:properties").each(function() {
var liHtml = "<li>" + $(this).attr("d:Title") + "</li>";
$("#debug").append(liHtml);
});
}
});
};
I have modified the url each possible way:
var url = $().SPServices.SPGetCurrentSite() +
"/_vti_bin/listdata.svc/"; //responseText=null, status:404
var url = $().SPServices.SPGetCurrentSite();//responseText=null, status:404
var url = "" //responseText=soapresponse above, status:500
Why is this not working??? What am I doing wrong???
Can you change your implemetation to something other ?
Your way is very complicated by my opinion. Third party libraries here are unnecessary.
Good way is to use out-of-box REST API or JSOM. It is easy to use.
I see you want to get list item Title field.
Use REST API this way:
http://site url/_api/web/lists/GetByTitle('Test')/items?$select=Title
Here you can find example:
https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-lists-and-list-items-with-rest
https://www.c-sharpcorner.com/blogs/retrieve-sharepoint-list-items-using-rest-api
May be you can use for SharePoint 2010:
var myRestApiUrl = _spPageContextInfo.webServerRelativeUrl + "/_vti_bin/ListData.svc/RFCExtract?$select=Title";
var get = function (url) {
return jQuery.ajax({
url: url,
type: "GET",
processData: false,
headers: {
Accept: "application/json;odata=verbose",
}
});
};
get(myRestApiUrl)
.then(function(response){
// TODO: do actions with response
});

DataTables reload table with different pageLength param

Based on this question.
I initialize my datatables.js table with the following code:
var table_entity = $('#myTable').DataTable({
"pageLength":getParameterByName('pageLength'),//getting param from query string
fixedHeader: true,
"ajax": {
"url": myUrl,
"data": function(d) {
d.type = $('#mytype').val();
}
}
});
So I know how to reload my datatable with different ajax params. Now I need to reload it with different pageLength param which does not belong to ajax section and is recorded during datatables init stage. I tried to pass it directly during reloading with the following code:
table_entity.ajax.reload({
"pageLength":77 //some new param different from the initial one
});
But it did not work, the table is reloaded with initial pageLength value. Any ideas how to fix it would be welcome. Thank you.
UPD_1
managed to do that with
1st: complete destruction of a table:
$('#MyTable').DataTable().destroy();
$('#MyTable tbody').empty();//note tbody here
2nd: reinit the table with updated params:
table_data.displayStart = Number(getParameterByName('displayStart'));
table_data.pageLength = Number(getParameterByName('pageLength'));
table_data.iDisplayStart = Number(getParameterByName('displayStart'));
table_data.iDisplayLength = Number(getParameterByName('pageLength'));
$('#MyTable').DataTable(table_data);
Note 2 params - iDisplayStart and iDisplayLength here
Is it possible to do the same without destruction of the table, but during ajax.reload phase? Thank you.
Is it possible to do the same without destruction of the table, but
during ajax.reload phase?
You can hook into the xhr.dt event and update page.len() from there. Example :
$('#example').on('xhr.dt', function(e, settings, json, xhr) {
table.page.len(json.data.length).draw()
})
Will dynamically set the page length to maximum i.e length of data. Demo -> http://jsfiddle.net/d72zbyus/
So you could use table.page.len(77).draw() or whatever. xhr.dt is triggered after each AJAX request.
It is not clear to me where your new pageLength come from exactly. If it is part of the returned JSON (as I suspect) you could do table.page.len(json.pageLength).draw().

Can Cloudant list/show functions return objects and arrays(any JSON)?

As per the API documentation for Cloudant: Show function can be used to render a document in a different format or extract only some information from a larger document. Same is the case for a list function, the only difference is that it applies on a set of documents. I created a design document with a show function as follows:
{ "shows": { "showDemo":"function(doc,req){return {'body': doc, 'headers':{'Content-Type':'application/json'}}}" } }
When I use this function, _design/showFunc/_show/showDemo/doc1, I get the following error:
{ "error": "unknown_error", "reason": "badarg", "ref": 1793182837 }
I have observed the same error when the show function returns an array. However, no error is given when HTML,Text, XML is returned. Can we say that list/show functions can only return data in a format other than JSON? This example shows the "Accept" header for req object request Object.
What's happening here is that the show function needs to return a response object. From the docs (see http://docs.couchdb.org/en/2.1.0/json-structure.html#response-object) the body field needs to be a string, so you can return whatever you like but it needs to be stringified or otherwise turned into a format that can be sent as HTTP.
If you want to send JSON then doing JSON.Stringify(doc) as the value for body should do what you expect.

How can i pass input argument when writing loopback-testing

I am writing a test driven development for my strongloop API code with the help of loopback-testing .
Here they do not have any detailed document on this, so i am stuck with case of argument passing with the API call
Example i have a below case,
Method : PUT
URL : /api/admin/vineyard/<vineyard_id>
i need to pass the below arguments with this URL
1. 'vineyard_id' is a id of vine, it should be an integer .
2. in header = 'token'
3. in body = '{'name':'tastyWine','price':200}'
How can i pass these three arguments with this API ?
I can easily handle ,if there is only two types of arguments
Example :
Method : POST
`/api/user/members/<test_username>/auth'`
arguments : test_username and password
I can handle this like this ,
lt.describe.whenCalledRemotely('POST',
'/api/user/members/'+test_username+'/auth', {
'password': test_passwords
},
But how can i handle the above case , Many thanks for your answers for this example.
I'm not entirely sure what your specific problem is, but I will attempt to walk through everything you should need.
I am assuming you are using the predefined prototype.updateAttributes() method for your model as described here.
Next assumption is that you want to use the built-in authentication and authorization to allow the user to call this method. Given that assumption, you need something like this in your test code:
var vineyard_id = 123; //the id of the test item you want to change
var testUser = {email: 'test#test.com',password: 'test'};
lt.describe.whenCalledByUser(testUser, 'PUT', '/api/admin/vineyard/'+vineyard_id,
{
'name':'tastyWine',
'price':200
},
function () {
it('should update the record and return ok', function() {
assert.equal(this.res.statusCode, 200);
});
}
);
If you are using the out-of-the-box user model, you should be fine, but if you extended the model as is commonly done, you may need something like this early on in your test file:
lt.beforeEach.withUserModel('user');
Also, be aware of a few (currently incomplete) updates to will allow for better handling of built-in model extensions: Suggestions #56, Add support for non-default models #57, and givenLoggedInUser() function throws error #59.

When no data is returned from database

I am intiating a loading panel in init method and hiding it in ReturnDataPayload event.This is working perfectly when data Table has got some values in it.But when there is no data returned from database , the control is not going to returnDataPayLoad event.Please help me in finding an event which will be fired even when the response doesn't have any data or tell me a way to hide the loading panel.
If you want a custom behavior, use DataSource's sendRequest method of the dataTable's dataSource
(function() {
var YdataTable = YAHOO.widget.DataTable,
YdataSource = YAHOO.util.DataSource;
var settings = {
container:"<DATATABLE_CONTAINER_GOES_HERE>",
source:"<URL_TO_RETRIEVE_YOUR_DATA>",
columnSettings:[
{key:"id", label:"Id"}
],
dataSourceSettings:{
responseType:YdataSource.TYPE_JSON,
responseSchema:{
resultsList:"rs",
fields:[
{key:"id"}
]
}
},
dataTableSettings:{
initialLoad:false
}
}
var dataTable = new YdataTable(
settings.container,
settings.columnSettings,
new YdataSource(
settings.source,
settings.dataSourceSettings),
settings.dataTableSettings);
})();
keep in mind No matter which source is your data: XML, JSON, JavaScript object, TEXT, you always will get your data in a unified way through DataSource's sendRequest method. So when you want to retrieve your data and, at the same time, add custom behavior, use it
dataTable.getDataSource().sendRequest(null, {
success:function(request, response, payload) {
if(response.results.length == 0) {
// No data returned
// Do what you want right here
// You can, for instance, hide the dataTable by calling this.setStyle("display", "none");
} else {
// Some data returned
// If you want to use default the DataTable behavior, just call
this.onDataReturnInitializeTable(request, response, payload);
}
},
scope:dataTable,
argument:dataTable.getState()
});
The properties of the response are
results (Array): Your source of data in a unified way. For each object in the results Array, There is a property according to responseSchema's fields property. Notice i use response.results.length to verify if some data has been returned
error (Boolean): Indicates data error
cached (Boolean): Indicates cached response
meta (Object): Schema-parsed meta data
On the YUI dataTable page, look for Loading data at runtime to see some built-in functions provided by YUI dataTable
I hope it can be useful and feel free to ask for help for anything else you want about YUI. See a demo page of nice features of YUI dataTable

Resources