1

I have a publishing page in which I have placed content editor web part which contains the link to redirect the user to application page.

Code in the content editor web part is:

<script type="text/javascript">
    function redirectToDownloadsPage() {
    SP.SOD.executeOrDelayUntilScriptLoaded(function(){
        var ctx = SP.ClientContext.get_current();
        var items = SP.ListOperation.Selection.getSelectedItems(ctx);
        var myItems = '';
        var i;

        for (i in items) {
            myItems += '|' + items[i].id;
        }

        if (myItems) {        
            var siteUrl = url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl + "/_layouts/15/DownloadImages.aspx?Items=" + myItems;        
            window.location.href = siteUrl
        }
        else {
            alert("Select atleast one item");
        }
    }, "SP.js");
 }
</script>
<a onclick="return redirectToDownloadsPage();" href="#"> Download Images </a>  

My problem is when I edit the page and click on the link JavaScript works, then I check in the page then also the JavaScript works fine but when I publish the page the JavaScript does not work.

I am getting no items in the item in JavaScript.

What am I missing?

8
  • did you try using ExecuteOrDelayUntilScriptLoaded? It seems like SP.js is not loading or loading at last.
    – Amit
    Commented May 12, 2014 at 9:51
  • As a part of the new Minimal Download Strategy (MDS), sp.js is not loaded by default on published pages. You will have to load your sp.js explicitly from the /_layouts/15 directory.
    – Amit
    Commented May 12, 2014 at 9:54
  • Thank you for response sir, now to make some experiments I removed the content editor web part and added Script Editor web part. And placed the above code (see the code in post). But it works until the page is published. And after publishing the page I am getting the error: Unable to get property 'Selection' of undefined or null reference @Amit
    – users1100
    Commented May 12, 2014 at 10:27
  • The same error occurred when I place content editor web part
    – users1100
    Commented May 12, 2014 at 10:33
  • Do you have any idea about this issue sir? @Amit
    – users1100
    Commented May 12, 2014 at 11:35

3 Answers 3

2

Try this:
In the Content Editor WebPart put the following:


<script src="/sites/test/_layouts/15/SP.Runtime.js" type="text/javascript">
<script src="/sites/test/_layouts/15/SP.js" type="text/javascript">
<script src="/sites/test//sites/test/SiteAssets/Scripts/Script.js" type="text/javascript">

<a onclick="return redirectToDownloadsPage();" href="#"> Download Images </a> 

Now create a folder Scripts in the SiteAssets and put jquery-1.11.1.min.js and Script.js file in that. Which will contain following code:


 function redirectToDownloadsPage() {
    var ctx = SP.ClientContext.get_current();
    var items = SP.ListOperation.Selection.getSelectedItems(ctx);
    var myItems = '';
    var i;

    for (i in items) {
        myItems += '|' + items[i].id;
    }

    if (myItems) {        
        var siteUrl = url = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl + "/_layouts/15/DownloadImages.aspx?Items=" + myItems;        
        window.location.href = siteUrl
    }
    else {
        alert("Select atleast one item");
    }
 }
1

if all else fails just reference sp.js directly on the master page using:

<SharePoint:ScriptLink language="javascript" Name="sp.js" runat="server"           OnDemand="false" LoadAfterUI="true" Localizable="false"><SharePoint:ScriptLink>
0

I faced the same issue, my custom CEWP script was not loading because of url redirection through _layout/15/start.aspx.

Just I have de-activated the Minimal download strategy site feature and everything is working as expected. I hope this helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.