Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
Web Property:
__PageLayouts
Web Property Value:
<pagelayouts>
<layout guid="e87b7c1d-4a23-4ca1-8fb8-36314cabb7c4" url="_catalogs/masterpage/NewsArticlePage.aspx" />
</pagelayouts>
*/
var setAvailablePageLayouts = function (availablePageLayoutFilenames, onSuccess, onFailure) {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
if (jQuery.isArray(availablePageLayoutFilenames) && availablePageLayoutFilenames.length > 0) {
var gallery = ctx.get_site().get_rootWeb().get_lists().getByTitle("Master Page Gallery");
getFilesByName(gallery, availablePageLayoutFilenames, availablePageLayoutFilenames.length, function (listItems) {
var lis = CC.CORE.Utilities.CastSPCollectionToArray(listItems);
if (lis.length < 1) {
onFailure("Found " + lis.length + " page layouts with filenames: " + availablePageLayoutFilenames.join(", "));
}
else {
var xmlEl = jQuery("<pagelayouts></pagelayouts>");
_.each(lis, function (li) {
xmlEl.append(getLayoutXml(li));
});
updatePropertyBag(web, "__PageLayouts", xmlEl[0].outerHTML, onSuccess, onFailure);
}
},
onFailure);
}
else {
updatePropertyBag(web, "__PageLayouts", "__Inherit", onSuccess, onFailure);
}
};
/*
Web Property:
__DefaultPageLayout
Web Property Value:
<layout guid="e87b7c1d-4a23-4ca1-8fb8-36314cabb7c4" url="_catalogs/masterpage/NewsArticlePage.aspx" />
*/
var setDefaultPageLayout = function (defaultPageLayoutFilename, onSuccess, onFailure) {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
if (typeof defaultPageLayoutFilename === "string" && defaultPageLayoutFilename.length > 0) {
var gallery = ctx.get_site().get_rootWeb().get_lists().getByTitle("Master Page Gallery");
getFilesByName(gallery, [defaultPageLayoutFilename], 1, function (listItems) {
var lis = CC.CORE.Utilities.CastSPCollectionToArray(listItems);
if (lis.length !== 1) {
onFailure("Found " + lis.length+ " page layouts with filename: " + defaultPageLayoutFilename);
}
else {
var li = lis[0];
var xml = getLayoutXml(li);
updatePropertyBag(web, "__DefaultPageLayout", xml, onSuccess, onFailure);
}
},
onFailure);
}
else {
updatePropertyBag(web, "__DefaultPageLayout", "__Inherit", onSuccess, onFailure);
}
};
/*
Web Property:
__WebTemplates
Web Property Value:
<webtemplates>
<lcid id="1033">
<webtemplate name="{BDACFFF5-05DF-4446-9907-B4C39F15F1D7}#WT_NewsHubSite" />
</lcid>
<lcid id="all">
<webtemplate name="{BDACFFF5-05DF-4446-9907-B4C39F15F1D7}#WT_NewsHubSite" />
</lcid>
</webtemplates>
*/
var setAvailableWebTemplates = function (availableWebTemplates, onSuccess, onFailure) {
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var xml = "";
var inheritWebTemplates = "False";
if (jQuery.isArray(availableWebTemplates) && availableWebTemplates.length > 0) {
var xmlEl = jQuery("<webtemplates><lcid id='all'></lcid></webtemplates>");
var lcidAll = xmlEl.find("lcid");
_.each(availableWebTemplates, function (availableWebTemplate) {
var wtEl = jQuery("<webtemplate />").attr("name", availableWebTemplate);
lcidAll.append(wtEl);
});
xml = xmlEl[0].outerHTML;
}
else {
xml = "";
inheritWebTemplates = "True";
}
updatePropertyBag(web, "__WebTemplates", xml, function () {
updatePropertyBag(web, "__InheritWebTemplates", inheritWebTemplates, onSuccess, onFailure);
}, onFailure);
};
/*
Utility methods to support the above
*/
var getLayoutXml = function (li) {
var xml = "<layout guid='" + li.get_item("UniqueId") + "' url='" + li.get_item("FileRef") + "' />";
return xml;
};
var getFilesByName = function (list, filenames, rowLimit, onSuccess, onFailure) {
var camlQuery = new SP.CamlQuery();
var camlQueryXml = getViewXml("FileLeafRef", "Text", filenames, rowLimit);
camlQuery.set_viewXml(camlQueryXml);
var collListItem = list.getItems(camlQuery);
ctx.load(collListItem, "Include(UniqueId, FileRef)");
ctx.executeQueryAsync(function () {
onSuccess(collListItem);
},
onFailure);
};
var getViewXml = function (fieldName, fieldType, fieldValues, rowLimit) {
var caml = "<View><Query><Where><In><FieldRef Name='" + fieldName + "' /><Values>";
_.each(fieldValues, function (fieldValue) {
caml += "<Value Type='Text'>" + fieldValue + "</Value>";
});
caml += "</Values></In></Where></Query><RowLimit>" + rowLimit + "</RowLimit></View>";
return caml;
};
var updatePropertyBag = function (propertyBagWeb, key, value, successCallback, failedCallback) {
var ctx = SP.ClientContext.get_current();
var properties = propertyBagWeb.get_allProperties();
properties.set_item(key, value);
propertyBagWeb.update();
ctx.load(propertyBagWeb);
ctx.load(properties);
ctx.executeQueryAsync(successCallback, failedCallback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.