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));
    }
}
2
  • stackoverflow.com/questions/792851/…
    – Nikhil J
    Commented Jul 4, 2012 at 15:28
  • this is what I based my code on, however the life cycle seems to subltly different in that you cannot seem to add script links to the page
    – Rob
    Commented Jul 5, 2012 at 9:16

2 Answers 2

1

You need to register the script. Something like :

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

Here is reference!

2
  • 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
    Commented Jul 5, 2012 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
    Commented Jul 5, 2012 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.

2
  • 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
    Commented Jul 5, 2012 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
    Commented Jul 5, 2012 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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