Archive for November 13, 2015

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<string> 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<XElement> fields = from el in root.Elements(ViewFields).DescendantNodes().OfType<XElement>()
                                    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<XElement>()
                    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! 🙂


Get every new post delivered to your Inbox

Join other followers: