Add ECB to View Columns

Some code to add the ECB menu to the specified columns of a view:

public void SetEcbColumnOnView(SPView view, List internalNames)
    if (view == null)
        throw new ArgumentNullException("view");

    const string ListItemMenu = "ListItemMenu";
    const string ViewFields = "ViewFields";
    const string AttributeName = "Name";

    XElement root = XElement.Parse(view.GetViewXml());

    // Get all columns that use the ECB menu
    IEnumerable fields = from el in root.Elements(ViewFields).DescendantNodes().OfType()
                                    where el.Attribute(ListItemMenu) != null
                                    select el;

    // Remove the ECB Menu from all columns that currently use it.
    foreach (XElement el in fields)

    if (internalNames.Count > 0)
        //Get the field specified in the parameters
        fields = from el in root.Elements(ViewFields).DescendantNodes().OfType()
                    let nameAttribute = el.Attribute(AttributeName)
                    where nameAttribute != null && internalNames.Any(v => v.Equals(nameAttribute.Value))
                    select el;

        // Add the attribute to the columns to show the ECB menu
        foreach (XElement el in fields)
            var attribute = new XAttribute(ListItemMenu, bool.TrueString);

    // Update the view
    string viewXml = root.ToString(SaveOptions.DisableFormatting);

Code should be pretty self explanatory, happy coding! 🙂

This entry was posted in SharePoint and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *