TransWikia.com

Search REST API for documents modified by logged in user

SharePoint Asked on December 21, 2020

I am working on a query to get the documents modifies by logged in user. I am able to get the documents with the specific file extension.

var myurl = _spPageContextInfo.webAbsoluteUrl + '/_api/search/query?'+
"Querytext='* AND " +
    "(FileExtension:doc OR FileExtension:docx OR FileExtension:ppt OR FileExtension:pptx OR FileExtension:xls OR FileExtension:xlsx OR FileExtension:pdf OR ContentTypeId:0x010100F3754F12A9B6490D9622A01FE9D8F012*)'"+
'&sortlist='LastModifiedTime:descending'&RowLimit=10';

function onQuerySuccess2(data) {
    var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
     $.each(results, function (index, result) {
            var modifiedTime = result.Cells.results[9].Value;
            console.log(modifiedTime);
     });
}

function onQueryError(error){
    console.log(error);
}

$.ajax({ url: myurl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: onQuerySuccess2, error: onQueryError });

The above piece of code is working properly. The only thing pending is to show the modified by currently logged in user. For this, I am adding the

&querytemplate=’modifiedby:{User}’

So my query becomes:

var myurl = _spPageContextInfo.webAbsoluteUrl + '/_api/search/query?'+
"Querytext='* AND " +
    "(FileExtension:doc OR FileExtension:docx OR FileExtension:ppt OR FileExtension:pptx OR FileExtension:xls OR FileExtension:xlsx OR FileExtension:pdf OR ContentTypeId:0x010100F3754F12A9B6490D9622A01FE9D8F012*)'"+
'&sortlist='LastModifiedTime:descending'&RowLimit=10&querytemplate='modifiedby:{User}'';

But then specific file extension filtering stops working. What am I doing wrong? any guess.?

One Answer

Instead of specifying the querytext, you can directly specify it in the query template itself

Try and modify the below endpoint:

var myurl = _spPageContextInfo.webAbsoluteUrl + '/_api/search/query?'+
"queryTemplate='* ModifiedBy:{User} AND " +
    "(FileExtension:doc OR FileExtension:docx OR FileExtension:ppt OR FileExtension:pptx OR FileExtension:xls OR FileExtension:xlsx OR FileExtension:pdf OR ContentTypeId:0x010100F3754F12A9B6490D9622A01FE9D8F012*)'"+
'&sortlist='LastModifiedTime:descending'&RowLimit=10';

function onQuerySuccess2(data) {
    var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
     $.each(results, function (index, result) {            
            var modifiedTime = result.Cells.results[9].Value;
            console.log(modifiedTime);
     });
}

function onQueryError(error){
    console.log(error);
}

$.ajax({ url: myurl, method: "GET", headers: { "Accept": "application/json; odata=verbose" }, success: onQuerySuccess2, error: onQueryError });

Answered by Gautam Sheth on December 21, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP