How to: Create a Custom Document Library
Posted
Wednesday, June 11, 2008 8:53 AM
by
CoreyRoth
Not too long ago, I talked about how to remove the Explorer View from a document library. As part of the post, I mentioned that I would post how to build a custom document library in the near future. That is what I am covering today. I really don't know what the best practice is on how to create a document library, I am just going to show you how I have done it in the past.
Usually, the way I start is by taking a copy of the existing builtin SharePoint DocumentLibrary feature and add it to a new Visual Studio solution. Typically I would put this in a 12 hive folder (i.e. TEMPLATE\FEATURES). The next step is I rename the DocumentLibrary feature folder (i.e. CustomDocumentLibrary). The next thing you will want to do is edit the Feature.xml file. We have to make some basic changes so that this is considered a new feature. Therefore, you will want to pick a new GUID for the Id and give it a new Title and Description. You may also want to change Hidden to False so that you can activate it and deactivate it through the UI.
Next, we need to edit the ListTemplates/DocumentLibrary.xml file. This file defines the list template itself. The first thing you need to change is the Type. The Type number for a Document Library is 101. It is recommended that user defined list templates start with 10000, so pick any number in that range that isn't used.
The next file you need to modify is the DocLib/Schema.xml file. On the root List element, you will want to change at the minimum the Title element. You may also want to set EnableContentTypes to true if you want to use custom content types. Set FolderCreation to false if you have a custom folder type and set VersioningEnabled to true if you want versioning enabled in the document library. The Url property I believe is the default URL that you document library will use when created. It is specified as a relative path (usually just the folder name). Here is what a typical root list element looks like.
<List xmlns:ows="Microsoft SharePoint" Title="My Custom Document Library" Direction="$Resources:Direction;" Url="My Custom Documents" BaseType="1" xmlns="http://schemas.microsoft.com/sharepoint/" EnableContentTypes="True" FolderCreation="false" VersioningEnabled="true">
I have been leaving the BaseType attribute set to 1 and things have been working fine. However, I have a feeling that it should probably be set to 101 so that Document Library is the base type. In the MetaData section, you can override the folder and document content types. I discussed how to properly do that in this post. You just need to reference the content types you are using and then also add Field elements for each custom site column you are using in those content types. Again, the post mentioned above describes how to do that. The Scema.xml file is also where you can remove (or create) different views for your document library. This is where you would go to remove the explorer view that I mentioned a while back.
Once you have made these changes, you are ready for deployment. There are other files in the DocLib folder, but I typically don't mess with them. Your custom document library can be deployed by copying out the feature and using stsadm or you can create a solution file for it. Once the feature is activated you are ready to create instances of your new document library. Also note that the Visual Studio Extensions for SharePoint can also create a lot of these files for you, but you will still need to go through and customize your various XML files.