Skip Ribbon Commands
Skip to main content

Vardhaman Deshpande | zevenseas | SharePoint Blog

:

Vardhaman Deshpande | zevenseas | SharePoint Blog > Posts > RegisterClientScriptBlock for SharePoint Sandbox Solutions
August 21
RegisterClientScriptBlock for SharePoint Sandbox Solutions

So we had this requirement where we had to check if a script was already loaded on the page before we push it. So my natural choice was to use the RegisterClientScriptBlock method. But this being a Sandbox solution, like always, things were much more difficult than they initially appeared.

The code executed without throwing any error so I thought I was good to go but the script was not getting registered and also the IsClientScriptBlockRegistered method was not doing its job. So after some searching around, I found the following page which explained my scenario:
http://blog.a-dahl.dk/post/Sharepointe28093Where-is-my-Page-object.aspx 

So turns out that sandbox solutions runs in a "sandbox" mode with separate context and no access to the rest of the page. So to my huge disappointment, the ClientScriptManager class was out of bounds. Now it was up to me to figure out a workaround for this issue.

So I thought, why not push some good old JavaScript to the page to check the loading of the script? The challenge before me was that since I was pushing the script from server side to the page, the code would be executed before my DOM was loaded. Also, I could not use any jQuery here because it was included in my script which was to be checked and  loaded. So 1) I had to make sure that my code would get executed only after the page was loaded and  2) I had to do it using pure JavaScript.

The first problem could have been solved by using window.load = function( ) {} but due to the function's browser incompatibility, I decided against it. Thankfully, SharePoint provides an Out of the Box mechanism called _spBodyOnLoadFunctionNames in which we can push functions to be executed when the body tag of the document completes loading.
Being using jQuery very heavily in the past, using pure JavaScript was also an exciting challenge which was fun to do. So after about an hour of fiddling with this issue, I managed to put together a function which would check if a script with the particular id was loaded and if not, then only load it on the page. Here is my SandboxRegisterClientScriptBlock function:

public static void SandboxRegisterClientScriptBlock(string scriptId, string scriptUrl,Page page)
{
    //Add script control.
    HtmlGenericControl addJscript = new HtmlGenericControl("script");
    addJscript.Attributes.Add("type", "text/javascript");
    //Checks if the script is already loaded and if not, load it.
    addJscript.InnerHtml = @"function CheckScriptAndLoad() {
                 //Check if script is loaded. Return true if yes, else return false.
                          var isScriptLoaded = function (scriptId) {
                                   if (document.getElementById(scriptId) != null) {
                                   return true;
                                  }
                                  else {
                                           return false;
                                  }
                          }
//If script is not loaded, create a DOM element with specified script id and url.
                          if (!isScriptLoaded('" + scriptId + @"')) {
                                   var script = document.createElement('script');
                                   script.type = 'text/javascript';
                                   script.id = '" + scriptId + @"';
                                   script.src = '" + scriptUrl + @"';
                                   document.body.appendChild(script);
                          }
                 }
                 _spBodyOnLoadFunctionNames.push('CheckScriptAndLoad');";
            page.Controls.Add(addJscript);
}

 

 

Comments

Hi Vardhaman

Can you please Explain in deatil how to consume the above method.
 on 10/31/2012 6:15 AM

Re: RegisterClientScriptBlock for SharePoint Sandbox Solutions

HI,
You can consume the method from your C# code just like you would use the RegisterClientScriptBlock method.

You might use it in the CreateChildControls method of a WebPart.

Something like:
SandboxRegisterClientScriptBlock("myScriptID","_layouts/js/myscript.js", this);

-Vardhaman
ZSHOSTED\vardhaman on 11/9/2012 7:23 AM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


ReCaptcha

 

Attachments

 

 Statistics

 
Views: 1149
Comments: 2
Tags:
Published:274 Days Ago