Monday, April 11, 2011

Dynamically set the DefaultValue of a ParameterBinding in a DataFormWebPart

In my custom aspx page in WSS I am using a DataFormWebPart with an xsl file to render some data. In order to pass values to the xsl I use parameter bindings. Specifically, I need to pass in the server host url like this:

<ParameterBinding 
    Name="HttpHost" 
    Location="CAMLVariable" 
    DefaultValue="http://hardcoded.com" />

This works fine, but the next thing I want to do is to get the host name dynamically. So figuring out how to get that from SharePoint I added the following binding:

<ParameterBinding 
    Name="HttpHost" 
    Location="CAMLVariable" 
    DefaultValue='<%# SPContext.Current.Site.Url.Replace
       (SPContext.Current.Site.ServerRelativeUrl, "") %>' />

Now to the problem. The code works as expected if used some other place in the page, but with the above code SharePoint reports:

Web Part Error: The 'ParameterBindings' property of 'WebPartPages:DataFormWebPart' does not allow child objects.

Anyone have a take on this?

Update: I have enabled server side code according to this article

From stackoverflow
  • Ok, I found a solution that is not that elegant but it works.

    After trying various methods of manipulating the ParameterBindings property without success I thought of how I could get the dynamic value in there using the Location attribute.

    The ParameterBinding Location attribute refers to where to fetch the value from. Articles like this hints of the "Control()" option. So changing the parameter binding to:

    <ParameterBinding
      Name="HttpHost"
      Location="Control(MyHttpHost, Text)"
      DefaultValue="" />
    

    and adding the following code to my page:

    <asp:TextBox ID="MyHttpHost" runat="server" Visible="false" />
    <script runat="server">
    protected void Page_Load()
    {
      MyHttpHost.Text = 
       SPContext.Current.Site.Url.Replace(SPContext.Current.Site.ServerRelativeUrl, ""); 
    }
    </script>
    

    ...actually did the trick!

    To get to the parameter values from within the accompanying XSL file I put param elements in the root element. The param name attribute must match that of the ParameterBinding:

    <xsl:stylesheet ...>
        ...
        <xsl:param name="HttpHost"/>
    

    The parameter can then be referenced as any other XSL variable.

  • Hi,

    Can you then use it a template to show the value? for example in a xsl:value-of?

    Thanks!

    Peter Lillevold : @zerabba - see my updated answer above. BTW, you should use the comment feature instead of adding an answer (unless you actually have an answer to the question :)
  • Using Server Variables probably makes more sense: http://mdasblog.wordpress.com/2007/10/19/data-view-web-part-parameters-based-on-server-variables/

    Peter Lillevold : For the specific case of the url I would agree, so +1. But my answer also works for any other value one wishes to pass into the xsl.

0 comments:

Post a Comment