Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 870
Peter Lillevold Avatar asked Mar 04 '09 10:03

Peter Lillevold


2 Answers

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.

like image 165
Peter Lillevold Avatar answered Nov 15 '22 09:11

Peter Lillevold


Using Server Variables probably makes more sense: http://mdasblog.wordpress.com/2007/10/19/data-view-web-part-parameters-based-on-server-variables/

like image 41
Marc D Anderson Avatar answered Nov 15 '22 08:11

Marc D Anderson