1

I'm trying hard to make work this code:

shptService.create_board = function (url, title, description, template) {
            //ensure form digest
            shptService.ensureFormDigest(function (fDigest) {

                $.ajax({
                    url: shptService.hostWebUrl+"/_api/web/webinfos/add",
                    type: "POST",
                    data: JSON.stringify(
                          {
                              'parameters': {
                                  '__metadata': { 'type': 'SP.WebInfoCreationInformation' },
                                  'Url': url,
                                  'Title': title,
                                  'Description': description,
                                  'Language': 1033,
                                  'WebTemplate': 'sts',
                                  'UseUniquePermissions': false
                              }
                          }
                      ),
                        headers: { 
                            "accept": "application/json; odata=verbose", 
                            "content-type":"application/json;odata=verbose",
                            "X-RequestDigest": fDigest
                    },
                    success: doSuccess,
                    error: doError
                });


            });
        };

i got from this source : https://msdn.microsoft.com/en-us/library/office/jj164022.aspx#bk_CreateSite but I'm having this error : 403 (Forbidden) .can anybody tell me why i'm getting this problem or a solution is better.

1 Answer 1

2

Forbidden Error Generally occours because of these two reasons.

  1. Your Form Digest value is not correct.
  2. The user you are running tests as doesnt have the requisite permissions.

To solve 1 - use $('#__REQUESTDIGEST').val() to get the form digest value. In case you are not getting/able to resolve the value, you may consider refreshing the form digest value by hitting the /contextinfo endpoint

var getFormDgst= function(url) {

  var req = {};
  req.url = url + "/_api/contextinfo";
  req.method = "POST";
  req.body = "";
  req.header = {
    "Accept": "application/json; odata=verbose"
  };
  req.success = function(data) {

    //get context info from data.
    $('#__REQUESTDIGEST').val(data.d.GetContextWebInformation.FormDigestValue)

  };
  req.failure = function(data, errorCode, errorMessage) {

  }
  $ajax(req);      
}

The case 2 is easier to solve - you just give the requisite permission :)

Source : Wiktor Wilan's Blog

9
  • it brings me to this new error : 500 (Internal Server Error) :(
    – Looytawon
    Commented Sep 17, 2015 at 1:27
  • Are you using sharepoint 2013 server or 0365 Commented Sep 17, 2015 at 6:00
  • I'm using O365 ..im currently developing sharepoint hosted app
    – Looytawon
    Commented Sep 17, 2015 at 6:11
  • log the error - in req.failure - add a statement, console.log(data) Open developer tools in browser, and in console, check the error details that get logged Commented Sep 17, 2015 at 7:14
  • "A sub site cannot be created in an app site.\" when is use url: shptService.appWebUrl+"/_api/web/webinfos/add" and when i use url: shptService.hostWebUrl+"/_api/web/webinfos/add" "readyState":4,"responseText":"","status":403,"statusText":"Forbidden"
    – Looytawon
    Commented Sep 17, 2015 at 8:09

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.