As you probably know there is possibility to set <Summary view> in ListViewWebPart via UI when you select “Modify Shared Web Part” on your ListViewWebPart:
But is it possible to do it programmatically? I saw many questions on forums how to do it but there were no solution for this problem. I investigated it and found how to set Summary view for ListViewWebPart programmatically. But what is the problem? Here is the regular procedure which you should make when add ListViewWebPart on page programmatically:
Problem is that Summary view is not separate view in the target list, so you can’t get reference on this view and store it’s ID into ViewGuid property of the ListViewWebPart. So there should be some internal mechanism in ListViewWebPart which set this Summary view.
First of all I found a useful post which describes how to remove toolbar from ListViewWebPart: Changing the toolbar on a SharePoint ListViewWebPart. It doesn’t provide solution for setting Summary view programmatically, but I found direction of research – ListViewToolPart class (this is internal class so there is no documentation for it). Exactly this class is responsible for showing “Selected View” and “Toolbar Type” dropdown lists in ListViewWebPart properties. I investigated its CreateChildControls() method view Reflector:
It iterates through all views in target list and adds items in dropdown list. After that it adds <Summary view> list item:
And one thing attracted my attention – when it adds list items which correspond to existing list views it sets list view’s InternalName into Value property of list item:
But when it adds list item for <Summary view> it sets string.Empty to Value of item:
So I made a hypothesis that when user selects some item from UI value of this item is stored into ViewGuid property of web part (I couldn’t see what happens directly because ListViewToolPart.ApplyChanges() method is obfuscated). And I tried to set string.Empty to ViewGuid property of ListViewWebPart when add it on page programmatically:
And it works! After that ListViewWebPart have Summary view immediately after installing it via code. Hope this trick will help you in your work.
Update 2010-09-07: there is more easy way to add ListViewWebPart into publishing page during provisioning: use <View> element (see http://msdn.microsoft.com/en-us/library/ms438074.aspx) inside <File> element into your elementManifest.xml: