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!
Comments
Anonymous
June 15, 2008
PingBack from http://stevepietrek.com/2008/06/15/links-6152008/Anonymous
June 22, 2008
Web Content Management TechNet: Administering Web content management TechNet: White paper: End-to-EndAnonymous
August 06, 2008
I have followed exact steps....but views are not being created. :( I am not sure whether this will add custom view into the default View dropdown menu. Any help would be appreciated.Anonymous
September 18, 2008
Thanks. Worked for me. The SetupPath="pagesviewpage.aspx" attribute value was what I needed to get past the file not found exception.Anonymous
September 18, 2008
good to hear that :)Anonymous
November 25, 2008
Thanks for this post! I was stuck on this for hours before I found this post. The SetupPath was my issue as well. Now working just fine!Anonymous
December 22, 2008
good answar. But i have a question that is why we are creating custom list defination whatever we are doing in this , that working also i am doing in custom list of sharepoint env.Anonymous
December 22, 2008
There may be some situation that you want to create custom site defintion and deploy it to the sharepoint server. So, in that case if you want to create a custom site defintion with custom views, then this particular work-around will make it easier.Anonymous
February 27, 2009
Nice to read this blog. I'm running into the similar situation while upgradation. Hope this workaround solves my problem ! Thanks :-)Anonymous
January 13, 2010
One can add the baseview "0" programmatically by scanning through available views as follows: foreach (SPView view in oobList.Views) { if (view.BaseViewID == "0") { listViewWebPart.ViewGuid = view.ID.ToString("B").ToUpper(); break; } }Anonymous
July 26, 2011
I am changing the GUID of the view, as the list definition with custom views will be deployed on other servers as well so this will keep changing. But somehow, it doesn't accepts the GUID given by me which creates problem while assigning this custom view to any webpart such as xslt list view wp.