AnswerBun.com

Sharepoint problem creating two views for top level and folder level

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)

  1. 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.

  2. 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

Views—————–Show In———-Default View

All Documents———–Top-Level—————–Yes

FolderView—————Folder——————-Yes

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

Any ideas?

SharePoint Asked by SP2010_ssz on July 28, 2020

1 Answers

One Answer

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

Add your own answers!

Related Questions

Add QuickLinks Webpart by using pnp powershell

1  Asked on October 26, 2021 by abdelkader_ak

     

Reading configuration at runtime

1  Asked on October 25, 2021 by milind

         

what went wrong with my sharepoint server deployment?

0  Asked on October 25, 2021 by sharepointadmin

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.