'use strict';
window.VCA = window.VCA || {};
window.VCA.HostWebApp = function () {
var hostWebUrl,
appWebUrl,
hostWebContext,
destinationServerRelativeUrl,
destinationFileName,
createInfo,
newFile,
newFileID,
// locate a file in the app web and retrieve the contents. If successful, provision to host web..
readFromAppWebAndProvisionToHost = function (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);
});
},
// utility method for uploading files to host web..
uploadFileToHostWebViaCSOM = function (serverRelativeUrl, filename, contents) {
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();
hostWebContext.load(files);
newFile = files.add(createInfo);
hostWebContext.load(newFile, 'ListItemAllFields');
hostWebContext.executeQueryAsync(onProvisionFileSuccess, onProvisionFileFail);
},
onProvisionFileSuccess = function () {
$('#message').append('<br /><div>File provisioned in host web successfully: ' + destinationServerRelativeUrl + '/' + destinationFileName + '</div>');
newFileID = newFile.get_listItemAllFields().get_id();
setContentTypeEtPublishing('/' + destinationServerRelativeUrl + '/' + destinationFileName);
},
onProvisionFileFail = function (sender, args) {
alert('Failed to provision file into host web. Error:' + sender.statusCode);
},
// set content type et publishing
setContentTypeEtPublishing = function (masterUrl) {
var hostWeb = hostWebContext.get_web();
var oList = hostWeb.get_lists().getByTitle('Master Page Gallery');
var oMastreItem = oList.getItemById(newFileID); //test remplace avec query
//389
oMastreItem.set_item('ows_ContentType', 'Html Master Page');
oMastreItem.update();
var oMasterFile = oMastreItem.get_file();
oMasterFile.publish("Published by app");
hostWebContext.executeQueryAsync(onsetContentTypeEtPublishingSuccess, onProvisionFileFail);
},
onsetContentTypeEtPublishingSuccess = function () {
$('#message').append('<br /><div>Master page content type edited et page published successfully: ' + destinationServerRelativeUrl + '/' + destinationFileName + '</div>');
setMaster('/' + destinationServerRelativeUrl + '/' + destinationFileName);
},
onProvisionFileFail = function (sender, args) {
alert('Failed to provision file into host web. Error:' + sender.statusCode);
},
// set master page on host web..
setMaster = function (masterUrl) {
var hostWeb = hostWebContext.get_web();
var siteRerlativeMasterUrl = _spPageContextInfo.siteServerRelativeUrl + masterUrl;
hostWeb.set_customMasterUrl(siteRerlativeMasterUrl);
hostWeb.set_masterUrl(siteRerlativeMasterUrl);
hostWeb.update();
hostWebContext.load(hostWeb);
hostWebContext.executeQueryAsync(onSetMasterSuccess, onSetMasterFail);
},
onSetMasterSuccess = function () {
$('#message').append('<br /><div>Master page updated successfully..</div>');
},
onSetMasterFail = function (sender, args) {
alert('Failed to update master page on host web. Error:' + args.get_message());
},
init = function () {
var hostWebUrlFromQS = $.getUrlVar("SPHostUrl");
hostWebUrl = (hostWebUrlFromQS !== undefined) ? decodeURIComponent(hostWebUrlFromQS) : undefined;
var appWebUrlFromQS = $.getUrlVar("SPAppWebUrl");
appWebUrl = (appWebUrlFromQS !== undefined) ? decodeURIComponent(appWebUrlFromQS) : undefined;
}
return {
execute: function () {
init();
hostWebContext = new SP.ClientContext(window.VCA.appHelper.getRelativeUrlFromAbsolute(hostWebUrl));
readFromAppWebAndProvisionToHost(appWebUrl + '/MasterPages/MasterPageProvisionedByApp.txt', '_catalogs/masterpage', 'ProvisionedByApp.master');
}
}
}();
window.VCA.AppHelper = {
getRelativeUrlFromAbsolute: function (absoluteUrl) {
absoluteUrl = absoluteUrl.replace('https://', '');
if (absoluteUrl == absoluteUrl)
absoluteUrl = absoluteUrl.replace('http://', '');
var parts = absoluteUrl.split('/');
var relativeUrl = '/';
for (var i = 1; i < parts.length; i++) {
relativeUrl += parts[i] + '/';
}
return relativeUrl;
},
};
//$(document).ready(function () {
// window.VCA.HostWebApp.execute();
//});
Version corrigée de
http://www.sharepointnutsandbolts.com/2013/05/sp2013-host-web-apps-provisioning-files.html
CHRIS O'BRIEN
No comments:
Post a Comment