Friday, September 19, 2014

Creating two different default views for SharePoint List - part 2

In this post, I will explain how to create two different default views for a SharePoint list programmatically. In previous post (here) I wrote how to create two default views for a list using SharePoint's GUI and this is the problem described in that post:


Let's say you have one Document Library in SharePoint and in this library you have created custom Content Type called "DocCase", which is derived from Content Type "Folder" and it has its own custom metadata columns. "DocCase" is a container for documents, i.e. every document that is uploaded to this library is uploaded to one "DocCase". It is actually a Folder for my documents, but with my custom columns (metadata).



But, Content Type "DocCase" and documents that are uploaded to that "DocCase" have different metadata. I would like to set two different views (to show different metadata) for each of these Content Types.



NOTE: I am using "Doc Case" Content Type just as an example. This process of creating two default views can be applied to ordinary "Folder" Content Type, or any other Content Type that inherits "Folder" Content Type.


I created a simple Console Application in Microsoft Visual Studio 2012, added Microsoft.SharePoint.dll to its References and pasted following code in Main function:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.SharePoint;
using System.Collections.Specialized;

namespace SharePointConsoleApplication3 {
  class Program {
   static void Main(string[] args) {
      using (SPSite site = new SPSite("http://dev/sites/test")) {
         using (SPWeb web = site.OpenWeb()) {
                    
            SPList newList = web.Lists["Documents"];

            // create view for top-level folder
            StringCollection viewFields = new StringCollection();
            viewFields.Add("LinkFilename");
            viewFields.Add("Client Address");
            viewFields.Add("Client ID");
            SPView newView = newList.Views.Add("DocCase view", viewFields, "", 100, true, true);
            newView.ContentTypeId = new SPContentTypeId("0x012001");//root of list
            newView.Update();

            // create view for documents
            StringCollection viewFields2 = new StringCollection();
            viewFields2.Add("LinkFilename");
            viewFields2.Add("Author");
            viewFields2.Add("Created");
            viewFields2.Add("Editor");
            viewFields2.Add("Modified");
            SPView newView2 = newList.Views.Add("Docs", viewFields2, "", 100, true, false);
            //here you paste ID of your Content Type
            newView2.ContentTypeId = new SPContentTypeId("0x01200077A411909BB3FD4D8496482CB376DA3A");
            newView2.DefaultViewForContentType = true;
            newView2.Update();
         }
      }
   }
  }
}




Now, when you go to your list (Documents in my case), default view "DocCase view" opens and shows our "DocCases" with their columns (Client Address, Client ID...).




But, when you click on one of the "DocCases" ("My New Doc Case"), it switches to the second view - "Docs" - and you can see that different columns appear (Created By, Created, Modified, Modified By...).





In my next (here) post, I will show how to do the exact same thing programmatically using List Definition.

1 comment: