Programmatically create a view (custom view) of a list and change the view
If you want to programmatically create a view of a list and if you want to change the existing view of a ListViewWebPart to the custom view you can develop it by referring the following code sample.
code snippet for creating a custom view
=====================================
SPSite oSite = new SPSite([Site URL]);// [Site URL] change it to your sharepoint site URL
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["shared documents"];
SPViewCollection oViewCollection = oList.Views;
string strViewName = "MyCustomView";
System.Collections.Specialized.StringCollection viewFields =
new System.Collections.Specialized.StringCollection();
viewFields.Add("Name");
viewFields.Add("Type");
string query = "<Where><Eq><FieldRef Name=\"Name\"/>" +
"<Value Type=\"Text\">mysample</Value></Eq></Where>";// here you can filter your items using the selected
item in the dropdownlist
oViewCollection.Add(strViewName, viewFields, query, 100, true, false);
oWeb.Update();
Now refresh the list in UI and then you can see the “MyCustomView” in the selection list of views.
The below code will change the current view of list to our custom view. You can use the below code to change the view programmatically.
code snippet for changing the view
======================================
try
{
SPSite oSite = new SPSite([Site URL]);// change it to your sharepoint site URL
SPWeb oWeb = oSite.OpenWeb();
SPList oList = oWeb.Lists["shared documents"];
SPLimitedWebPartManager oWebPartManager = oWeb.GetLimitedWebPartManager("Shared%20Documents/MyCustomPage.aspx", PersonalizationScope.Shared);// change the wepart page absolute URL to your webpart page URL
foreach (System.Web.UI.WebControls.WebParts.WebPart oWebpart in oWebPartManager.WebParts)
{
if (oWebpart.GetType().Name == "ListViewWebPart")
{
ListViewWebPart myWebPart = (ListViewWebPart)oWebpart;
SPView doclibview = oList.Views[“MyCustomView”];
Guid oGuid = new Guid(myWebPart.ViewGuid);
SPView oWebPartView = oList.Views[oGuid];
oWebPartView.ViewFields.DeleteAll();// deleting the existing view fields for adding new one
foreach (string strField in doclibview.ViewFields)
{
oWebPartView.ViewFields.Add(strField);
}
oWebPartView.Query = doclibview.Query;
oWebPartView.RowLimit = doclibview.RowLimit;
oWebPartView.ViewEmpty = doclibview.ViewEmpty;
oWebPartView.ViewFooter = doclibview.ViewFooter;
oWebPartView.ViewHeader = doclibview.ViewHeader;
oWebPartView.Scope = doclibview.Scope;
oWebPartView.GroupByFooter = doclibview.GroupByFooter;
oWebPartView.GroupByHeader = doclibview.GroupByHeader;
oWebPartView.Update();
myWebPart.ViewGuid = oWebPartView.ID.ToString("B").ToUpper();
myWebPart.Visible = true;
myWebPart.Title = "My title";
oWebPartManager.SaveChanges(myWebPart);
break;
}
}
oWeb.Update();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
Comments
Anonymous
March 14, 2008
PingBack from http://msdnrss.thecoderblogs.com/2008/03/15/programmatically-create-a-view-custom-view-of-a-list-and-change-the-view-2/Anonymous
September 29, 2008
The 1st half of the code works perfectly fine but would you mind helping me with where should I write "code snippet for changing the view". Also, Does "Shared Documents/MyCustomPage.aspx" has a physical location? If yes, what it is?Anonymous
September 29, 2008
"code snippet for changing the view" I have tested this code in a .Net windows based application. If you want to plug it in an menu item then you can do it. Let me know what is your exact requirement then I can suggest the best way to put it. All the contents of the document library are storing in sharepoint content DB. You can also open those files in a windows explorer using WebDav. Actions --> open with windows explorer.Anonymous
September 29, 2008
Thanks for your reply. My exact requirement is to create a web based sharepoint customized view to display the list data that is displayed in the document library into a datagrid.Anonymous
September 30, 2008
Can you please share the dummy windows application that you created with me.If yes can you please email me at gunjana@gmail.comAnonymous
October 01, 2008
I am so sorry. Unfortuanately I don't have that sample right now. You can develop one by simply copying this code.Anonymous
November 12, 2008
We can achieve same thing by setting following property: webPart.SuppressWebPartChrome = true;Anonymous
March 04, 2009
How can we programmatically create a styledd ttsheetAnonymous
March 16, 2009
While testing "code snippet for creating a custom view", I am getting same view twice which we have created programmatically. But I dont want that to be happen, any suggestions ?Anonymous
December 17, 2009
Hi, Im new to Sharepoint.Please help!! Is it possible to create a view, that displays all documents based from a document libraries? Requirement is like, I have 4 document libraries where i upload documents. While uploading the document there is a custom dropdown column named Group.And user has to select one from this dropdown. In the new View , all the document should be displayed based on the data in the group. Is it possible to do so?Anonymous
March 31, 2011
how to update default view of calender and assign filter programatically??? i am doing this... SPView view = oList.DefaultView; String query = "<Where><Eq><FieldRef Name='Room_x0020_Name' /><Value Type='Text'>IT Conference Room</Value></Eq></Where>"; view.Query = query; view.Update(); but it is not updating the default view of calender...please help....thanx....Anonymous
May 06, 2012
is there a way to add a column to a view with a link to the edit menu? i've tried "Name (linked to document with edit menu)" but that doesn't work...Anonymous
December 11, 2012
Instead of view.Query = query; try spquery Query = "Something"; view.Query = Query.query;Anonymous
February 11, 2013
I'm always impressed just what you Premier Engineers choose to blog. This was very useful.Anonymous
December 28, 2014
Hi Its giving me error at oWebPartManager.SaveChanges(myWebPart); exception has been thrown by the target of an invocation Please Help