Introduction
In this article, we will see how files can be provisioned to the host Web from an app (SharePoint hosted app in this case). A custom master page will be provisioned to the Host Web and the master page will be setup as the default Master page.
Please follow the steps, mentioned below.
Steps
- Open Visual Studio and create a new SharePoint app project.
- Select the Host for the app as “SharePoint-Hosted”.
- Once the project is created, you will find the structure, mentioned below.
- Add a module to the project, named Masterpages.
- Inside the module, you will see Sample.txt. Rename the file to customMasterpage.txt.
- Copy the code of your Custom Master Page to this file customMasterpage.txt (You can take a copy of the seattle.master from SharePoint Designer).
- Add the code, mentioned below, to App.js file.
- 'use strict';
-
- var hostWebUrl;
- var appWebUrl;
- var hostWebContext;
- var destinationServerRelativeUrl;
- var destinationFileName;
- var appWebContext;
-
-
- $(document).ready(function () {
- hostWebUrl = decodeURIComponent($.getUrlVar("SPHostUrl"));
- appWebUrl = decodeURIComponent($.getUrlVar("SPAppWebUrl"));
-
- appWebContext = new SP.ClientContext.get_current();
-
- hostWebContext = new SP.AppContextSite(appWebContext, hostWebUrl);
-
- readFromAppWebAndProvisionToHost(appWebUrl + '/Masterpages/customMasterpage.txt', '_catalogs/masterpage', customMasterpage.master');
-
- });
-
-
- function readFromAppWebAndProvisionToHost(appPageUrl, hostWebServerRelativeUrl, hostWebFileName) {
-
- destinationServerRelativeUrl = hostWebServerRelativeUrl;
- destinationFileName = hostWebFileName;
-
- var req = $.ajax({
- url: appPageUrl,
- type: "GET",
- cache: false
- }).done(function (fileContents) {
- if (fileContents != undefined && fileContents.length > 0) {
- uploadFileToHostWebViaCSOM(destinationServerRelativeUrl, destinationFileName, fileContents);
- }
- else {
- alert('Failed to read file from app web, so not uploading to host web..');
- }
- }).fail(function (jqXHR, textStatus) {
- alert("Request for page in app web failed: " + textStatus);
- });
- }
-
- function uploadFileToHostWebViaCSOM(serverRelativeUrl, filename, contents) {
-
- var createInfo = new SP.FileCreationInformation();
- createInfo.set_content(new SP.Base64EncodedByteArray());
- for (var i = 0; i < contents.length; i++) {
- createInfo.get_content().append(contents.charCodeAt(i));
- }
-
- createInfo.set_overwrite(true);
- createInfo.set_url(filename);
- var files = hostWebContext.get_web().getFolderByServerRelativeUrl(serverRelativeUrl).get_files();
- appWebContext.load(files);
- files.add(createInfo);
-
- appWebContext.executeQueryAsync(
- Function.createDelegate(this, function () {
- $('#message').append('<br /><div>File provisioned in host web successfully: ' + serverRelativeUrl + '/' + filename + '</div>');
- setMaster('/' + serverRelativeUrl + '/' + filename);
- }),
- Function.createDelegate(this, function (sender, args) {
- alert('Failed to provision file into host web. Error:' + args.get_message());
- }));
-
- }
-
-
- function setMaster(masterUrl) {
- var hostWeb = hostWebContext.get_web();
- hostWeb.set_masterUrl(masterUrl);
- hostWeb.update();
-
- appWebContext.load(hostWeb);
- appWebContext.executeQueryAsync(
- Function.createDelegate(this, function(){
- $('#message').append('<br /><div>Master page updated successfully..</div>');
- }),
- Function.createDelegate(this, function (sender, args) {
- alert('Failed to update master page on host web. Error:' + args.get_message());
- }));
- }
-
-
-
- jQuery.extend({
- getUrlVars: function () {
- var vars = [], hash;
- var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
- for (var i = 0; i < hashes.length; i++) {
- hash = hashes[i].split('=');
- vars.push(hash[0]);
- vars[hash[0]] = hash[1];
- }
- return vars;
- },
- getUrlVar: function (name) {
- return jQuery.getUrlVars()[name];
- }
- });
- Go to AppManifest.xml -Permissions tab. We need to give the permission to the app to access the Host Web. Select “SiteCollection” Permission Level to ”Full Control”.
- Deploy the solution and trust the app.
- The Custom Master page will be uploaded and set to the site.