1

I am trying to use REST to return items from a list, the search query needs to be filtered by Managed Properties, by Path and by type to ensure only List Items are returned.

The Managed Property values are dynamic, and sometimes null. This means I can't include them in a Search Scope I think. I have looked into the documentation for a POST request to the Search API but I am not quite sure how to get it working.

$.ajax({
url: url + "/_api/search/query",
method: "POST",
headers: {
    "Accept": "application/json; odata=verbose",
    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
body: {
    '__metadata': {
        'type': 'Microsoft.Office.Server.Search.REST.SearchRequest'
    },
    'Querytext': 'ManagedProp:value',
    'SelectProperties': {
        'results': [
            "mp1", "mp2", "ect"
        ]
    },
    'SourceId': 'guid' //I have put the Path and limit to STSListItem in this results sourse
},
success: function(data) {
    console.log("no of items:  " + data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results.length);
    //data.d.query.PrimaryQueryResult RETURNING NULL
}
});

This is what I have so far, data.d.query.PrimaryQueryResult returns null from this

1

Did you try to use a QueryTemplate. You could dynamically change this in your javascript for every call. The QueryText could be the same for all queries, but can as easily be dynamic. You should be able to build the query dynamically for whatever managed property you want to filter on.

For example the following POST format:

'Querytext' : 'SharePoint',
'Querytemplate' : '{searchterms} Author:johndoe'

Will look for SharePoint that is related to author johndoe.

Changing both for every call should result is different search results.

BTW: a very handy tool for Search API is SharePoint Search Query Tool

Consider below example for correct implementation

```var searchQuery = {
            'request': {
            '__metadata': { 'type': 'Microsoft.Office.Server.Search.REST.SearchRequest' },
            'Querytext':'*',
            'QueryTemplate': '{searchterms} ContentType:Post contentclass:STS_ListItem_Posts PostOnHomepageOWSBOOL:1',
            'SelectProperties': {
                'results': ["Author", "AuthorOWSUSER", "Path", "Title", "PictureThumbnailURL", "Created", "SiteTitle", "owstaxIdSolutionPortfolios", "owstaxIdSecondaryTopics", "owsSecondaryTopics", "ListItemID", "PostOnHomepageOWSBOOL"
                ]
            },
            'RowLimit':10,
            'EnableSorting':true,
            'TrimDuplicates':false
        }
    };
  • So I am assuming I can use this like creating a Result Source? – Curtis Hennessy Jun 14 '17 at 12:38
  • It's sort of Result source. Result Source is your main 'searchable content'. This querytemplate is to separate the query (often input from a user) from a more complex query. You don't want to create a lot of result sources for this functionality. – Koen van der Linden Jun 15 '17 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.