1

I have a little (crude) javascript function that should add a javascript file to every page on my site using custom actions (including the subsites via a recursive function). It seems to work well on every page I have tried so far, except the Site Settings pages. Can any one tell me why this isn't working on the settings pages and how I can get it to work there.

[Using Sharepoint Online 2013.]

<a onclick="loadJQuery();" href="#">Create Custom Actions</a>
<div id='feedback'></div>

<script>

function loadJQuery() {
    var count = 0;

    function addScript() {
        if ( count++ <=1 ) {
            var script=document.createElement('script');
            script.setAttribute("type", "text/javascript");
            script.setAttribute("src", _spPageContextInfo.siteServerRelativeUrl + "/MyLibrary/jquery-1.12.0.min.js");
            script.setAttribute("async", true);

            document.getElementsByTagName("head")[0].appendChild(script);
        }
    }

    function waitForJQuery() {
        if (typeof jQuery == 'undefined') {
            addScript();
            setTimeout(waitForJQuery, 50);
        } else {
            AddCustomActions();
        }
    }
    waitForJQuery();
}

function AddCustomActions() {
    var ignoresites = ['App Site 1', 'App Site 2'];

    function customiseAllSubsites(success,error) {
        var ctx = SP.ClientContext.get_current();
        var web = ctx.get_site().get_rootWeb();

        var level = 0;
        var customiseAllSubsitesInner = function(web,success,error) {
            level++;
            var ctx  = web.get_context();
            var uca  = web.get_userCustomActions();
            var webs = web.get_webs(); 

            var thisweb = web;

            ctx.load(web,'UserCustomActions','Title');
            ctx.load(webs, 'Include(Title,Webs,Url)');
            ctx.executeQueryAsync(
                function(){
                    // Add the userCustomAction to the site if it isn't there yet
                    var found = false;
                    var cae = uca.getEnumerator();
                    while (cae.moveNext())  {
                        // Get the current custom action
                        var cca = cae.get_current();

                        if (cca.get_title() == 'My ScriptLink') {
                            found = true;
                            $('#feedback').append('<font color=green><b>' + thisweb.get_title() + ' already has the custom action set</b></font><br/>');
                        }
                    }
                    if ( ! found ) {
                        var nca = uca.add();
                        nca.set_location('ScriptLink');
                        nca.set_scriptSrc('~SiteCollection/MyLibrary/myscript.js');
                        nca.set_sequence(10);
                        nca.set_title('My ScriptLink');
                        nca.update();

                        $('#feedback').append('<font color=orange><b>Adding custom action to: ' + thisweb.get_title() + '</b></font><br/>');
                    }
                    if ( webs.get_count() > 0 ) {
                        // Get the sub-sites for this website
                        for(var i = 0; i < webs.get_count();i++){
                            var subweb = webs.getItemAtIndex(i);
                            if ( ignoresites.indexOf(subweb.get_title()) == -1 ) {
                                // Recusively add the content of the subsite to the menu
                                customiseAllSubsitesInner(subweb,success,error);
                            }
                        }
                    }

                    level--;
                    if (level == 0 && success) {
                        ctx.executeQueryAsync(success,error);
                    }
                },
                error
            );
        }; 

        customiseAllSubsitesInner(web,success,error);    
    }

    customiseAllSubsites(
        function onQuerySucceeded(sender, args) {
            $('#feedback').append('<font color=green><b>New Support files added to Site. Refresh the page.</b></font><br/>');
        },
        function onQueryFailed(sender, args) {
            $('#feedback').append('<font color=red><b>Request failed. ' + args.get_message() + '<br>' + args.get_stackTrace() + '</b></font><br/>');
        }
    );
}
</script>
  • Waar laat je deze code uitvoeren? In de Masterpage? – Danny '365CSI' Engelman Jan 28 '16 at 19:05
  • @DannyEngelman - This code gets run on a separate page in the site collection root, and it only needs to be run once (unless you add new subsites), then the custom actions are defined for all the pages (except for obviously site settings pages). – Kees Jan 29 '16 at 9:48
  • Are you allowed to change the MasterPage? – Danny '365CSI' Engelman Jan 31 '16 at 9:26
  • I am the administrator, so I have permission to change Master Pages, but I understand from many different posts that this no longer the preferred way to apply site wide changes. – Kees Feb 1 '16 at 12:58
  • It all depends on what you are trying to achieve, you want a script file executed on all pages, putting it in the MasterPage is simpler code, and easier than attaching a script to each and every page and then ever so often check pages that have no scripts attached. PS. script.setAttribute("onload", AddCustomActions ); save you some lines of code and kludgy setTimeout usage – Danny '365CSI' Engelman Feb 1 '16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.