1

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?

  • You just want to get some fields where Title equals Country? – Atish Dipongkor - MVP Nov 2 '15 at 16:01
0
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

  • For the demonstration purposes , yes i want to get the SpField with the Title "Country". But in a real scenario multiply this by 20, and then specify the properties you want to retrieve, you will soon notice that the URL is getting too long, hard to construct and maintain and even might reach the Sharepoint's url limit (still not sure if is 256 or 4096). Thanks. – Ciprian Nov 3 '15 at 9:39

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.