I have a doc library which is using a content type. This doclib has subfolders. The subfolders have word / excel documents.
My requirement is two views (both default views)
when you click on the library the folders underneath show up as a list as usual. for that, edit the “All Documents” view and change the folders setting Show this view = In top level folder.
And then I need a different view when any folder is clicked. this view will have a group on one of the column, and in the folders setting under “Show this view:”, “In folders of content type: Folder” should be selected, also mark this view as default also.
When I do this manually, it works fine as per Need a different views for Folders & Files in Document Library
So basically the end result that I need is two views as below
when I use code, it is not working.
Below is my code:
SPSite site = properties.Feature.Parent as SPSite; SPWeb web = site.RootWeb; SPDocumentLibrary lib = web.Lists["libraryName"] as SPDocumentLibrary; StringCollection strViewFields = lib.Views["All Documents"].ViewFields.ToStringCollection(); string viewQuery = @" <GroupBy Collapse=""FALSE"" GroupLimit=""100""> <FieldRef Name=""AColumn"" Ascending=""TRUE"" /> </GroupBy>"; var folderView = lib.Views.Add("FolderView", strViewFields, viewQuery, 100, true, false); //folderView.ContentTypeId = new SPContentTypeId("0x0120D5"); //folderView.ContentTypeId = new SPContentTypeId("0x0120"); folderView.ContentTypeId = SPBuiltInContentTypeId.Folder; folderView.DefaultView = true; folderView.Update(); var allDocumentsView = lib.Views["All Documents"]; allDocumentsView.ContentTypeId = new SPContentTypeId("0x012001"); //"In the Top level folder" allDocumentsView.DefaultView = true; allDocumentsView.Update(); lib.Update();
But when I run this code, go to my document library’s settings, under Views… I see the All Documents View as default and FolderView is not default and “Show In column” is empty.
The FolderView although it gets created but it does not show as default and does not show the folder setting. When I click on the Folderview and hit OK button without any changes, the folder setting now magically shows up as “Show In” Folder.
Is this a bug in my code? Can any one please help?
Basically I am trying to do this in code as per this!
Create the view to show at the root set it as default view
Create the view to show inside folders set it as default view, but also down in the
folders section set Show this view to In folder of content type:
<Your folder content type>
Go back to the root view and change Show this view to In the top-level folder
Your second view declaration overrides the first view declaration when you set the default view, so each time you call the
.DefaultView = true; you set a default view for the list and the previous statement is overriden.
To make the view be default on the root folder you use the content type, which is the same case as when setting the default view for other folders as well, here's how to do it for the root folder:
yourView.ContentTypeId = new SPContentTypeId("0x012001");
In this case, you shouldn't use
.DefaultView property as it's general for the root folder and subfolders.
Answered by user19105 on July 28, 2020
2 Asked on January 29, 2021 by tejas-thite
2 Asked on January 27, 2021 by antra-sharma
1 Asked on January 26, 2021 by mark-l
2 Asked on January 26, 2021 by jackie
1 Asked on January 26, 2021
1 Asked on January 25, 2021 by user7268
1 Asked on January 24, 2021 by fmh
0 Asked on January 23, 2021 by sandy-hanna
0 Asked on January 22, 2021 by pepozzo
1 Asked on January 22, 2021 by user31823
2 Asked on January 21, 2021 by maurizio
4 Asked on January 19, 2021 by extractor
1 Asked on January 17, 2021 by shahid
1 Asked on January 12, 2021 by aquen
3 Asked on January 11, 2021
1 Asked on January 11, 2021 by leroy
1 Asked on January 11, 2021
2 Asked on January 8, 2021 by user43231
Get help from others!