views:

519

answers:

3

I have two SharePoint features, each implemented identically (see below), that add controls to a delegate control in the header of my pages. One of the controls depends on the other (one is the jQuery library, and the other depends upon jQuery), however, when the controls are rendered, the order of the controls is incorrect. How do I specify the order that these controls are rendered in?

The control (both are identical, except that they reference a different .js file):

<%@ Control ClassName="MyScriptControl" %>
<script type="text/javascript" src="/_layouts/MyScript.js"></script>

feature.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<Feature Id="AA9D59AC-D53E-4338-9B52-CD39F2A8C31A" 
    ActivateOnDefault="true" 
    Title="My Feature" 
    Description="Does something." 
    Scope="Site" Version="1.0.0.0" 
    Hidden="FALSE" 
    DefaultResourceFile="core" 
    xmlns="http://schemas.microsoft.com/sharepoint/"&gt;
    <ElementManifests>
        <ElementManifest Location="MyFeature\Elements.xml" /> 
    </ElementManifests>
</Feature>

Elements.xml:

<?xml version="1.0" encoding="utf-8" ?> 
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" 
    Id="24A4BB9A-9636-4a73-B3A5-35661DE19902">
    <Control Id="AdditionalPageHead" 
        ControlSrc="~/_controltemplates/MyControl.ascx" /> 
</Elements>

The output on the page (jQuery is deployed before DependsOnjQuery):

<script type="text/javascript" src="/_layouts/DependsOnjQuery.js"></script>
<script type="text/javascript" src="/_layouts/jQuery.min.js"></script>

I want MyControl1.js to render before MyControl2.js.

+1  A: 

It looks to me like the Sequence attribute can be used to control the order in which these guys are rendered. Give your jQuery control a sequence of 50 and your DependsOnjQuery control a sequence of 100 and you should be good.

See the Delegate Control documentation on MSDN for more info.

Nathan DeWitt
That's what I thought at first too, but when I tried this out, it didn't change the order.
Kyle Trauberman
A: 

did you try and set the DEFER="DEFER" attribute on the script.

that would tell the user agent that the script should wait for the page to load and render before it loads the script.

Else you could hack it by setting a variable at the one script, that is set to true when script is loaded. In the other you placed logic that tested (think loop and timeout method) if the script was loaded and then calls your logic when both scripts are loaded.

not very nice, but you need some kind of logic to handle execution, and not just run the methods inline in your script.

To nathan: sequences in delegates are used to determine which of the delegate controls to load. This should have nothing to do with the issue in question. As i understand it the JS files just set the value on a script that just happens to be a delegate control.

Anders Rask
I was hoping the sequences would affect more... sometimes the documentation isn't... complete.
Nathan DeWitt
+1  A: 

I was able to get this to work by adding a second DelegateControl to the header of my masterpage, and adding jQuery to that delegate. This way, I can make sure that jQuery will always load before everything else, without having to write some logic to delay the loading of my dependant libraries.

<SharePoint:DelegateControl runat="server" ControlId="jQueryDelegate" AllowMultipleControls="true"/>
<SharePoint:DelegateControl runat="server" ControlId="AdditionalPageHead" AllowMultipleControls="true"/>
Kyle Trauberman