Search code examples
sharepointxsltsharepoint-2010

Getting Counted Values of a Multi-Value Lookup Column in DVWP using XSLT


I am in the process of building a "dashboard" where I am using DVWP's and XSLT to show the client things such as total count of tasks, how many open, how many closed etc. similar to this article

My issue is that I have a multi-value lookup column for which I need to get the count of the values, but I am not able to generate any results with what I have tried.

Any suggestions or recommendations on how to accomplish would be great.

So to add to the above as an update:

I am not exactly certain the best approach on how to achieve the desired results. Essentially I have a multi-value lookup column that currently has 20 values. The client has the ability to add new values when needed. So what I am attempting to do is to get a totals type count of the values they have selected for each record.

So let's say for example the lookup colum has five (5) values:

Value 1 Value 2 Value 3 Value 4 Value 5

In the newform.aspx they have the ability to select multiple values (so the could select value 3 and value 5; or value 2, value 4, and value 5 etc etc). In the list view it of course shows the selections as it should. What I am attempting to do is to get a total count of those values.

For example the output would look something like:

Value 1 : 5
Value 2 : 1
Value 3 : 2
Value 4 : 3
Value 5 : 6
Value 3 & 5 : 4

Value 2,4, & 5: 3

I am not certain about the XSLT to develop such a thing as I have not had to do this with such a complex Lookup Column before. Normally I would do someting like below, but this only gets me the basics, and since the values can be combined I am not certain how to approach:

<xsl:template name="dvt_1.body">
    <xsl:param name="Rows"/>
    <xsl:variable name="total1" select="count(/dsQueryResponse/Rows/Row/@MyLookupCol.[contains(.,'1;#Value1')])"></xsl:variable>
                    <xsl:call-template name="cs3_totalRow">
                        <xsl:with-param name="cs3_RowName1">
                            Value 1
                        </xsl:with-param>
                        <xsl:with-param name="cs3_RowValue1">
                            <xsl:value-of select="$total1"/>
                        </xsl:with-param>
                    </xsl:call-template>
            </xsl:template>

            <xsl:template name="totalsRow">
                <xsl:param name="RowName1"></xsl:param>
                <xsl:param name="RowValue1"></xsl:param>
                    <table>
                        <tr>
                            <td>
                                <xsl:value-of select="$cs3_RowName1"/>:
                            </td>
                            <td>
                                <xsl:value-of select="$cs3_RowValue1"/>
                            </td>
                        </tr>
                    </table>

Solution

  • OK; figured it out. You will need to use MUENCHIAN METHOD which utilizes keys in order to generate something of a "distinct group by" list and then use count in your expression and count the keys.

    References used are here and here

    See the code below for details:

    <!--GROUPING USING THE MUENCHIAN METHOD-->
    <!--Add a key as shown below. This is important! Will not "group by" without it-->
    <xsl:key name="YourKeyNameHere" match="Row" use="@YourColumnName"/>
    
       <xsl:template match="/">
          <!--Get your column values look you normally would-->
          <xsl:variable name="cbs_Rows" select="/dsQueryResponse/Rows/Row/@YourColumnName"/>
          <table border="0" width="100%" cellpadding="2" cellspacing="0">
             <tr valign="top">
                <th class="ms-vh" nowrap="nowrap">TheValueName</th>
                <th class="ms-vh" nowrap="nowrap">ValueTotals</th>
             </tr>
             <!--This gets the distinct strings for you.-->
             <xsl:for-each select="//Row[generate-id() = generate-id(key('YourKeyName', @YourColumnName)[1])]">
                <xsl:sort select="@YourColumnName"/>
                <xsl:for-each select="key('YourKeyName', @YourColumnName)">
                    <xsl:call-template name="Rows.RowView" />
                </xsl:for-each>
             </xsl:for-each>
          </table>         
       </xsl:template>
       <!--then build your row view-->
       <xsl:template name="Rows.RowView">
          <xsl:variable name="SortValue" select="ddwrt:NameChanged(string(@YourColumnName), 0)"/>
          <xsl:if test="string-length($SortValue) &gt; 0">
             <tr id="group0{generate-id()}">
                <td>
                   <xsl:value-of select="@YourColumnName"/>
                </td>
                <td>
                    <xsl:value-of select="count(key('YourKeyName', @YourColumnName))"></xsl:value-of>
                </td>
             </tr>
          </xsl:if>
       </xsl:template>