Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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);
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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