TransWikia.com

Sharepoint 2013 Query Filter Using POST

SharePoint Asked by Ciprian on November 11, 2021

I’m trying to use SharePoint 2013 REST api to query using POST method. I have found few solutions to query SPListItems using CAML but i cannot find a solution to use Linq like queries.

I’m trying in the following example to query SPFields of a list by passing some filtering expressions.(e.g. get all the taxonomy fields of a list).

Example:

            var expressionResult = generateResultOrElseExpression(1, fieldsExpressions, fieldsExpressions[0]);

            var lambda = Expression.Lambda<Func<Field, bool>>(expressionResult, Expression.Parameter(typeof(Field), "x"));

            var fieldsQuery = list.Fields.Where(lambda).Include(i => i.Title, i => i.InternalName, i => i.TypeAsString, i => i.SchemaXml, i => i.FromBaseType);

            var spFields = clientContext.LoadQuery(fieldsQuery);

            clientContext.ExecuteQuery();

This is the code that is not working:

    JSRequest.EnsureSetup();

    hostWebUrl = decodeURIComponent(JSRequest.QueryString["SPHostUrl"]);
    appWebUrl = decodeURIComponent(JSRequest.QueryString["SPAppWebUrl"]);

    var restQueryUrl = String.format("{0}/_api/sp.appcontextsite(@target)/web/lists/getByTitle('{1}')/fields" +
        "?@target='{2}'", appWebUrl, listTitle, hostWebUrl);

    var query = '<View><Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">Country</Value></Eq></Where></Query></View>';

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

    var requestBody = JSON.stringify(queryPayload);

    var executor = new SP.RequestExecutor(appWebUrl);

    executor.executeAsync({
        url: restQueryUrl,
        method: "POST",
        headers: {
            "accept": "application/json;odata=verbose",
            "content-type": "application/json;odata=verbose",
            "content-length": requestBody.length,
            "X-RequestDigest": $("#__REQUESTDIGEST").val(),
            "X-HTTP-Method": "POST"
        },
        body: requestBody,
        success: function (data, textStatus, xhr) {
            deferred.resolve(JSON.parse(data.body));
        },
        error: function (xhr, textStatus, errorThrown) {
            deferred.reject(JSON.stringify(xhr));
        }
    });

I could just get all the fields and then extract the data on the client side, but it must be a better way to do this (at least i hope).

Any ideas on how to use Linq like queries in the POST body?

One Answer

var query = '<View><Query><Where><Eq><FieldRef Name="Title" /><Value Type="Text">Country</Value></Eq></Where></Query></View>';

Your CAML query and end-point seems that your are trying to get some fields where Title equals Country. If it is your main requirement, then just make a GET request to the following end-point.

var restQueryUrl = String.format("{0}/_api/sp.appcontextsite(@target)/web/lists/getByTitle('{1}')/fields?$filter=Title eq 'Country'" +
        "&@target='{2}'", appWebUrl, listTitle, hostWebUrl);

<Where> can be replaced by the $filter

Answered by Atish Dipongkor - MVP on November 11, 2021

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