Booden = window.Booden || {};
Booden.CreateSitePart = window.Booden.CreateSitePart || function() {
this.target = "";
this.Url = "";
this.BaseUrl = _spPageContextInfo.webAbsoluteUrl;
this.Title = "";
this.ResultElement = "createResult";
this.LoadingElement = "waitForLoad";
this.ShowWait = function () {
var wait = Booden.jq("#" + this.target + " > ." + this.LoadingElement);
var parent = wait.parent();
wait.width(parent.width());
wait.height(parent.height());
wait.show();
};
this.ShowMessage = function (html, clear) {
if (clear)
Booden.jq("#" + this.target + " > ." + this.ResultElement).html("");
Booden.jq("
" + html + "
").appendTo(Booden.jq("#" + this.target + " > ." + this.ResultElement));
};
this.onCreationSuccess = function (sender, args) {
Booden.jq("#" + this.target + " > ." + this.LoadingElement).hide();
this.ShowMessage("The site '" + this.Title + "' was successfully created."
+ "
Url: " + this.BaseUrl + "/" + this.Url + "", true);
};
this.onCreationError = function (sender, args) {
Booden.jq("#" + this.target + " > ." + this.LoadingElement).hide();
var msg = args.get_message();
this.ShowMessage("The site '" + this.Title + "' could not be created.
" + msg, true);
};
this.Create = function () {
this.ShowMessage("", true);
var url, title, desc, temp;
var valUrl = false, valTitle = false, valTemp = false;
var el = Booden.jq("#" + this.target);
el.find("*").each(function () {
var e = Booden.jq(this);
var name = e.attr("name");
if (name != undefined) {
switch (name) {
case "title":
title = e.get(0).value;
break;
case "description":
desc = e.get(0).value;
break;
case "url":
url = e.get(0).value;
break;
case "type":
if (e.get(0).checked)
temp = e.get(0).value;
break;
}
}
});
if (title == undefined || title == "") {
this.ShowMessage("Please add a name", false);
}
else
valTitle = true;
if (url == undefined || url == "") {
this.ShowMessage("Please add a url", false);
}
else
valUrl = true;
if (temp == undefined || temp == "") {
this.ShowMessage("Please select a type", false);
}
else
valTemp = true;
if (valTitle && valUrl && valTemp)
this.CreateWithParams(url, title, desc, temp);
};
this.CreateWithParams = function (url, title, description, webtemplate) {
this.ShowWait();
this.Url = url;
this.Title = title;
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
clientContext.load(web);
//set creation info
var WCI = new SP.WebCreationInformation();
WCI.set_webTemplate(webtemplate);
WCI.set_description(description);
WCI.set_title(title);
WCI.set_url(url);
web.get_webs().add(WCI);
web.update();
//save changes and generate callbacks
clientContext.load(web);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onCreationSuccess),
Function.createDelegate(this, this.onCreationError));
}
this.Render = function (el) {
this.target = el.attr("id");
Booden.jq(" Please wait
").appendTo(el);
Booden.jq("").appendTo(el);
Booden.jq("").appendTo(el);
Booden.jq("").appendTo(el);
Booden.jq("").appendTo(el);
Booden.jq("Site type:
").appendTo(el);
var btn = Booden.jq("");
var me = this;
btn.on("click", function () { me.Create() });
btn.appendTo(el);
}
};