Forum Discussion
SharePoint Online Usage Statistics with JavaScript Injection Add-in
Instead of using that Add-in model, I would look at using this open source solution, which is based on the PnP responsive UI project and works on the new modern doc libraries also.
https://github.com/eoverfield/SP-Custom-Script-Action
It will inject your Javascript using the User Custom Action method. Just modify the 2 XML PnP Provisioning template files to reference your .js file. The PS script will upload the .js file to the location you specify in template file and then inject it into every page in the site collection.
So you will have to run this PS against each site collection
Thanks
Steve
I can finally report some success. To make this easier I created two functions, one based on John's example and one I put together by looking at the link you gave and seeing if there is another way to write it. my second function works and gives no errors in the console. The PS works like a charm and simply replaces my customSciptAction when I call my set up function.
function debugit2() { console.log("Debugit2 Started...."); var ctx = SP.ClientContext.get_current(); var web = ctx.get_web(); functionToCall().done(function (returnedValue) { // do something with the returned value console.log(web.get_title()); }); function functionToCall() { var dfd = $.Deferred(function () { ctx.load(web); ctx.executeQueryAsync(function () { dfd.resolve(web); }, function (sender, args) { //throw an error var failmessage = args[1].get_message(); console.log(" oh no not again " + failmessage); }); }); return dfd.promise( /* do I chain here to say get my user or site or list objects */ ); } }
The only thing I need to do now is chain my promises to get the jsom objects: user, list, site etc, rename my function calls and then re-add my google delegate code. I think I hit the pain barrier on this ;-(
- Daniel WesterdaleMay 09, 2017Iron Contributor
Just a quickup update ,
I have now tried a few methods for injecting my analytics script into my site or web. Looks like a script link is better than a script block . Also I am getting mixed results with a heavily branded site. Thanks for your previous actions as I did use it to inect a simpel script block into my iste
Got really stuck on John Liu's http://johnliu.net/blog/2015/12/convert-sharepoint-jsoms-executequeryasync-to-promise-in-the-prototype ... I seem never to get passed the error 'Protoype not defined' . Spoke to Hugh from Rencore about his custom SOD loader for sp.js and runtime.js but his answer gave me a headache trying to understand it lol.... Level 400 JavaScript springs to mind!
Annyway John kindly pointed at this one page add your script .....
Ok before I am ready to inject my final script I do need to remove the first script action with the client id shown ( see attachment) . It is part of the PnP CoreJavaScriptInjection which despite sucessfully deploying via VS2017 and removing via the UI , I can't find a way of remove this Action link.
- Steve BorgwardtMay 09, 2017Brass Contributor
Hi, Daniel
Glad to hear you are getting closer with your solution. In regards to that first client script action (PnP), I believe you still need that. But you may want to clean everything up first (remove all script actions) and then uses the PnP injection script method.
Thanks
- Daniel WesterdaleMay 11, 2017Iron Contributor
I applied the script, yesterday evening on our branded test site . The good news I am able to see some of my Custom Dimensions on our https://analytics.google.com portal. The bad news is we experienced some unfortunate side effects ... No ribbon bar on list settings, some web part pages not showing content.
Before I think of a rewriting say using JSCore & batching I think I will change sequence and also deal wth the clash shown below - this is an Add-in installed years ago that nobody seems to remember anything about ;-(. As anybody experienced similar issues with Custom Actions?