'use strict'; // Variables var context = SP.ClientContext.get_current(); var hostContext = null; var hostUrl = decodeURIComponent(getQueryStringParameter('SPHostUrl')); var scriptLinkName = 'SampleScriptLink'; var scriptContents = null; var scriptFileName = 'Sample.js'; function getQueryStringParameter(paramToRetrieve) { var params = document.URL.split('?')[1].split('&'); var strParams = ""; for (var i = 0; i < params.length; i = i + 1) { var singleParam = params[i].split('='); if (singleParam[0] == paramToRetrieve) { return singleParam[1]; } } } function logToConsole(text) { if (window.console && window.console.log) { window.console.log(text); } } function onCsomFailed(sender, args) { logToConsole('Request failed: ' + args.get_message() + '\n' + args.get_stackTrace()); } function ensureHostContext(next) { if (hostContext) { if (next) next(); } else { var appSiteUrl = _spPageContextInfo.siteAbsoluteUrl; var scriptbase = hostUrl + '/_layouts/15/'; $.getScript(scriptbase + 'SP.RequestExecutor.js', function () { var factory = new SP.ProxyWebRequestExecutorFactory(appSiteUrl); context.set_webRequestExecutorFactory(factory); hostContext = new SP.AppContextSite(context, hostUrl); if (next) next(); }); } } function checkScriptLink(name, nextExists, nextNotAvailable) { var web = hostContext.get_web(); var userCustomActions = web.get_userCustomActions(); context.load(userCustomActions); context.executeQueryAsync(function () { var enumerator = userCustomActions.getEnumerator(); while (enumerator.moveNext()) { var ca = enumerator.get_current(); var caName = ca.get_name(); if (caName == name) { if (nextExists) nextExists(ca); return; } } if (nextNotAvailable) nextNotAvailable(); }, onCsomFailed); } function getScriptFileContents(next) { $.get('../Scripts/' + scriptFileName, function (data) { scriptContents = data; if (next) next(); }, 'text'); } function getListByUrlPart(urlPart, next, nextNotFound) { var web = hostContext.get_web(); var lists = web.get_lists(); context.load(lists, 'Include(RootFolder)'); context.executeQueryAsync(function () { var list = null; var enumerator = lists.getEnumerator(); while (enumerator.moveNext()) { var checkList = enumerator.get_current(); var listUrl = checkList.get_rootFolder().get_serverRelativeUrl(); if (listUrl.indexOf(urlPart) >= 0) { list = checkList; continue; } } if (list != null) { if (next) next(list); } else { if (nextNotFound) nextNotFound(); } }, onCsomFailed); } function addScriptFile(list, next) { var fci = new SP.FileCreationInformation(); fci.set_overwrite(true); fci.set_url(scriptFileName); fci.set_content(btoa(scriptContents)); list.get_rootFolder().get_files().add(fci); context.executeQueryAsync(function () { if (next) next(); }, onCsomFailed); } function addScriptLink(name, scriptSrc, next) { var web = hostContext.get_web(); var userCustomActions = web.get_userCustomActions(); var userCustomAction = userCustomActions.add(); userCustomAction.set_name(name); userCustomAction.set_location('ScriptLink'); userCustomAction.set_sequence(1001); userCustomAction.set_scriptSrc(scriptSrc); userCustomAction.update(); context.executeQueryAsync(function () { if (next) next(); }, onCsomFailed); } var removeParagraph = '
'; var addParagraph = ''; function removeScriptLink(name) { ensureHostContext(function () { checkScriptLink(name, function (ca) { ca.deleteObject(); context.executeQueryAsync(function () { $('#message').html('ScriptLink Removed
' + addParagraph); }, onCsomFailed); }, function () { $('#message').text('ScriptLink not found.'); }); }); } function createScriptLink() { ensureHostContext(function () { getScriptFileContents(function () { var urlPart = '/SiteAssets'; getListByUrlPart(urlPart, function (list) { addScriptFile(list, function () { addScriptLink(scriptLinkName, '~siteCollection/SiteAssets/' + scriptFileName, function () { $('#message').html('<p>ScriptLink Added</p>' + removeParagraph); }) }) }, function () { $('#message').text('List with url part ' + urlPart + ' not found.'); }) }) }); } // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model $(document).ready(function () { $('#back').html('Back to host site'); ensureHostContext(function () { checkScriptLink(scriptLinkName, function () { $('#message').html('ScriptLink Already Set
' + removeParagraph); }, function () { $('#message').html('ScriptLink Not Set
' + addParagraph); }) }); });