I'm running the following code to create a new view:
SPList docLib = eWeb.Lists["Documents"]; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("EmailFrom"); string query = "<Where><IsNotNull><FieldRef Name=\"EmailFrom\"/></IsNotNull></Where>"; SPView eMailView = docLib.Views.Add("All Emails", viewFields, query, 20, true, false); eMailView.Update(); docView.Update();
When I now inspect my new view it has a BaseViewID of 1. Even if I add another 10 views they all have view ID 1.
Then I'm trying to set the view of my web part on a page with this code:
SPLimitedWebPartManager WebPartManagerUpdate = page.GetLimitedWebPartManager(PersonalizationScope.Shared); WebPartManagerUpdate.AddWebPart(webpart, "TopLeftRow", 1); webpart.ViewId = Int32.Parse(item.ListItems.List.Views["All Emails"].BaseViewID); webpart.ViewGuid = item.ListItems.List.Views["All Emails"].ID.ToString("B").ToUpper(); WebPartManagerUpdate.SaveChanges(webpart);
But I'm only ever getting the default view with the BaseViewId 1 to appear on the page.
I'm guessing that I need to set the BaseViewId to something else than 1. But if there is any other way to get my view to appear on the page then that will be ok as a solution.
I also tried this without setting the ViewID and just the ViewGuid but that doesn't seem to work either.