How to create a custom list with custom views based upon a custom list definition

Once I have got a customer request to create custom lists with custom views, not through custom code but through custom list definition.

 

WOW it will be really a cool stuff to do!!!

They already have a custom list definition and whenever they create a new list based upon that custom list definition it will create a list with a default view which was “All items”. But, they wanted some custom views has to be there by default whenever they create a new list based upon their custom list definition.

If you want to know how we can create a custom list defintion please refer the below MSDN article:

https://msdn.microsoft.com/en-us/library/ms466023.aspx

Now we can see the funny development part…

Here we can talk with a custom list definition by taking an eg: of custom list definition of “LinkList” and its definition will be there in the following location.

Drive:\Program Files\Common Files\Microsoft Shared\web server extensions \12 \ TEMPLATE \ FEATURES \LinksList

If you do the necessary changes after copying and renaming this folder your custom list definition for LinksList is ready. And once you activate the corresponding feature and create new list then you can see that it will show only a single view which will be “All items” by default.

Now, we can see how we can add custom view definition to the schema.xml of our custom view. If we open the schema.xml file then you can see the following tags for the views. View with the BaseViewID=1 is the "All Items" view.

<Views>

<View BaseViewID="0" Type="HTML" OrderedView="TRUE">

+ <ViewHeader>

+ <HTML>

- <![CDATA[ ……

]]>

</HTML>

</ViewEmpty>

- <Query>

- <OrderBy>

<FieldRef Name="Order" Ascending="TRUE" />

</OrderBy>

</Query>

- <ViewFields>

<FieldRef Name="URL" Explicit="TRUE" />

<FieldRef Name="URLNoMenu" />

</ViewFields>

<RowLimit>20</RowLimit>

</View>

<View BaseViewID="1" Type="HTML" OrderedView="TRUE" WebPartZoneID="Main" DisplayName=" $Resources:core,All_Links; " DefaultView="TRUE" SetupPath="pages\viewpage.aspx" ImageUrl=" /_layouts/images/links.png" Url="AllItems.aspx">

…..

</ View></ Views>

Now our next task is creating a new view definition <View></ View> for our custom view.

So now the question is, do I need to write all those definitions inside the schema.xml file L ?

Ans : yes ofcourse, but don’t worry we have an easy way to do it. J.

Though the “easy way” is somewhat tricky it will reduce our task and it will not create any syntactical errors. In order to get a custom view definition, first you have to create a view from UI and then save your list as list template. From the list template you will get the new view’s definition.

Here are the steps:

1. Create an instance of LinksList

      2. Create custom view in the UI

3. Now save the list as List Template.

    3.1 List settings à Permissions and Management à Save List as Template.

4. Download the List template from the List Template Gallery.

    4.1 Site Actions àSite Settings à Galleries à List templates.

     5. Rename the list template file to .cab and open the manifest.xml file

      6. From the manifest.xml we will get the View definition of our custom view. Just copy it and paste

            it in to the schema.xml.

While doing the 6th step you have to remember some points. If you just cross check the custom view definition with the “All Items” view then we can see that some of the attributes were missing in the custom view definition. One important attribute will be SetupPath. You can see it in the highlighted part of my custom view definition below. SetupPath will take a page viewpage.aspx as its value , which is located physically in the following location.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\Pages

If you open this page then you can see a WebPartZone with ID “main”, this webpart zone will include the view pages once we creates it. Thus, WebPartZoneID and SetupPath attributes are mandatory while adding a custom view definition.

Also, we can specify new Url instead of AllItems.aspx like below and then it will show the list in this new Url.

<!--My Custom View Start-->

<View Name="{EAA1BF1B-C838-42CC-BA89-DEE69C1591E3}" WebPartZoneID="Main" Type="HTML" DisplayName="sowmyanview" SetupPath="pages\viewpage.aspx" Url="sowmyan.aspx" Level="1" BaseViewID="2" ContentTypeID="0x" ImageUrl="/_layouts/images/generic.png">

</View>

<!--My Custom View End-->

 

If you didn’t do the above changes then while creating a new instance of your list you will get a "FileNotFoundException" exception. So you have to remember those important points in mind. After completing those modifications, we have to uninstall our custom fetaure or install it forcefully again. Now we can see our custom view whenever we create a new list!