Guest User

Untitled

a guest
Feb 24th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. var outerObject = outerObject || {};
  2.  
  3. SP.SOD.executeFunc("sp.js");
  4.  
  5. //These functions taken from the internet
  6. function getUrlVars() {
  7. var vars = [], hash;
  8. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  9. for (var i = 0; i < hashes.length; i++) {
  10. hash = hashes[i].split('=');
  11. vars.push(hash[0]);
  12. vars[hash[0]] = hash[1];
  13. }
  14. return vars;
  15. };
  16.  
  17. function getUrlVar(name) {
  18. return getUrlVars()[name];
  19. };
  20.  
  21. var hostWebUrl = decodeURIComponent(getUrlVar('SPHostUrl'));
  22. var appWebUrl = decodeURIComponent(getUrlVar('SPAppWebUrl'));
  23.  
  24. jQuery.getScript(
  25. hostWebUrl + "/_layouts/15/SP.RequestExecutor.js",
  26. continueExecution
  27. );
  28.  
  29. function continueExecution() {
  30.  
  31. //Begin Create Site Portion
  32. outerObject.CreateSite = outerObject.CreateSite ||
  33. function CreateSite(event) {
  34.  
  35. SP.SOD.executeFunc("sp.js");
  36.  
  37. // Get App and Host context (don't be confused that the object name has the word 'app' in it)
  38. var appContext = SP.ClientContext.get_current();
  39. var hostContext = new SP.AppContextSite(appContext, hostWebUrl);
  40.  
  41. // Create variables, object, and properties for sub-site
  42. //debugger;
  43. var userSiteName = $("#userInputSiteName").val();
  44. var userSiteCode = $("#userInputSiteCode").val();
  45. var newSiteDetails = new SP.WebCreationInformation();
  46. newSiteDetails.set_title(userSiteName);
  47. newSiteDetails.set_url(userSiteCode);
  48. newSiteDetails.set_language(1033);
  49. newSiteDetails.set_webTemplate("CASESTUDYMONMORN#0");
  50.  
  51. // Create the Site URL
  52. var siteUrl = String.format(hostWebUrl + "/" + userSiteCode);
  53.  
  54. // Get the web site and site Collection from host site and add sub-site
  55. var website = hostContext.get_web();
  56. var webCollection = website.get_webs();
  57. webCollection.add(newSiteDetails);
  58.  
  59. // execute query
  60. $("#messageToUser").text("Working on it...");
  61. $("#messageToUserArea").css("color", "black");
  62. $("#messageToUserArea").css("display", "block");
  63. appContext.set_requestTimeout(10*60*100000);
  64. appContext.executeQueryAsync(successSubsiteCreation, failSubsiteCreation);
  65.  
  66. function successSubsiteCreation(sender, args) {
  67.  
  68. $("#messageToUser").html("Success creating project site '" + userSiteName + "' at <a href='" + siteUrl + "'>" + siteUrl + "</a>.");
  69. $("#messageToUserArea").css("color","green");
  70. $("#messageToUserArea").css("display", "block");
  71. }
  72.  
  73. function failSubsiteCreation(sender, args) {
  74.  
  75. $("#messageToUser").text("Error occurred while attempting to create site " + userSiteName + " at URL " + siteUrl + ". \n" + args.get_message() + "\n" + args.get_stackTrace() + "\n" + args.get_errorDetails());
  76. $("#messageToUserArea").css("color", "red");
  77. $("#messageToUserArea").css("display", "block");
  78. }
  79.  
  80. }
  81.  
  82. }
Add Comment
Please, Sign In to add comment