Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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));
    }
}
share|improve this question
    
    
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 Jul 5 '12 at 9:16
add comment

2 Answers

You need to register the script. Something like :

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

Here is reference!

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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