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


No comments:

Post a Comment