简体   繁体   中英

Creating SharePoint Site with JavaScript

I am attempting to use JSOM to create a sub-site (web) on button-click. The function is working - in so far as a site is created with the given configuration. However, despite the fact that a site is indeed being create, the ExecuteQueryAsync call always routes to the failure function. The relevant code is below:

 var ctx = SP.ClientContext.get_current();
 var web = ctx.get_web();

 var webCreationInfo = new SP.WebCreationInformation();
 webCreationInfo.set_title(portalName); //note: this variable is defined elsewhere
 webCreationInfo.set_description('');
 webCreationInfo.set_language(1033);
 webCreationInfo.set_url(webUrl); //note: this variable is defined elsewhere
 webCreationInfo.set_useSamePermissionsAsParentSite(false);
 webCreationInfo.set_webTemplate(templateGuid); //note: this variable is defined elsewhere

 web.get_webs().add(webCreationInfo);
 web.update();

 ctx.load(web);                     
 ctx.executeQueryAsync(
      function() {
           alert("Created Site");
      },
      function(sender, args) {
           alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
      }
 );

This includes a couple variables that are defined elsewhere in my code, but this should not be a part of my problem. The site is properly created, using the correct template, name, and URL.

The 'Request failed' alert is always what pops up for me, despite the site being created correctly. The value of args.get_message() is "Unexpected response from server" and the value of args.get_stackTrace() is 'null'.

The issue here was actually due to the button and not the above code. I needed to add 'event.preventDefault();' to the start of my onClick function. Otherwise, the page attempts to refresh - which happens much faster than the site can be provisioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM