Skip to main content

Load dynamically JavaScript file inside Custom Action

This starts to be very common scenario in the SharePoint online projects:


You want to have custom actions (“My Action”) that execute custom logic.
The main challenge is how to load the JavaScript file with this custom logic (/sites/demo/SiteAssets/Actions/dts.demo.script.js) only when the custom action needs it.

One approach described in many posts is to use a Script link. 
Here is a working example:

    function RegisterScriptLink() {

        var scriptBlock = 'var headID = document.getElementsByTagName("head")[0];var newScript = document.createElement("script");newScript.type = "text/javascript";newScript.src = "'
        scriptBlock += '/sites/demos/SiteAssets/Actions/dts.demo.script.js' + '?ver=' + ((new Date()) * 1);
        scriptBlock += '";headID.appendChild(newScript);';

        var context = new SP.ClientContext.get_current;
        this.site = context.get_web();
        var collUserCustomAction = this.site.get_userCustomActions();
        var scriptLink = collUserCustomAction.add();
        scriptLink.set_name('DTSDemo Script');
        scriptLink.set_title('DTSDemo Script');
        scriptLink.set_scriptBlock(scriptBlock);
        scriptLink.set_location('ScriptLink');
        scriptLink.set_group('');
        scriptLink.update();
        context.executeQueryAsync(
            function (a, b) {
                console.log('success');
            },
                function (a, b) {
                    console.log('error');
                    console.log(a);console.log(b);
                });
    }
    
The problem here is that you will load the script to all pages inside the web (!), which is not the best architecture.

The solution I find better is to load the file inside the custom Action.
Note that I create the custom actions with JavaScript instead of SharePoint designer! This one way to automate and simplify the deployment of your project. Also this provides you more options to configure the action.

Ribbon Action
Use the EnabledScript methold to load you script. Here is an example:



ECB Action




Here is the link to the code from this post.



PS: Your feedback is highly appreciated. 


Comments

Popular posts from this blog

ClientPeoplePicker in SharePoint 2013

Start using SharePoint 2013 I noticed that the way for selecting people or groups is changed. The new way is simple – just ‘Enter name or email address’ without any icons for ‘Check Names’ or ‘Browse’. I guess that the PeoplePicker is changed but NO. PeoplePicker sitll has the same functionality as before. There is a new control called ClientPeoplePicker . How to use it: 1. Add this references <% @ Register TagPrefix ="wssawc" Namespace ="Microsoft.SharePoint.WebControls" Assembly ="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 2. Add the following control declaration       < wssawc : ClientPeoplePicker          Required ="true"          ValidationEnabled ="true"          ID ="peoplePicker"          runat ="server"   ...

SharePoint and List Joins with CAML

This came to me as an issue when I started migrating one project from SharePoint 2010 to SharePoint 2013. The code uses LINQ to SharePoint and it was not written in a clever way. We decide to use CAML queries and optimize the code. The scenario: Create gridview that gathers information from three(or more) lists. The problem: How to create the CAML query joins and projected fields correctly. Explanation : Option 1: Get generated CAML query from LINQ to SharePoint code This doesn’t work. I wrote the LINQ to SharePoint code connected the three lists. When I executed the code the following error was thrown – “ The query uses unsupported elements, such as references to more than one list, or the projection of a complete entity by using EntityRef/EntitySet. ” Here is some explanation about it and why this can’t be used. Option 2: Write the query yourself I found this article in MSDN . Read it ! The second part in it is exactly my scenario. But it takes me a lot of time to un...

SharePoint User Group Bulgaria

In the past year I invested my free time in activities related to the SharePoint User Group Bulgaria. This is the reason why I stopped writing in this blog. Please start following this new place  www.sugbg.com . I promise you that you will find there truly valuable content.