SharePoint 2007 provides us with a very elegant way to insert items into the head section of our HTML pages. These insertions might typically be referencing additional CSS or JavaScript resources. The mechanism that is provided by SharePoint is a DelegateControl with an ID of AdditionalPageHead. The cool fact about the AdditionalPageHead delegate control is that it allows multiple controls to be injected into it!
You can read the following articles to see examples of using the AdditionalPageHead delegate control to perform these types of common tasks:
- Integrating SharePoint 2007 and jQuery
- Using the Delegate Control to Add Meta Tags to SharePoint Pages
- The jQuery library JavaScript files
- Your WebPart class
- JavaScript behaviours for your WebPart
- CSS styles for your WebPart
- CommonPageHeadInfrastructure - A feature that includes a custom web control which writes common JavaScript and CSS references into the page head region and a Feature definition which injects the custom control into the AdditionalPageHead delegate control.
- CustomWebPartFeature - A feature which encapsulates the functionality of your custom web part.
public class PageHeadContentControl : Control { protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.Write("<script type="text/javascript" src="/_layouts/1033/jquery-1.2.6.min.js"></script>") ; } }
And you would deploy that control into the delegate control using a technique similar to what is shown in this article.
As for your custom web part, you would create it as you normally would and, when creating your Child Controls, register your scripts in the typical manner with the ScriptManager like so:As for your custom web part, you would create it as you normally would and, when creating your Child Controls, register your scripts in the typical manner with the ScriptManager like so:"
var scriptPath = "/_layouts/1033/YourFeatureName/YourScriptFileName.js"; var scriptKey = "YourFeatureNameScriptIncludeKey" ; var type = this.GetType() ; var cs = Page.ClientScript; if (!cs.IsClientScriptIncludeRegistered(type, scriptKey)) { cs.RegisterClientScriptInclude(cstype, scriptKey, scriptPath); }
Of course this means that you must deploy the YourScriptFileName.js file as part of your feature. You can learn about the structure for doing that by reading this article.
No comments:
Post a Comment