Sharepoint 2013 Hosted App execute javascript on all pages load -
i know if it's possible have sp 2013 hosted app injects piece of javascript gets executed on every page load.
for sake of simplicity, imagine want create app on every page load of sp site displays alert('hello world!');
i don't want have remote web, pure , simple hosted app can added picking sp store.
is possible?
thanks!
you can inject javascript using custom action script link @alexcode suggests app require web - full control
permissions. can't remember adapted code while looking add-in development. poc should make more robust before using in live environment.
app.js contents
(function(undefined) { "use strict"; var actions, web, context, hostcontext, actiondescription; console.log('running function'); // getquerystringparameter: method retrieve query string parameter values var getquerystringparameter = function(param) { var params = document.url.split('?')[1].split('&'); var length = params.length; (var = 0; < length; = + 1) { var singleparam = params[i].split('='); if (singleparam[0] == param) { return singleparam[1]; } } }; // inject: method return string js ran custom action var inject = function() { debugger; var scripttorun; scripttorun += '(function (){' + 'var elem = document.getelementsbytagname("head")[0];' + 'var script = document.createelement("script");' + 'script.appendchild(document.createtextnode(alert("hello world")));' + 'elem.appendchild(script);' + '}());'; return scripttorun; }; var success = function() { alert('done'); } var fail = function() { alert('failed'); } // unprovision: removes custom action , javascript file var unprovision = function() { context = sp.clientcontext.get_current(); hostcontext = new sp.appcontextsite(context, decodeuricomponent(getquerystringparameter('sphosturl'))); // load custom actions host web actions = hostcontext.get_web().get_usercustomactions(); context.load(actions); web = hostcontext.get_web(); context.load(web); context.executequeryasync(unprovisionex, fail); }; // unprovisionex: method remove custom action var unprovisionex = function() { var enumerator = actions.getenumerator(); var removethese = []; // find custom action while (enumerator.movenext()) { var action = enumerator.get_current(); if (action.get_description() == actiondescription && action.get_location() == 'scriptlink') { // add temporary array (we cannot modify enumerator while enumerating) removethese.push(action); } } // actual removal of custom action var length = removethese.length; (var = 0; < length; i++) { removethese[i].deleteobject(); delete removethese[i]; } context.executequeryasync(success, fail); }; // provisionscriptlink: method adds custom action var provisionscriptlink = function() { var enumerator = actions.getenumerator(); var removethese = []; // check if custom action exists, if remove before adding new 1 while (enumerator.movenext()) { var action = enumerator.get_current(); if (action.get_description() == actiondescription && action.get_location() == 'scriptlink') { removethese.push(action); } } var length = removethese.length; (var = 0; < length; i++) { removethese[i].deleteobject(); delete removethese[i]; } // create custom action var newaction = actions.add(); // 'description' we'll use uniquely identify our custom action newaction.set_description(actiondescription); newaction.set_location('scriptlink'); newaction.set_scriptblock(inject()); newaction.update(); context.executequeryasync(success, fail); }; // provision: starts uploading javascript file host we, once done continue provisionscriptlink() method var provision = function() { context = sp.clientcontext.get_current(); hostcontext = new sp.appcontextsite(context, decodeuricomponent(getquerystringparameter('sphosturl'))); // load custom actions host web actions = hostcontext.get_web().get_usercustomactions(); context.load(actions); web = hostcontext.get_web(); context.load(web); context.executequeryasync(provisionscriptlink, fail); }; document.getelementbyid("add").onclick = provision; }());
default.apsx content
<%-- following 4 lines asp.net directives needed when using sharepoint components --%> <%@ page inherits="microsoft.sharepoint.webpartpages.webpartpage, microsoft.sharepoint, version=15.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c" masterpagefile="~masterurl/default.master" language="c#" %> <%@ register tagprefix="sharepoint" namespace="microsoft.sharepoint.webcontrols" assembly="microsoft.sharepoint, version=15.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c" %> <%@ register tagprefix="utilities" namespace="microsoft.sharepoint.utilities" assembly="microsoft.sharepoint, version=15.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c" %> <%@ register tagprefix="webpartpages" namespace="microsoft.sharepoint.webpartpages" assembly="microsoft.sharepoint, version=15.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c" %> <%-- markup , script in following content element placed in <head> of page --%> <asp:content contentplaceholderid="placeholderadditionalpagehead" runat="server"> <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script> <script type="text/javascript" src="/_layouts/15/sp.js"></script> <!-- add css styles following file --> <link rel="stylesheet" type="text/css" href="../content/app.css" /> </asp:content> <%-- markup in following content element placed in titlearea of page --%> <asp:content contentplaceholderid="placeholderpagetitleintitlearea" runat="server"> page title </asp:content> <%-- markup , script in following content element placed in <body> of page --%> <asp:content contentplaceholderid="placeholdermain" runat="server"> <div> <button type="button" value="add" name="add" id="add">add</button> </div> </asp:content> <asp:content contentplaceholderid="placeholderutilitycontent" runat="server"> <!-- add javascript following file --> <script type="text/javascript" src="../scripts/app.js"></script> </asp:content>
Comments
Post a Comment