January 20, 2012

SharePoint: Filter Data Form Web Part using Metadata value of current Publishing Page

Challenge:

On a Welcome Page of a Publishing Site I had few Managed Metadata fields. I was to implement a functionality that would fetch items from a list located on another site and filter the items using value of a metadata field of the current Welcome Page.

The idea is brilliant: there is a customer site, and there is a common list of contacts. Due to requirements, one common contacts list was preferred, so I decided to map contacts to customers by adding metadata to contact indicating to what customer it belonged to AND have the same metadata defined on the customer page's properties.

Solution:

As you cannot reference page field values in DFWP like it is done with CQWP, you must use Control parameter in Location attribute inside your ParameterBinding node (thank you Tomas Breen).

<ParameterBinding Name="Customer" Location="Control(CustomerName,ItemFieldValue)" DefaultValue=""/>

Customer above is the name of the parameter you will use in XSL. CustomerName is the ID of the Control on the Page Layout that contains the value you want to use as a filter. ItemFieldValue is the magic word I had to look for way too long.

Then, in the xsl:stylesheet of your DFWP, you will have

<xsl:param name="Customer" />

and finally you will do the filtering like this

<xsl:variable name="Rows" select="/dsQueryResponse/Rows/Row[@Customer= substring-before($Customer, '|')]"/>

Note that @Customer is the value of the Customer metadata field of one of the list items we're about to display. $Customer is the xsl:param that contains the value from the Customer metadata field of the current Welcome Page. Substring is done because Welcome Page metadata value comes in the form CompanyX|GUID.

Not yet working? No, because what is still missing is the actual CustomerName control that will contain the Customer value of the current Welcome Page. In my case I used TaxonomyFieldControl and added the ID attribute. You could also use SharePointWebControls:FieldValue, or SharePointWebControls:ListItemProperty, but TaxonomyFieldControl allows editing of the value when in Edit mode and I needed that.

<Taxonomy:TaxonomyFieldControl FieldName="becb57f8-ac0e-45b1-a0b4-e5fa71c0d7fa" ID="CustomerName" runat="server"></Taxonomy:TaxonomyFieldControl>

Note that you will not be able to preview the functionality in SPD. You will have to browse to the page to see your beautiful markup magic.

2 comments:

  1. Hi,
    I have a similar requirement, where i wish to display a specific field from publishing page on a data form web part. I have used your same logic that you have mentioned in your post. but for some reason, it is not displaying the field that i have selected within the web part. I don't want to filter the data, i just would like to get that field from publishing page auto populate into an input field wihtin data form web part. can you please throw any ideas further on how to autopopulate a data form web part field from a publihsing page meta data fields? TIA.

    ReplyDelete
  2. Hi,
    I believe it should be possible. This is pretty much the same I have already written, but in a more logical order so you can double check you have everything needed.

    You need 4 things:

    1. The specific control (which value you want to display) must be on the page, e.g.
    <SharePointWebControls:FieldValue FieldName="becb57f8-ac0e-45b1-a0b4-e5fa71c0d7fa" ID="CustomerName" runat="server"></SharePointWebControls:FieldValue>

    2. ParameterBinding binding to the value of that control:
    <ParameterBinding Name="Customer" Location="Control(CustomerName,ItemFieldValue)" DefaultValue=""/>

    3. parameter defined in xsl:stylesheet:
    <xsl:param name="Customer" />

    4. Then you can decide the location within XSL where to show the value:
    <xsl:value-of select="$Customer" />

    ReplyDelete