User Tools

Site Tools


notes:sharepoint2010:spview

SPView in SharePoint

Add fields to the default view of a list:

// 'list' is of type SPList (or SPDocumentLibrary)
SPView view = list.DefaultView;
view.ViewFields.Add("Field1");
view.ViewFields.Add("Field2");
view.Update();

Create two views for a single library (a LibaryView and a FolderView):

// 'lib' is an instance of SPDocumentLibrary
 
// Prepare fields for LibraryView.
StringCollection fields = new StringCollection();
fields.Add("Field1");
fields.Add("Field2");
fields.Add("Field3");
 
// Create LibraryView.
lib.Views.Add("LibraryView", fields, String.Empty, 50, true, false, SPViewCollection.SPViewType.Html, false);
SPView libraryView = lib.Views["LibraryView"];
 
libraryView.DefaultView = true;
libraryView.MobileDefaultView = true;
libraryView.DefaultViewForContentType = true;
libraryView.ContentTypeId = SPBuiltInContentTypeId.RootOfList;
libraryView.Update();
 
// Prepare fields for FolderView.
fields.Clear();
fields.Add("Field4");
fields.Add("Field5");
fields.Add("Field6");
 
// Create FolderView.
lib.Views.Add("FolderView", fields, String.Empty, 50, true, false, SPViewCollection.SPViewType.Html, false);
SPView folderView = lib.Views["FolderView"];
 
folderView.DefaultView = true;
folderView.MobileDefaultView = true;
folderView.DefaultViewForContentType = true;
folderView.ContentTypeId = SPBuiltInContentTypeId.Folder;
folderView.Update();
 
lib.Update();
notes/sharepoint2010/spview.txt · Last modified: 2016/12/06 by admin