0

I am creating a view from code behind. The view contains Title column for which I would like to be a link to open the list item and have a drop-down menu.

I know that this is done by setting <FieldRef Name='Title' ListItemMenu='TRUE' />, but how can I achieve the same thing from code behind?

Here is the code for creating a view:

private SPView CreateView(SPList list, string title, string[] columnDisplayNames)
{
    StringCollection columns;
    string columnInternalName;
    SPViewCollection views;
    SPView view;

    columns = new StringCollection();
    foreach (string columnDisplayName in columnDisplayNames)
    {
        columnInternalName = this.GetListColumnInternalName(list, columnDisplayName);
        columns.Add(columnInternalName);
    }

    views = list.Views;
    view = views.Add(title, columns, string.Empty, 30, true, true);
    view.Update();

    for (int i = views.Count - 2; i >= 0; i--)
    {
        views.Delete(views[i].ID);
    }

    return view;
}

Note: The accepted answer in this question is false, as the Title field that has ListItemMenu is LinkTitle (internal name) and using that column throws an exception.

  • Did you get it working? – Hardik Jun 15 '16 at 12:35
0

You can use below mention code. It is used to show Context menu in other column apart from Title Column in SharePoint List.

SPList list = site.RootWeb.Lists["Tasks"]; //Change the list name here
SPField field = list.Fields["Priority"]; //Change the column name here
field.ListItemMenu = true;
field.ListItemMenuAllowed = SPField.ListItemMenuState.Required;
field.Update();
list.Update();

You can replace the column name with your column name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.