Tuesday 10 November 2009

Adding the Edit Control Block (ECB) to an additional/different list column

This is how I add the ECB menu to a different column. All changes are made within the context of my list's Schema.xml file.

To start, I already have a custom text field defined within the Fields element:

<Field
Description="$Resources:Field_Caption_Description;"
DisplaceOnUpgrade="TRUE"
DisplayName="$Resources:Field_Caption_DisplayName;"
Group="$Resources:Group_DefaultName;"
ID="{1ac83cea-5b25-4e2f-ae43-5116e005ca97}"
Name="Caption"
Type="Text"
/>

To this I add a new computed field that references the existing field:

<!-- Edit Control Block (ECB) Context Menu -->
<Field
AuthoringInfo="(with menu)"
ClassInfo="Menu"

DisplaceOnUpgrade="TRUE"
DisplayName="$Resources:Field_Caption_DisplayName;"
DisplayNameSrcField="Caption"
ID="{09868F6C-7419-4db3-9C1A-434598ABE010}"
Group="$Resources:Group_DefaultName;"
Name="CaptionContextMenu"
ReadOnly="TRUE"
Type="Computed"
>
<FieldRefs>
<!-- First FieldRef points to parent field -->
<FieldRef ID="{1ac83cea-5b25-4e2f-ae43-5116e005ca97}" Name="Caption" />

<FieldRef ID="{3c6303be-e21f-4366-80d7-d6d0a3b22c7a}" Name="_EditMenuTableStart" />
<FieldRef ID="{2ea78cef-1bf9-4019-960a-02c41636cb47}" Name="_EditMenuTableEnd" />
</FieldRefs>
<DisplayPattern>
<Field Name="_EditMenuTableStart" />
<HTML><![CDATA[<a onfocus="OnLink(this)" href="]]></HTML>
<URL />
<HTML><![CDATA[" ONCLICK="GoToLink(this);return false;" target="_self">]]></HTML>
<!-- Points to parent field -->
<Field Name="Caption" />

<HTML><![CDATA[</a>]]></HTML>
<Field Name="_EditMenuTableEnd" />
</DisplayPattern>
</Field>

Note DisplayNameSrcField points to the original field, as does the <Field> element in the DisplayPattern section.

Finally in my view's ViewFields element, I modify the FieldRef pointing to my original field to point to my ECB column:

<FieldRef Name="CaptionContextMenu" />

A few things to note:

  • When I first tried this with the original field set as type HTML, it didn't work; I ended up changing the field to a text field. I haven't tried other field types.
  • /_layouts/sitemanager.aspx presents the view in a similar but slightly different way to that presented by the list itself (e.g. /Lists/MyList/AllItems.aspx); Site Manager tends to always add the ECB on the second column whereas the latter tends to do a better job doing what you tell it to do. [Update: I use the DocIcon field in position #1 to work around this issue... it shows up as a little document icon with a column name of Type--which I haven't been able to hide. It's clickable in the AllItems.aspx-style view at least so not completely useless and it obviously shoves the ECB to a potentially meaningful field in the second position (you can alternatively use ID but this may not not make sense if you're ordering list items).]

7 comments:

  1. hi..
    this post is amazing..

    But i want the field to be as a custom field type rather than in custom list defination. please let me know how it can be done.

    ReplyDelete
  2. @Kesava - good questiong but I'm not sure how that would be accomplished. Have you tried adding the ECB bits to your field def? I think I did, if memory serves, and it didn't work. I suppose the ECB is more about the list and not the field (arguably) but let me know if you have any luck!

    ReplyDelete
  3. Hi have you managed to get this working on a SP2010 site ? Ive tried to do this, the field appears in the view etc but the content of the field and drop down menu doesnt for me. Ive also tried the ps1 script at http://ilovesharepoint.codeplex.com/releases/view/14994 on Sp2010 and that too shows the same problem - any thoughts ?

    ReplyDelete
  4. @Anonymous - Sorry, haven't done this with 2010.

    ReplyDelete
  5. Thanks Michael for a useful solution.
    I have implemented the custom field like above but the data in the CaptionContextMenu does not show up which should be the same as Caption. I do see the ECB menu around CaptionContextMenu. I must be missign something. Can you please help me?
    Thanks in advance
    -Arun

    ReplyDelete
  6. Why aren't you using ListItemMenu property?
    Or am I missing something here?
    I did this today. In the elements.xml and schema.xml file of the list definition, where you 'declare' the columns, just add one more attribute - "ListItemMenu = TRUE". It worked for me! :)

    ReplyDelete
  7. I have this working now for the document libraries. It displays the title if there is one, if not it defaults to the file name as usual. It preserves the link to the document opening it in the right program and all of the integration behavior. It wasn't easy, but it's packaged as a WSP now.

    http://www.codeproject.com/Articles/424763/Zen-and-the-art-of-XSLT-rendering-fields-SharePoin

    ReplyDelete

Spam comments will be deleted

Note: only a member of this blog may post a comment.