You may provision list views web parts as any other web part by adding it to Module > File > AllUsersWebPart in the feature’s element manifest. In this case you have to add xml declaration of the web part inside AllUsersWebPart (you may get it if e.g. will add list view web part on the page manually and then will export it to the file on disk). However there is more simple way to provision list view web parts using View element:
1: <Module Name="MyPages" Url="$Resources:cmscore,List_Pages_UrlName;" Path="pages">
2: <File Url="default.aspx" Type="GhostableInLibrary" Level="Published">
3: <View
4: List="Lists/MyList"
5: BaseViewID="3"
6: Name="MyList"
7: WebPartZoneID="RightWebPartZone"
8: WebPartOrder="0" />
9: ...
10: </File>
11: </Module>
In this example we provision list view web part on the default.aspx page into RightWebPartZone. This web part will use list view with BaseViewID=3 for list with url Lists/MyList on the current web site. BaseViewID can be retrieved from query string when you choose create new view of specific type from the list settings of ribbon.
However this web part will use default settings for the selected list view, e.g. it will have default columns and sorting settings. In some cases we may need to modify the list view used by web part, e.g. add new columns, remove columns which were added by default, change sorting settings, etc. In order to do that we need to understand what happens behind the scene when we provision list view web part like this.
If you will check the views for the list for which we provision web part e.g. in Sharepoint Manager and then compare them with list, which was created by the same template, but without provisioned list view web part, you will find that during provisioning web part creates additional view with specified BaseViewID and with Hidden=true. I.e. this view won’t be visible from UI. So if we want to modify this view, we need to get instance of appropriate SPListView object and then manipulate its properties. We can do it e.g. in feature receiver:
1: public class MyFeatureReceiver : SPFeatureReceiver
2: {
3: public override void FeatureActivated(SPFeatureReceiverProperties properties)
4: {
5: ...
6: var list = web.Lists.Cast<SPList>().FirstOrDefault(l => l.Title == "MyList");
7: if (list != null)
8: {
9: var view = list.Views.Cast<SPView>().LastOrDefault(v => v.BaseViewID == "3" && v.Hidden);
10: if (view != null)
11: {
12: view.ViewFields.DeleteAll();
13: view.ViewFields.Add("Title");
14: view.ViewFields.Add("Modified");
15: view.Query = "<OrderBy><FieldRef Name=\"Modified\" Ascending=\"FALSE\" /></OrderBy>";
16: view.Update();
17: }
18: }
19: ...
20: }
21: }
In this example at first we get instance of the list (line 6), then find hidden view with specified BaseViewID and (line 9). After that we delete all columns which were added by default and add columns which we need: Title and Modified. Also we specify sort order: by Modified field descending. After that your web part will show the view with specified settings, instead of default ones.