简体   繁体   English

如何在SharePoint 2013 Online中使用JavaScript动态创建母版页文件?

[英]How to create master page file dynamically using JavaScript in SharePoint 2013 Online?

I am creating one SharePoint App in that I want do following. 我正在创建一个SharePoint App,我想执行以下操作。

  1. In master page gallery, copy current master page and create new master page. 在母版页库中,复制当前母版页并创建新的母版页。 (as we do new SharePoint Designer manually.) (因为我们手动执行新的SharePoint Designer。)
  2. Inject JavaScript reference in new master page and save it. 在新的母版页中插入JavaScript参考并将其保存。
  3. Apply new master page as default master page. 将新的母版页应用为默认母版页。

All these should be done using JavaScript only. 所有这些都应仅使用JavaScript完成。

Does anybody know how to do ? 有人知道怎么做吗?

Thanks user988917 谢谢user988917

To get current master page, make a GET request to following end-point 要获取当前的母版页,请向以下端点发出GET请求

/_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. 我没有在JS中尝试过此方法,但通常有等效的方法,因此查看服务器端代码会有所帮助。 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 http://blog.vegaitsourcing.rs/2008/10/programmatically-uploading-master-pages.html这也是如何使用JS上传文件的示例(我相信仅适用于HTML5浏览器) 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. 棘手的事情将是文件操作(注入JS)...我看不出有一种纯粹的方法可以从JS做到这一点的好方法,您可能需要一些服务器端代码才能打开文件并注入新代码,保存后再上传回去。

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 arichterwork.blogspot.com/2008/03/programmatically-inherit-master-page.html

Eg web.MasterUrl = "/_layouts/custom.master" 例如web.MasterUrl = "/_layouts/custom.master"

Next the SharePoint JavaScript API reference on Core objects, the Web in this case: 接下来是在核心对象(在这种情况下为Web)上的SharePoint JavaScript API参考:

msdn.microsoft.com/EN-US/library/office/jj245288.aspx msdn.microsoft.com/EN-US/library/office/jj245288.aspx

You can see that both masterUrl and customMasterUrl are available R/W. 您可以看到masterUrl和customMasterUrl都是可用的R / W。

Finally, you'll want to do some basic file manipulation. 最后,您需要进行一些基本的文件操作。 Here are the MSDN resources: 这是MSDN资源:

msdn.microsoft.com/en-us/library/jj163201.aspx#BasicOps_FileTasks 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. 另一个风险更大的挑战是,您正在向页面中注入JS,而JS可能会被恶意使用。

If you planned to use this approach with an app for SharePoint, I'd consider finding an alternative approach. 如果您打算将此方法与SharePoint应用程序一起使用,我会考虑寻找另一种方法。 Script injection like this is not permitted. 不允许这样的脚本注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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