Saturday, October 4, 2014

Creating two different default views for SharePoint List - part 3


In previous two posts, I showed how to create two default views for a SharePoint list for different Content Types with different metadata. In first post (here) I explained how to do it with SharePoint's GUI and in second post (here) I explained how to do it using SharePoint API, i.e. Server Object Model with C#.

In this post I will do the same thing, but using List Definitions in Visual Studio 2012. In first two posts I only created new views on the existing library (Documents), but this time, I will create new Site Columns, new Content Type and new List Definition and than create new default views for this new list.

First, open Visual Studio and create new project:



On the next screen write the URL to your site and select "Deploy as farm solution":



Next step is to create new Site Columns. In previous posts I created two columns; Client Address and Client ID, so these will be new Site Columns in this post also.

Right mouse click on your project's name in Solution Explorer and select "Add new item". When new screen appears, choose "Site Columns", give it a name and click on "Add" button.



The file Elements.xml will open. Delete existing text from it and paste following text which will add two new columns (Client Address and Client ID) to Site Columns of SharePoint:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">   
  <Field
       ID="{492D3DD2-4B11-4A0C-A7AB-C82F367FCF96}"
       Name="Client Address"
       DisplayName="Client Address"
       Type="Text"
       Required="FALSE"
       Group="Custom Site Columns">
  </Field>
  <Field
       ID="{DE8BDED0-A393-4D2C-B042-8FF028779FEA}"
       Name="Client ID"
       DisplayName="Client ID"
       Type="Text"
       Required="FALSE"
       Group="Custom Site Columns">
  </Field>
</Elements>


Second step is to create new Content Type. Right mouse click on your project's name in Solution Explorer and select "Add new item". When new screen appears, choose "Content Type", give it a name (I named it "DocCase") and click on "Add" button.


Add newly created site columns; "Client Address" and "Client ID".




Now, we need to create new SharePoint List. Right mouse click on your project's name in Solution Explorer and select "Add new item". When new screen appears, choose "List", give it a name (for a List Definition) and click on "Add" button.


On the next screen, write the name of your list instance and select template "Document Library".




Add Content Type (DocCase) that we created in previous step to newly created list.




Next step is to create two default views, which is what this post is all about.

Click on the "Views" tab in List Definition. First, I am going to create view "DocCase view" and add following fields to it: Type, Name, Client Address and Client ID. After that, click on button "Set as default".


Then, I will create second view called "Docs" and add following fields to it: Type, Name, Created By, Created, Modified By and Modified.




After creation of two new views, we need to update Schema.xml file to setup views for different content types in the list. Open Schema.xml file like shown in following image:



Serach for text "DocCase view" and insert ContentTypeID="0x012001" like shown in image. This will define this view for being default view for "Root folder" of SharePoint list.


Then, search for text "Docs" and insert following.
NOTE: Here you paste ContentTypeId of your Content Type, don't just copy this one because it won't work. You can find it in Elements.xml file of the your newly created Content Type – "DocCase" in this project).

ContentTypeID="0x0120002009DCEC46A248998371E3E862196CFE00A613D8CEBE6E4D10BE51CE085548312F" DefaultViewForContentType="TRUE"



Now, when you go to your newly created list in SharePoint, 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...).


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.