Add New View Using Existing View
In this post I’m going to describe how you can programmatically add a view to SharePoint list or Document Library. You can get this work done by usingSPViewCollection.Add Method (Microsoft.SharePoint), here is a sample code,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| public void createListView(){ SPSite site = new SPSite(""http://sp2010/test""); SPWeb web = site.OpenWeb(); SPList list = web.Lists["Tasks"]; SPViewCollection allviews = list.Views; string viewName = "Test View"; System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection(); viewFields.Add("Title"); viewFields.Add("Priority"); string myquery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>"; allviews.Add(viewName, viewFields, myquery, 100, true, false);} |
Please refer my article on CAML & SPQuery in SharePoint to find out easy way to write CAML quires used in above code.
Add New View Using Existing View
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| public void _createListView(){ SPSite site = new SPSite("http://sp2010/test"); SPWeb web = site.OpenWeb(); SPList list = web.Lists["Tasks"]; SPViewCollection allviews = list.Views; string viewName = "New Test View"; System.Collections.Specialized.StringCollection viewFields = list.Views["Test View"].ViewFields.ToStringCollection(); viewFields.Add("Status"); string myquery = "<Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt></Where>"; allviews.Add(viewName, viewFields, myquery, 100, true, false);} |
No comments:
Post a Comment