0

I'm trying to add a javascript link to my Sharepoint page via a HttpModule, using the 'OnPreRequestHandlerExecute' event.

However nothing is happening and the script link is not added but i'm unsure why

public void Init(HttpApplication context)
{
    context.PreRequestHandlerExecute += OnPreRequestHandlerExecute;
}

void OnPreRequestHandlerExecute(object sender, EventArgs e)
{
    HttpContext currentContext = HttpContext.Current;
    Page page = currentContext.CurrentHandler as Page;

    if (page != null)
    {
        AddScript(page);
    }
}

void AddScript(Page page)
{

    var link = new ScriptLink
                          {
                              Defer = true,
                              Localizable = false,
                              Name = "_LAYOUTS/CompanyFolder/AcceptCookies.js"
                          };            

    page.Controls.Add(link);
}

Update 1

I also tried registering the script and this did not work either (with and without the ResolveClientUrl).

void AddScript(Page page)
{
    if (!page.ClientScript.IsClientScriptIncludeRegistered(scriptName))
    {
        string script = "<script type=\"text/javascript\" src=\"/_layouts/CompanyFolder/AcceptCookies.js\"></script>";
        page.ClientScript.RegisterClientScriptInclude(scriptName, page.ResolveClientUrl(script));
    }
}
1

You need to register the script. Something like :

if(!ClientScript.IsClientScriptIncludeRegistered("ScriptFile")) 
{      
    ClientScript.RegisterClientScriptInclude("ScriptFile", "/UserControls.js"); 
} 

Here is reference!

  • this is one of the other things I tried and still no luck, the script link just does not get added to the page. – Rob Jul 5 '12 at 9:03
  • here is what I had if (!page.ClientScript.IsClientScriptIncludeRegistered(scriptName)) { string script = "<script type=\"text/javascript\" src=\"/_layouts/CompanyFolder/AcceptCookies.js\"></script>"; page.ClientScript.RegisterClientScriptInclude(scriptName, page.ResolveClientUrl(script)); } – Rob Jul 5 '12 at 9:06
0

Why do you try to use HttpModule to add script on the page? In my practice I found out that the easiest way is to add script through delegate control. See this example. In this case you can easily activate/diactivate your script on the site pages by feature.

  • I wanted to use a HttpModule as i didn't want to have to amend the markeup of any master pages (we have at least 15 sharepoint sites). I think however that this is the direction to go in as the Httpmodule doesn't seem to behave as expected. – Rob Jul 5 '12 at 9:01
  • You don't need to change markup :) You should only create delegate control, deploy it on the farm and activate features on the sites. If you have site definition you can include this feature to the features elemet for automatic activation. If you have already created sites you can use PowerShell to automate feature activation on these sites. – Alexander Jul 5 '12 at 9:14

Your Answer

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

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