04 July 2012

SharePoint ECMA libs in non-master page

If you have a SharePoint .aspx page that has no master page content or containers and you want to include the SharePoint ECMA client object model libs, SharePoint is very particular about how you do that.

As we know, including the libraries is as simple as including the following line of markup:

<SharePoint:ScriptLink runat="server" ID="sl" Name="SP.js" OnDemand="false" Localizable="false" />

Note: the OnDemand attribute is set to false here to ensure that the script files are loaded immediately and so that we don't have to over-complicate with ExecuteOrDelayUntilScriptLoaded()

The key thing to remember with this piece of markup is that it MUST appear after the form element. So it will look a little like this:

<form id="form1" runat="server">
<SharePoint:ScriptLink runat="server" ID="sl" Name="SP.js" OnDemand="false" Localizable="false" />

I had originally tried to put this in the <head> ... </head> element and was left head-scratching as to why it failed to include the scripts.

1 comment:

Mihail Stacanov said...

You could also register scripts with some static methods of SPScriptLink instead of using SPScriptLink in a declarative way:
* ScriptLink.RegisterOnDemand (OnDemand=true)
* ScriptLink.RegisterScriptAfterUI (LoadAfterUI=true)

Post a Comment