Enabling ECB menu on a custom column in SharePoint 2010 (Part 2)

OK. In Part 1, I’ve explained the problem of “Edit in undefined” when you enable the ECB menu on a field in the document library. To workaround this problem, we have to go back to the way we used for MOSS. In MOSS, when we want to enable the ECB menu on a field, we have to define a custom field with the Computed field type and add references to two special fields, _EditMenuTableStart and _EditMenuTableEnd. We usually need to add another reference to a field from which the value of the custom field comes. So here in SharePoint 2010, let’s do the same thing.

The following is the definition of my custom field. I’ve highlighted the attributes and values which you may pay attention to. In this field, I want it to have the same value of Title so I add a reference to Title. Note in SharePoint 2010 now we use _EditMenuTableStart2 instead of _EditMenuTableStart. To make things simple, I also create a content type to use this custom field.

 <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="https://schemas.microsoft.com/sharepoint/">
    <Field ID="{566F83FF-CCFE-4f7a-BE3C-332169E040B8}"
        Group="Morpheus Columns"
        ReadOnly="TRUE"
        Type="Computed"
        Name="TitleEx"
        StaticName="TitleEx"
        DisplayName="TitleEx"
        DisplayNameSrcField="Title"
        ClassInfo="Menu"
        AuthoringInfo="(with edit menu)"
        SourceID="https://schemas.microsoft.com/sharepoint/v3">
        <FieldRefs>
            <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" 
                      Name="Title" />
            <FieldRef ID="{1344423c-c7f9-4134-88e4-ad842e2d723c}" 
                      Name="_EditMenuTableStart2" />
            <FieldRef ID="{2ea78cef-1bf9-4019-960a-02c41636cb47}" 
                      Name="_EditMenuTableEnd" />
        </FieldRefs>
        <DisplayPattern>
            <FieldSwitch>
                <Expr>
                    <GetVar Name="FreeForm"/>
                </Expr>
                <Case Value="TRUE">
                    <Field Name="Title"/>
                </Case>
                <Default>
                    <Field Name="_EditMenuTableStart2"/>
                    <Field Name="Title"/>
                    <Field Name="_EditMenuTableEnd"/>
                </Default>
            </FieldSwitch>
        </DisplayPattern>
    </Field>
  <!-- Parent ContentType: Document (0x0101) -->
  <ContentType ID="0x010100922fc480f3ae4beba09c8eb3aec9530a"
               Name="TitleEx"
               Group="Morpheus CTypes"
               Description="A content type with an ECB menu enabled field. "
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
        <FieldRef ID="{fa564e0f-0c70-4ab9-b863-0177e6ddd247}" 
                  Name="Title" Required="TRUE" />
        <FieldRef ID="{566F83FF-CCFE-4f7a-BE3C-332169E040B8}" 
                  Name="TitleEx" Required="TRUE" />
    </FieldRefs>
  </ContentType>
</Elements>

Now if we deploy and use the content type in a document library, my field, TitleEx, is actually displayed as below. There is no value displayed, although there is a value in the Title field.

image

The reason why there is nothing in this field is the same as what I mentioned in my previous post: SharePoint 2010 now is using XSLT instead of <DisplayPattern> to render the field on the list view. Although I defined <DisplayPattern> for my field, it will never be used. We have to create a custom XSLT to render it.

The following is the XSLT stylesheet I used for my field.

 <?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:x="https://www.w3.org/2001/XMLSchema"
                xmlns:d="https://schemas.microsoft.com/sharepoint/dsp"
                version="1.0"
                exclude-result-prefixes="xsl msxsl ddwrt"
                xmlns:ddwrt="https://schemas.microsoft.com/WebParts/v2/DataView/runtime"
                xmlns:asp="https://schemas.microsoft.com/ASPNET/20"
                xmlns:__designer="https://schemas.microsoft.com/WebParts/v2/DataView/designer"
                xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:SharePoint="Microsoft.SharePoint.WebControls"
                xmlns:ddwrt2="urn:frontpage:internal">

    <xsl:template match="FieldRef[@Name='TitleEx']" mode="Computed_body">
        <xsl:param name="thisNode" select="." />
        <xsl:param name="ShowAccessibleIcon" select="0"/>
        <a onfocus="OnLink(this)" href="{$FORM_DISPLAY}&amp;ID={$thisNode/@ID}" 
           onclick="GoToHistoryLink(this, {$thisNode/@_UIVersion});return false;" 
           target="_self">
            <xsl:call-template name="LinkTitleValue">
                <xsl:with-param name="thisNode" select="$thisNode"/>
                <xsl:with-param name="ShowAccessibleIcon" select="$ShowAccessibleIcon"/>
            </xsl:call-template>
        </a>
        <xsl:if test="$thisNode/@Created_x0020_Date.ifnew='1'">
            <xsl:call-template name="NewGif">
                <xsl:with-param name="thisNode" select="$thisNode"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

Now there is a value shown in my field, and it is the same as the value of Title.

image

The final step is to edit TitleEx in SharePoint Design and enable Show List Item Menu on it. After that, we will be able to see the ECB menu on this field. The result is below. It shows the “Edit in Microsoft Word” correctly.

image

CTypeTitleEx.zip