3

I am successful in getting data from host web to app web but the problem i am facing with is querying the API.

This is i am trying to do but getting error..

    var getTaskByUser = function (userID) {
            var hostweburl =
                decodeURIComponent(
                    getQueryStringParameter("SPHostUrl")
            );
           var  appweburl =
                decodeURIComponent(
                    getQueryStringParameter("SPAppWebUrl")
            );
            var scriptbase = hostweburl + "/_layouts/15/";

            // Load the js files and continue to the successHandler
            $.getScript(scriptbase + "SP.RequestExecutor.js", execCrossDomainRequest);
            function execCrossDomainRequest() {
            // executor: The RequestExecutor object
            // Initialize the RequestExecutor with the add-in web URL.
            var executor = new SP.RequestExecutor(appweburl);
            executor.executeAsync(
    {
        url:
            appweburl +
            "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('Asana2')/Items?$select=Title,Category?@target='" +
            hostweburl + "'",
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: successHandler,
        error: errorHandler
    }
);

ERROR:

 GET https://niisar-3b7c10c2234504.sharepoint.com/Development/SharePointApp1/_ap…ect=Title,Category?@target=%27https://niisar.sharepoint.com/Development%27 
400 (Bad Request)

"{"error":{"code":"-1, Microsoft.SharePoint.Client.InvalidClientQueryException","message":{"lang":"en-US","value":"The expression \"Title,Category?@target='https://niisar.sharepoint.com/Development'\" is not valid."}}}"

Need help. I am new to sharepoint

1
  • What's your error? sharepoint hosted or provider hosted app? Commented Oct 15, 2015 at 4:55

1 Answer 1

4
appweburl + "/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('Asana2')/Items?$select=Title,Category?@target='" + hostweburl + "'"

It seems your URL is not valid where you are requesting to. Try following URL

var url = appweburl +
"/_api/SP.AppContextSite(@target)/web/lists/GetByTitle('Asana2')/Items?$select=Title,Category&@target='" +
hostweburl + "'";

The problem is you have used ? after $select but it will be &.

Remember few things while constructing rest end-point.

  • Main part of URL should be ended up with ?
  • Rest of the parts will be joined with & like @target, $select, $filter and other query operators.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.