When defining a listview webpart in a custom site definition, how can I make sure that the view has the columns that I want it to show?

The standard view shows these column: Title, Assigned To and Modified. What I want it to show is these columns: Title, Due Date.

What's the best way of doing so?


The easiest way is to build the View you want into your list definition. Then in your ListViewWebPart refer it to that view.

In CAML, this will be something like:

<View List="Lists/Tasks" BaseViewID="7" WebPartZoneID="Left" WebPartOrder="5" />

BaseViewID is the identifier for the View in your List Definition.

However, you might want to do this programmatically - for example, if you create the view within your list programmatically too. In that case, you'll need something like:

SPView myView = ...
using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
    using (ListViewWebPart webPart = new ListViewWebPart())
        webPart.Title = "My List";
        webPart.ListName = web.Lists["MyList"].ID.ToString("B").ToUpper();
        webPart.ViewGuid = myView.ID.ToString("B").ToUpper();
        manager.AddWebPart(webPart, "Left", 3);
| improve this answer | |

Your Answer

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