Saturday, February 19, 2011

How to register Javascripts within SharePoint page programmatically

When you’re integrating script within a SharePoint page, you can use the ClientScriptManager object to add and manage scripts within a Web application. For example, the following code snippet shows a simple method that ensures only one instance of each script is added to a page:

public static void RegisterScript
(ref ClientScriptManager csm,
string key, string url)
{
if (!csm.IsClientScriptBlockRegistered(key))
csm.RegisterClientScriptInclude(key, url);
}

For more information on the ClientScriptManager, see http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.aspx.