I am creating one SharePoint App in that I want do following.
All these should be done using JavaScript only.
Does anybody know how to do ?
Thanks user988917
To get current master page, make a GET request to following end-point
/_api/Web?$select=CustomMasterUrl,MasterUrl
To change current master page,
function MastePageChange() {
var clientcontext;
var cweb;
var customMasterURL = '/_catalogs/masterpage/NewMasterPage.master';
clientcontext = new SP.ClientContext.get_current();
cweb = clientcontext.get_web();
cweb.set_customMasterUrl(masterPageUrl);
cweb.set_masterUrl(masterPageUrl);
cweb.update();
clientcontext.executeQueryAsync(function() {
alert("Master Page has been changed successfully \n" + customMasterURL);
}, function(sender, args) {
alert("Error: " + args.get_message());
});
}
I haven't tried this in JS but usually there are equivalent methods so looking at sever side code helps. http://blog.vegaitsourcing.rs/2008/10/programmatically-uploading-master-pages.html Here is also a sample of how to upload files using JS (only works with HTML5 browsers I believe) http://msdn.microsoft.com/en-us/library/jj163201.aspx
The tricky thing is going to be the file manipulation (injecting the JS)... I don't see a good way to do that purely from JS, you will likely need some server side code to crack open the file and inject new code, save it and then upload it back.
To modify Masterpages, I'd recommend taking a look at these two resources.
First, a blog post that explains how to apply master pages:
arichterwork.blogspot.com/2008/03/programmatically-inherit-master-page.html
Eg web.MasterUrl = "/_layouts/custom.master"
Next the SharePoint JavaScript API reference on Core objects, the Web in this case:
msdn.microsoft.com/EN-US/library/office/jj245288.aspx
You can see that both masterUrl and customMasterUrl are available R/W.
Finally, you'll want to do some basic file manipulation. Here are the MSDN resources:
msdn.microsoft.com/en-us/library/jj163201.aspx#BasicOps_FileTasks
There are two more challenges. The first is about permissions: updating the masterpage gallery is a high privilege operation. The other, riskier, challenge here is that you're injecting JS into pages, and JS can be used maliciously.
If you planned to use this approach with an app for SharePoint, I'd consider finding an alternative approach. Script injection like this is not permitted.
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.