1

I'm trying to create a list using REST API, the javascript is in a Sharepoint Page, I'm logged as a System Account and every time I run the script I get the following error:

{"error":{"code":"-1, Microsoft.SharePoint.SPException","message":{"lang":"pt-BR","value":"N\u00e3o h\u00e1 contexto do aplicativo para executar esta solicita\u00e7\u00e3o."}}}

Translating to english it would be:

There is no app context to execute this request.

My code to create the list is:

var params = "{ '__metadata': { 'type': '"+type+"' },'AllowContentTypes': true,'ContentTypesEnabled': true, 'BaseTemplate': 100,'Description': '" + list + "', 'Title':'" + listName + "'}";

var Url = _spPageContextInfo.webAbsoluteUrl+"/_api/SP.AppContextSite(@target)/web/lists?@target='"+_spPageContextInfo.webAbsoluteUrl+"'";

var formDigest = $('#__REQUESTDIGEST').val(); 

$.ajax({
   url: Url,
        method: 'POST', 
        dataType: "json",
        contentType: 'application/json',
        headers: {
            Accept: "application/json;odata=verbose",
             "X-RequestDigest": formDigest
        },
        data: JSON.stringify(params),
        cache: true,
        error: function (data) {
            console.log("Error");
        },
        success: callback
    });
  • Have you tried to create list from App web to Host web? – Samir Khimani Mar 21 '17 at 5:00
1

Below is the full code to create a SharePoint list which you can put inside a Script editor web part:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    $(function() {
        $("#btnClick").click(function(){
            var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists";
            $.ajax({
                url: requestUri,
                type: "POST",
                data:JSON.stringify({'__metadata': { 'type': 'SP.List' }, 'AllowContentTypes': true,'BaseTemplate': 100, 'ContentTypesEnabled': true, 'Description': 'My list description', 'Title': 'My Test List REST' }),
                headers: {
                    "accept":"application/json;odata=verbose",
                    "content-type": "application/json;odata=verbose",
                    "X-RequestDigest":$("#_REQUESTDIGEST").val()
                },
                success: onSuccess,
                error: onError
            });

            function onSuccess(data) {
                alert(data+ ' List Created');
            }

            function onError(error) {
                alert(JSON.stringify(error));
            }

        });
    });
</script>
<input type="button" id="btnClick" value="Click to Create List"/>
0

If you are on a page using this kind of domain-scheme business-e4b1dcb4676528.sharepoint.com then you are in the add-in environment and you need to use SP.AppContextSite(@target) in order to change the host-web.

If you are on your regular domain business.sharepoint.com then you can omit the SP.AppContextSite(@target) part.

Mixing these up could most likely result in such error.

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.