0

I need to get data from SharePoint online list by using rest api. I will use the GET and POST method (post method in camel query case).

urlapp: the encoded url such as https://name-29a8f2a9e3342d....

$.ajax({
   url: urlapp + "/../_api/web/lists/getbytitle('Letters')",
    method: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
        "content-type": "application/json;odata=verbose",  
    },
    async: false,
    success: function (data) {
        console.log(data);//Debug statement                 
    },
    error: function (error) {
        console.log(error);
    }
});

the previous code generate the 403 (Forbidden) error.

After searching I found solution by using executeAsyn (new SP.RequestExecutor), but I need some way is sync execution not async.

3
  • Is this a 365 tenant farm or a 365 dedicated farm? Apr 10, 2017 at 9:08
  • Navigate to url value. If it returns any results, then you shouldn't be getting any errors.
    – Ashik Paul
    Sep 24, 2018 at 7:02
  • I dont think you need sync code, you can do the next operations in the async success callback.
    – Mikel
    Sep 24, 2018 at 10:22

3 Answers 3

0

You can try .done() method to achieve your synchronous effect on your calls.

For Example:

function AjaxCall(url) {
var call = $.ajax({
    url: url,
    type: "GET",
    headers: {
        "accept": "application/json;odata=verbose",
    },
    error: function(error) {
        console.log(JSON.stringify(error));
    }
});
return call;
}
$(document).ready(function() {
var firstCall = AjaxCall(_spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Letters')/items");
firstCall.done(function(data, textStatus, jqXHR) {
    $.each(data.d.results, function(i, val) {
       //doSomething
    });
    var secondCall = AjaxCall(_spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('newList')/items");
    secondCall.done(function(data, textStatus, jqXHR) {
        $.each(data.d.results, function(i, val) {
           //doSomething
        });
    });
});

This will help you achieve synchronous effect with asynchronous calls..

2
  • thank you for response but if i put any code after done calling will execute before finish the done calling and I can not insert all the code inside done
    – myomyo
    Apr 10, 2017 at 11:16
  • You can write function and call that function inside done Apr 11, 2017 at 6:37
0

Please try below code.

 var executor = new SP.RequestExecutor('AppWebURL');
     executor.executeAsync(
                {
                    url: "AppWebURL" + "/_api/SP.AppContextSite(@target)/Web/Lists/GetByTitle('Letters')/Items?@target='HostWebURL'",
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: function (data) {
                        console.log('success');
                    },
                    error: function () {
                        console.log("error");
                    }
                });

Hope it works for you.

2
  • Sorry for that but this solution is async and I have to use the rest api with online sharepoint
    – myomyo
    Apr 11, 2017 at 5:37
  • It is not possible to do sync call while access data from host web from app web. Apr 11, 2017 at 6:05
0

You could use jQuery Deffered for this.

Sample Script:

<script type="text/javascript">
    function testCreateFolder() {
        CreateFoldersbyparent("abc", "MyDoc/").done(function () {
            CreateFoldersbyparent("test", "MyDoc/abc/");
        })
    }

    function CreateFoldersbyparent(name, parent, callback) {
        var deferred = $.Deferred();
        // replace below with your logic
        var documentLibraryName = "MyDoc";
        var fileCollectionEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfolderbyserverrelativeurl('" + documentLibraryName + "')/folders/add('" + parent + name + "')";
        $.ajax({
            url: fileCollectionEndpoint,
            type: "POST",
            async: false,
            headers: {
                "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                "accept": "application/json;odata=verbose",
                "content-type": "application/json;odata=verbose"
            },
            success: function (data) {
                console.log("done");
                deferred.resolve(data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                //handle error...
                console.log(jqXHR);
            }
        });
        return deferred.promise();
    }
</script>

Your Answer

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

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