Instantly share code, notes, and snippets.
Last active
August 25, 2017 04:51
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(1)
1
You must be signed in to fork a gist
-
Save ludo6577/c59c14020194f1613a12 to your computer and use it in GitHub Desktop.
Send files to Sharepoint Online Host (Addfile, Set MasterPage, EditFile) http://www.sharepointnutsandbolts.com/2013/05/sp2013-host-web-apps-provisioning-files.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
window.COB = window.COB || {}; | |
window.COB.HostWebApp = function() { | |
var hostWebUrl, appWebUrl, hostWebContext, errorOccured=false; | |
/* | |
* Init hostWebUrl and hostWebContext | |
*/ | |
function init() { | |
var hostWebUrlFromQS = $.getUrlVar("SPHostUrl"); | |
hostWebUrl = (hostWebUrlFromQS !== undefined) ? decodeURIComponent(hostWebUrlFromQS) : undefined; | |
var appWebUrlFromQS = $.getUrlVar("SPAppWebUrl"); | |
appWebUrl = (appWebUrlFromQS !== undefined) ? decodeURIComponent(appWebUrlFromQS) : undefined; | |
hostWebContext = new SP.ClientContext(window.COB.appHelper.getRelativeUrlFromAbsolute(hostWebUrl)); | |
} | |
/* | |
* locate a file in the app web and retrieve the contents. If successful, provision to host web.. | |
*/ | |
function readFromAppWebAndProvisionToHost(appPageUrl, hostWebServerRelativeUrl, hostWebFileName, isMasterPage) { | |
isMasterPage = typeof isMasterPage !== 'undefined' ? isMasterPage : false; //By defaut is false | |
$.ajax({ | |
url: appPageUrl, | |
type: "GET", | |
cache: false | |
}).done(function (fileContents) { | |
if (fileContents !== undefined && fileContents.length > 0) { | |
$('#message').append('<br />File extracted from App successfully: ' + appPageUrl); | |
uploadFileToHostWebViaCSOM(hostWebServerRelativeUrl, hostWebFileName, fileContents, isMasterPage); | |
} | |
else { | |
errorOccured = true; | |
alert('Failed to read file ' + appPageUrl + ' from app web, so not uploading to host web..'); | |
} | |
}).fail(function (jqXHR, textStatus) { | |
errorOccured = true; | |
alert("Failed request to read file in App failed: " + appPageUrl); | |
}); | |
} | |
/* | |
* utility method for uploading files to host web.. | |
*/ | |
function uploadFileToHostWebViaCSOM(hostWebServerRelativeUrl, hostWebFileName, contents, isMasterPage) { | |
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(hostWebFileName); | |
var files = hostWebContext.get_web().getFolderByServerRelativeUrl(hostWebServerRelativeUrl).get_files(); | |
hostWebContext.load(files); | |
files.add(createInfo); | |
hostWebContext.executeQueryAsync( | |
function onProvisionFileSuccess() { | |
$('#message').append('<br />File provisioned in host web successfully: ' + hostWebServerRelativeUrl + '/' + hostWebFileName); | |
if(isMasterPage && !errorOccured) | |
setMaster(hostWebServerRelativeUrl + '/' + hostWebFileName); | |
}, | |
function onProvisionFileFail(sender, args) { | |
errorOccured = true; | |
alert('Failed to provision file into host web. Error:' + sender.statusCode); | |
}); | |
} | |
function createConfigFile(hostWebServerRelativeUrl, hostWebFileName) { | |
var createInfo = new SP.FileCreationInformation(); | |
createInfo.set_content(new SP.Base64EncodedByteArray()); | |
var contents = $("#AppID").val(); | |
for (var i = 0; i < contents.length; i++) { | |
createInfo.get_content().append(contents.charCodeAt(i)); | |
} | |
createInfo.set_overwrite(true); | |
createInfo.set_url(hostWebFileName); | |
var files = hostWebContext.get_web().getFolderByServerRelativeUrl(hostWebServerRelativeUrl).get_files(); | |
hostWebContext.load(files); | |
files.add(createInfo); | |
hostWebContext.executeQueryAsync( | |
function onProvisionFileSuccess() { | |
$('#message').append('<br />File provisioned in host web successfully: ' + hostWebServerRelativeUrl + '/' + hostWebFileName); | |
}, | |
function onProvisionFileFail(sender, args) { | |
errorOccured = true; | |
alert('Failed to provision file into host web. Error:' + sender.statusCode); | |
}); | |
} | |
/* | |
* set master page on host web.. | |
* TODO: modifier le master et pas le remplacer | |
*/ | |
function setMaster(masterUrl) { | |
var hostWeb = hostWebContext.get_web(); | |
var relativeURL = window.COB.AppHelper.getRelativeUrlFromAbsolute(hostWebUrl); | |
hostWeb.set_masterUrl(relativeURL + masterUrl); | |
hostWeb.update(); | |
hostWebContext.load(hostWeb); | |
hostWebContext.executeQueryAsync( | |
function onSetMasterSuccess() { | |
$('#message').append('<br />Master page updated successfully: ' + masterUrl); | |
}, | |
function onSetMasterFail(sender, args) { | |
errorOccured = true; | |
alert('Failed to update master page on host web. Error:' + args.get_message()); | |
}); | |
} | |
return { | |
execute: function () { | |
init(); | |
//createConfigFile(appWebUrl + '/Files', 'fb-config.txt'); | |
readFromAppWebAndProvisionToHost(appWebUrl + '/Files/fb-masterScript.txt', '_catalogs/masterpage', 'fb-masterScript.js'); | |
//readFromAppWebAndProvisionToHost(appWebUrl + '/Files/fb-masterPage.txt', '_catalogs/masterpage', 'fb-masterPage.master', true); | |
} | |
} | |
}(); | |
window.COB.AppHelper = { | |
getRelativeUrlFromAbsolute: function (absoluteUrl) { | |
absoluteUrl = absoluteUrl.replace('https://', ''); | |
var parts = absoluteUrl.split('/'); | |
var relativeUrl = '/'; | |
for (var i = 1; i < parts.length; i++) { | |
relativeUrl += parts[i] + '/'; | |
} | |
return relativeUrl; | |
}, | |
}; | |
//$(document).ready(function () { | |
// window.COB.HostWebApp.execute(); | |
//}); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hostweburl; | |
var appweburl; | |
var hostCookieName = "FacebookShare_Host"; | |
var appCookieName = "FacebookShare_App"; | |
window.COB = window.COB || {}; | |
window.COB.cookieHandler = { | |
setCookie: function (cookieName, cookieValue, cookieLifetimeDays, cookiePath) { | |
var expiryDate = new Date(); | |
expiryDate.setDate(expiryDate.getDate() + cookieLifetimeDays); | |
var cookieContents = escape(cookieValue) + ((cookieLifetimeDays === null) ? "" : "; expires=" + expiryDate.toUTCString()) + ((cookiePath === null) ? "" : "; path=" + cookiePath); | |
document.cookie = cookieName + "=" + cookieContents; | |
}, | |
getCookie: function (cookieName) { | |
var i, x, y, currentCookies = document.cookie.split(";"); | |
for (i = 0; i < currentCookies.length; i++) { | |
x = currentCookies[i].substr(0, currentCookies[i].indexOf("=")); | |
y = currentCookies[i].substr(currentCookies[i].indexOf("=") + 1); | |
x = x.replace(/^\s+|\s+$/g, ""); | |
if (x === cookieName) { | |
return unescape(y); | |
} | |
} | |
}, | |
ensureAppUrls: function () { | |
if (hostweburl === undefined) { | |
hostweburl = COB.cookieHandler.getCookie(hostCookieName); | |
} | |
if (appweburl === undefined) { | |
appweburl = COB.cookieHandler.getCookie(appCookieName); | |
} | |
} | |
}; | |
window.COB.appHelper = { | |
getRelativeUrlFromAbsolute: function (absoluteUrl) { | |
absoluteUrl = absoluteUrl.replace('https://', ''); | |
var parts = absoluteUrl.split('/'); | |
var relativeUrl = '/'; | |
for (var i = 1; i < parts.length; i++) { | |
relativeUrl += parts[i] + '/'; | |
} | |
return relativeUrl; | |
}, | |
}; | |
function sharePointReady() { | |
// retrieve passed app web/host web URLs and use session cookie to deal with SPHostUrl issue.. | |
var hostWebUrlFromQS = $.getUrlVar("SPHostUrl"); | |
hostweburl = (hostWebUrlFromQS !== undefined) ? decodeURIComponent(hostWebUrlFromQS) : undefined; | |
var appWebUrlFromQS = $.getUrlVar("SPAppWebUrl"); | |
appweburl = (appWebUrlFromQS !== undefined) ? decodeURIComponent(appWebUrlFromQS) : undefined; | |
// _spPageContextInfo is not present if code is running in an app part.. | |
if (_spPageContextInfo !== undefined) { | |
// if no appweburl was passed, use _spPageContextInfo.. | |
if (appweburl === undefined) { | |
appweburl = _spPageContextInfo.webAbsoluteUrl; | |
} | |
if ((document.referrer !== "") && (!document.referrer.toLowerCase().startsWith(appweburl.toLowerCase()) && (hostWebUrlFromQS))) { | |
// we came from outside app web and are being passed SPHostUrl, so this should be the correct value - let's | |
// store it in a cookie against this path.. | |
var x = appweburl.replace('://', ''); | |
var cookiePath = x.substr(x.indexOf('/'), x.length - x.indexOf('/')); | |
COB.cookieHandler.setCookie(hostCookieName, hostweburl, null, cookiePath); | |
COB.cookieHandler.setCookie(appCookieName, appweburl, null, cookiePath); | |
} | |
else { | |
// deal with "returning to default page in app web (*before it was fixed in March 2013 update*)" case.. | |
COB.cookieHandler.ensureAppUrls(); | |
} | |
} | |
else { | |
// deal with "browsing an app part page in the app web" case.. | |
COB.cookieHandler.ensureAppUrls(); | |
} | |
} | |
// jQuery plugin for fetching querystring parameters.. | |
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]; | |
} | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment