Changing default view for ListViewWebPart programmatically.

Hi2all! Recently I faced with problem of changing default view for ListViewWebPart. My ListViewWebPart placed is on the meeting workspace site, on default.aspx page.I need to switch view programmatically to custom. Ok, at first glance not so hard. We must take instance of our webpart and set it property “ViewGuid” equal to custom view id in list, which is associated with webpart. Here is a peace of code:

var file = workspaceWeb.Files["default.aspx"];
var wpMngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
var attendeeListViewWebPart = (ListViewWebPart)wpMngr.WebParts.Cast<WebPart>().FirstOrDefault(w => w.Title.Equals("Attendees"));
var list = workspaceWeb.Lists["Attendees"];
var view = list.Views["Attendees"];
attendeeListViewWebPart.ViewGuid = view.ID.ToString("B").ToUpper();
wpMngr.SaveChanges(attendeeListViewWebPart);

But it not works as expected. I get reference to SPLimitedWebPartManager, find my webpart and try to update ViewGuid, but every time I try to save changes I get error “The specified view is invalid”. Very strange behavior I suppose. I try to solve this issue about 8 hours and I find solution at last. May be it will be surprise for you, but every time when you add ListViewWebPart to a page, it internally creates hidden view in list. This hidden view belong to this webpart, it id generates by webpart and webpart set its own ViewGuid equal to this id. Let’s check it using SharePoint Manager 2010. I create test site collection from blank site and create links list named “dummy”. Let’s go to SharePoint Manager:

You can see that only one view in our “dummy” list. Consider such properties as “Default View”, “Hidden”. Then I add ListViewWebPart with the list to default page:

Two views! New view is hidden and his title is empty. I add even more webparts and for every webpart one hidden view was created.  Why so? Hmmm…May be this is one way to hold webpart-specific views in synchronization with their webparts.
But what actually happens when we change view throw UI (go to edit webpart -> change selected view)? How ListViewWebPart does update its view? It simply copy fields from selected view to its own hidden view. User thinks that he actually switches view.  Not bad magic, how do you think? :).
And we go back to the first question – how to change view programmatically? Not so hard I must admit. All we need – get the reference to view, that is associated with webpart and update this view. This is even simpler. My results code looks like follows:

var file = workspaceWeb.Files["default.aspx"];
var wpMngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
var attendeeListViewWebPart = (ListViewWebPart)wpMngr.WebParts.Cast<WebPart>().FirstOrDefault(w => w.Title.Equals("Attendees"));
var list = workspaceWeb.Lists["Attendees"];
var view = list.Views.Cast<SPView>().FirstOrDefault(w => w.ID.ToString("B").Equals(attendeeListViewWebPart.ViewGuid, StringComparison.OrdinalIgnoreCase));
view.ViewFields.DeleteAll();
view.ViewFields.Add(<some field>);
....
view.Update();

And the last question – what if we want first to add ListViewWebPart and assign specific view? One way I found – add webpart to a page, and then update view using method, mentioned above.

var file = web.Files["default.aspx"];
var wpMngr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
var list = web.Lists["Attendees"];
var newWP = new ListViewWebPart
				{
					Title = "hi there",
					ListName = list.ID.ToString("B").ToUpper(),
					ListId = list.ID,
				};
wpMngr.AddWebPart(newWP, "Right", 0);
//update view here

What is you might note – when you create ListViewWebPart you don’t need to set ViewGuid property – it will be generated and set internally behind the scene. If you try to set it, you are got an error. I also try method view = list.GetUncustomizedViewByBaseViewId(2); newWP. ListViewXml = view.HtmlSchemaXml
But it didn’t work for me. All times that I create ListViewWebPart it set its view to default list view.