views:

154

answers:

3

Problem description

Is it possible to register (include) javascript within RenderingTemplate? (RenderingTemplates are used to render list forms or fields) By using

<SharePoint:RenderingTemplate ID="NewRelatedListItem" runat="server">
  <Template>
    <span id="part1">
      <SharePoint:ScriptLink Name="SPFormFieldAssistant.js" Localizable="false" /> 
      ...
    </span>
    ...
  </Template>
</SharePoint:RenderingTemplate>

it couldn't be done - it didn't include script at HEAD area, but...:

Source

Is something wrong with my code? Althought script IS at Layouts folder and I checked with Reflector that it uses Layouts folder if Localizable='False'.

I don't want this script to be loaded with every page, but only for forms.

Any ideas on how this could be achieved?

My idea that is not working...

My first idea was to add script programmatically with <% this.Page.ClientScript.RegisterClientScriptInclude("spffa", "/_layouts/SPFormFieldAssistant.js") %>, but it is not working, as that code evaluation is happening in Render method and i read somwhere that you cannot register client scripts within Render method, but usually is done within PreRender method. Pity

My second idea that is still not working

I thought i would create custom UserControl named RegisterClientScript and there within overriden PreRender method i would add scripts i need with client script manager (this.Page.ClientScript...) but, as with SharePoint:ScriptLink, my control also is rendered as text! my control as text

How do i overcome this and where's the catch?

+1  A: 

Sometime back I had a Similar requirement. For which I have included the script Register in the CreateChildControls method of the Field Control class.

Kusek
Thank you, but i found much simplier solution - it is possible to directly add <script> tags to template.
Janis Veinbergs
+1  A: 

Looks like you are missing the runat. Try this:

<SharePoint:ScriptLink Name="SPFormFieldAssistant.js" Localizable="false" runat="server" />
Rich Bennema
Aahh, thank you! Doh! But i`v solved this already without using such server controls.
Janis Veinbergs
A: 

It turns out that you can directly add <script> tags to template!

<script src="/_layouts/SPFormFieldAssistant.js" type="text/javascript"></script>

Thank you for the notes however.

Janis Veinbergs