Connected Page Viewer Web Part
August 16, 2007
Adding a Document Library Feature to a Site Definition in WSS V3 / MOSS 2007
August 16, 2007

Creating a Custom Document Library Feature in WSS V3 / MOSS 2007

This is a repost of an article that was lost due to a server crash.

Features are a powerful new set of functionality that MOSS 2007 comes with out of the box. Features allow you to deploy functionality inside your MOSS 2007 portal in a granular and loosely coupled fashion. The Document Library that many of the out of the box site definitions come with in MOSS 2007 is implemented as a feature. Building on this out of the box Document Library Feature you can create your own custom Document Library Features as well.

Feature Basics

To begin, let’s take a look at the directories and files that make up a Feature.

Features are stored on the SharePoint Server at the following location:

C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURES

Each Feature on the SharePoint Server has its own sub directory that is created in the directory listed above.

Inside each Feature sub directory you will find a file named Feature.xml.

The Feature.xml file holds metadata about the Feature.

Document Library Features  

Here is the Feature.xml file that is used to create the DocumentLibrary Feature that comes with MOSS 2007:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>

<!-_LocalBinding –>

<Feature Id=”00BFEA71-E717-4E80-AA17-D0C71B360101″

Title=”$Resources:core,documentlibraryFeatureTitle;”

Description=”$Resources:core,documentlibraryFeatureDesc;”

Version=”1.0.0.0″

Scope=”Web”

Hidden=”TRUE”

DefaultResourceFile=”core”

xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ElementManifests>

<ElementManifest Location=”ListTemplatesDocumentLibrary.xml” />

</ElementManifests>

</Feature>  

The following metadata about the feature is contained in the Feature element in this XML file.

ID: The GUID that uniquely identifies the Feature

Title: The name of the feature. You will see the name of the Feature displayed on the Site Features web page inside the Site Settings section for a given SharePoint Site.

Description: The description of the feature. You will see the description of the Feature displayed on the Site Features web page inside the Site Settings section for a given SharePoint Site.

Version: The version of the Feature.

Scope: Web or Site are acceptable values here. The scope specifies if the Feature can span an entire site collection, or if the Feature will just be used for an individual sub web.

Hidden: TRUE or FALSE are acceptable values here. This setting specifies if the Feature is visible in the list of Features on the Site Features web page mentioned above. You will notice in the XML below that the DocumentLibrary Feature has its Hidden attribute set to TRUE, this is why you do not see the Document Library in the list of Features on the Site Features web page. Therefore the ability to create a Document Library is not able to be turned off by an end user who has access to the Site Features web page.

DefaultResourceFile: This is the name of the Resource file the feature relies on to provide it with additional configuration information. (More on Resource files later in this document).

You will notice the <ElementManifests> element in the Feature.xml file. This element contains the location of another XML file that contains the different <Elements> this Feature implements.

The <ElementManifest> element specifies that the Feature utilizes a file in the ListTemplates sub directory called DocumentLibrary.xml. The path to sub directories and files is a relative path.

Here is the DocumentLibrary.xml file that holds the <Elements> implemented by the DocumentLibrary Feature:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>

<!-_LocalBinding –>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ListTemplate Name=”doclib”

Type=”101″

BaseType=”1″

OnQuickLaunch=”TRUE”

SecurityBits=”11″

Sequence=”110″

DisplayName=”$Resources:core,doclibList;” Description=”$Resources:core,doclibList_Desc;”

Image=”/_layouts/images/itdl.gif”

DocumentTemplate=”101″ />

</Elements>

Notice the <ListTemplate> element? This should look familiar to those who have edited the ONET.XML file in the last version of SharePoint.

The <ListTemplate> element specifies the information needed to make a particular list available on a SharePoint site. The only change to this element you see in this particular example is the addition of the Sequence attribute. As far as I can tell this attribute controls the order of the list on the Create page within a SharePoint site.

Closer inspection reveals some incredibly powerful technology that has been baked into MOSS 2007 – Resource files. This leads us to the topic of Resource files.

Resource Files

The DisplayName and Description attributes have an interesting item in their values. Let’s examine the DisplayName attribute.

The value for this attribute is $Resources:core,docLibList. This value specifies that the doclibList value is read from the core Resource file.

Resource files are stored in the following directory on the SharePoint server:

C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12Resources

Resource files contain key value pairs of information and are used to provide localization capabilities to SharePoint. I believe we will see many developers using Resource files for more than localization in the future because of the power and flexibility they provide.

The $Resources:core part of the value corresponds to the core.en-US.resx resource file.

The docLibList part of the value corresponds to the docLibList <Data> element within the core Resource file.

The XML inside the core resource file that is used to populate the DisplayName and Description attributes of the <ListTemplate> element looks like this:

<Data Name=”doclibList”>

<Value>Document Library</Value>

</Data>

<Data Name=”doclibList_Desc”>

<Value>Create a document library when you have a collection of documents or other files that you want to share. Document libraries support features such as folders, versioning, and check out.</Value>

</Data>

At this time I have not been able to find any documentation regarding these files, so I don’t recommend editing the core resource file, instead, make your own.

Another Benefit Of Features  

In the last version of SharePoint the ONET.XML file was very big and contained a large amount of information. With the advent of Features the ONET.XML file shrinks because Features are used to componetize the contents of the ONET.XML file. As we have seen here, the <ListeTemplate> element, previously found in the <ListTemplates> element inside the ONET.XML file has been moved into the Document Library Feature. Other out of the box MOSS 2007 lists utilize this same approach.

Creating You Own Document Library Feature

Creating you own custom Document Library Feature is a simple task once you figure out all the pieces of the puzzle. Here is a list of tasks that you will need to do in order to create your own custom Document Library Feature.

Step 1: Create a directory for the Feature.

Step 2: (Optional) Create a custom Resource file.

Step 3: Create the Feature.xml file for the Feature.

Step 4: (Optional) Create the <DocumentTemplate> element inside the ONET.XML file if you are using a custom Document Template for your custom Document Library.

Step 5: Create the ListTemplates sub directory under your new Feature directory.

Step 6: Create the CustomDocumentLibrary.xml file for the Feature.

Step 7: Copy the contents of the DocLib sub directory into your new Feature directory.

Step 8: (Optional) If you plan to use a custom Document Template and you performed Step 4 above Create a custom Document Template.

Step 9: (Optional) Copy the custom Document Template you created that will server as the default Document Template for your new custom Document Library to the SharePoint server.

Step 10: (Optional) Add the Feature to the site definition you want the feature to be installed on by default when the site is created. (Please see the Adding a Document Library Feature to a Site Definition in WSS V3 / MOSS 2007 article.)

Step 11: Register the Feature with a SharePoint site via the STSADM.EXE command line utility.

Step 12: Reset IIS.

Step 13a: (Assuming you did not do Step 10) Browse to the SharePoint site you registered the feature with. Browse to the Create web page and the custom Document Library will be available in the list of items to create.

Step 13b: (Assuming you did perform Step 10) Create a new SharePoint site with the site definition you edited in Step 10. Browse to the new site to see the custom Document Library already created for you.

*Note: In both cases the custom Document Library Feature will be available on the site.

Step 1: Create a directory for the Feature.

Create a new directory for your Feature in the C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURES directory.

This example will use the directory named CustomDocumentLibrary.

Step 2: (Optional) Create a custom Resource file.  

Take advantage of the ability to store all your display settings and other items whose values may change frequently in your own Resource file by creating one.

Create a new file named customDocumentLibrary.en-US.resx in the C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12Resources directory.

Inside the file put the following XML:

<?xml version=”1.0″ encoding=”utf-8″?>

<!-_lcid=”1033″ _version=”12.0.4017.1004″ _dal=”1″ –>

<!-_LocalBinding –>

<root>

<Data Name=”customDocumentLibrary_Title”>

<Value>Custom Document Library</Value>

</Data>

<Data Name=”customDocumentLibrary_Folder”>

<Value>Custom Document Library</Value>

</Data>

<Data Name=”customDocumentLibraryDisplayName”>

<Value>Custom Document Library</Value>

</Data>

<Data Name=”customDocumentLibraryDescription”>

<Value>Create a custom document library when you have a collection of documents or other files that you want to share. Custom document libraries support features such as folders, versioning, and check out.</Value>

</Data>

</root>

Save the file.

Step 3: Create the Feature.xml file for the Feature.  

Create a GUID for your feature. You will need to insert this GUID into some of the XML in this document where you see <YOUR GUID HERE>.

You can create a GUID inside VS.NET 2005 or run the following SQL SELECT statement to return a new GUID. SELECT NewID()

Create a file named Feature.xml and place it in the CustomDocumentLibrary directory you just created.

If you performed Step 2 put the following XML in the file:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>

<!-_LocalBinding –>

<Feature Id=”<YOUR GUID HERE>”

Title=”$Resources:customDocumentLibrary,customDocumentLibrary_Title;”

Description=”$Resources:customDocumentLibrary,customDocumentLibraryDescription;”

Version=”1.0.0.0″

Scope=”Web”

Hidden=”FALSE”

DefaultResourceFile=”customDocumentLibrary”

xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ElementManifests>

<ElementManifest Location=”ListTemplatesCustomDocumentLibrary.xml” />

</ElementManifests>

</Feature>

If you did not perform Step 2 put the following XML in the file:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.4017″ _dal=”1″ –>

<!-_LocalBinding –>

<Feature Id=”<YOUR GUID HERE>”

Title=”Custom Document Library”

Description=”Create a custom document library when you have a collection of documents or other files that you want to share. Custom document libraries support features such as folders, versioning, and check out.”

Version=”1.0.0.0″

Scope=”Web”

Hidden=”FALSE”

DefaultResourceFile=”core”

xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ElementManifests>

<ElementManifest Location=”ListTemplatesCustomDocumentLibrary.xml” />

</ElementManifests>

</Feature>

Remember to replace <YOUR GUID HERE> with the GUID you created!

Save the file.

Step 4: (Optional) Create the <DocumentTemplate> element inside the ONET.XML file if you are using a custom Document Template for your custom Document Library.

 Open the ONET.XML file for a custom site definition you have created (Please see the Creating a custom Site Definition in MOSS 2007 article for more details on how to create a custom site definition in MOSS 2007.)

Inside the ONET.XML file create a new <DocumentTemplate> element inside the <DocumentTemplates> element.

If you performed Step 2 use the following XML:

<DocumentTemplate

Path=“SAMPLE”

DisplayName=”$Resources:customDocumentLibrary,customDocumentLibraryDisplayName;”

Type=”4000″

Default=”TRUE”

Description=”$Resources:customDocumentLibrary,customDocumentLibraryDescription;”>

<DocumentTemplateFiles>

<DocumentTemplateFile Name=”doctempwordcustom.dot”

TargetName=”Forms/template.doc”

Default=”TRUE”/>

</DocumentTemplateFiles>

</DocumentTemplate>

If you did not perform Step 2 use the following XML:

<DocumentTemplate

Path=”SAMPLE”

DisplayName=”Custom Document Library”

Type=”4000″

Default=”TRUE”

Description=” Create a custom document library when you have a collection of documents or other files that you want to share. Custom document libraries support features such as folders, versioning, and check out.”>

<DocumentTemplateFiles>

<DocumentTemplateFile Name=”doctempwordcustom.dot”

TargetName=”Forms/template.doc”

Default=”TRUE”/>

</DocumentTemplateFiles>

</DocumentTemplate>  

Step 5: Create the ListTemplates sub directory under your new Feature directory.  

In the C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURESCustomDocumentLibrary directory create a directory named ListTemplates.

Step 6: Create the CustomDocumentLibrary.xml file for the Feature.  

Create a file named CustomDocumentLibrary.xml and place it in the ListTemplates directory you just created.

If you performed Step 2 use the following XML:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.3820″ _dal=”1″ –>

<!-_LocalBinding –>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ListTemplate

Name=”doclib”

Type=”4000″

BaseType=”1″

OnQuickLaunch=”FALSE”

SecurityBits=”11″

DisplayName=”$Resources:customDocumentLibrary,customDocumentLibraryDisplayName;”

Description=”$Resources:customDocumentLibrary,customDocumentLibrary_Folder;”

Image=”/_layouts/images/itdl.gif”

DocumentTemplate=”4000″/>

</Elements>

If you did not perform Step 2 use the following XML:

<?xml version=”1.0″ encoding=”utf-8″ ?>

<!-_lcid=”1033″ _version=”12.0.3820″ _dal=”1″ –>

<!-_LocalBinding –>

<Elements xmlns=”http://schemas.microsoft.com/sharepoint/”>

<ListTemplate

Name=”doclib”

Type=”4000″

BaseType=”1″

OnQuickLaunch=”FALSE”

SecurityBits=”11″

DisplayName=”Custom Document Library”

Description=”Custom Document Library”

Image=”/_layouts/images/itdl.gif”

DocumentTemplate=”4000″/>

</Elements>  

*Note: You can add multiple <ListTemplate> elements here if you wish to create a Feature that deploys multiple document libraries at a time.

Step 7: Copy the contents of the DocLib sub directory into your new Feature directory.  

In the C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURESCustomDocumentLibrary directory create a directory named DocLib.

Copy the contents of the C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATEFEATURESDocumentLibraryDocLib directory to the new DocLib directory you just created.

Step 8: (Optional) If you plan to use a custom Document Template and you performed Step 4 above, create a custom Document Template.  

Open Microsoft Word.

Type in “Sample document template” into the document.

Click File | Save As

In the dropdown list that says Save as type: select Document Template (.dot)

Name the file custom.dot

Click the Save button.

Step 9: (Optional) Copy the custom Word Document Template you create that will server as the default Document Template for your new custom Document Library to the SharePoint server.  

Copy the custom.dot file to the following directory on the SharePoint server:

C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12TEMPLATE1033<NAME OF CUSTOME SITE DEFINITION>DOCTEMPWORD  

Step 10: (Optional) Add the Feature to the site definition you want the feature to be installed on by default when the site is created. (Please see the Adding a Document Library Feature to a Site Definition in WSS V3 / MOSS 2007 article.)  

Step 11: Register the Feature with a SharePoint site via the STSADM.EXE command line utility.  

On the SharePoint server type the following command on the command line to change to the directory stsadm.exe resides in:

cd “C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12BIN”

Then type the following command to install the feature on the SharePoint server:

stsadm -o installfeature -filename “CustomDocumentLibraryfeature.xml”

This example assumes you have created a SharePoint site at the following URL:

http://SharePointServerName/SiteDirectory/CustDocLib

To activate the feature on the SharePoint site mentioned above site use the following command:

stsadm –o activatefeature –filename “CustomDocumentLibraryfeature.xml” –url “http://SharePointServerName/SiteDirectory/CustDocLib”

*Note: You can also uninstall and deactivate features using the stsadm utility.

Uninstall command line: stsadm -o uninstallfeature -filename “CustomDocumentLibraryfeature.xml”

Deactivate command line: stsadm –o deactivatefeature –filename “CustomDocumentLibraryfeature.xml” –url “http://SharePointServerName/SiteDirectory/CustDocLib”

Step 12: Reset IIS.  

On the SharePoint server type iisreset on the command line and wait for IIS to reset.

Step 13a: (Assuming you did not do Step 10) Browse to the SharePoint site you registered the feature with. Browse to the Create web page and the custom Document Library will be available in the list of items to create.

Open Internet Explorer.

Browse to the http://SharePointServerName/SiteDirectory/CustDocLib site.

Click Site Settings

Click Create

Click Custom Document Library

Fill in the required information and to the Custom Document Library.

(If you created a custom Document Template) In the Document Template dropdown select Custom Document Library for the template.

Click Create.

If you created a custom Document Template clicking the new button on the Custom Document Library toolbar will open the custom Document Template you created.

Step 13b: (Assuming you did perform Step 10) Create a new SharePoint site with the site definition you edited in Step 10. Browse to the new site to see the custom Document Library already created for you.

Open Internet Explorer.

Create a new SharePoint site with the custom site definition you edited.

You will see the Custom Document Library already created for you and listed in the QuickLaunch Navigation on the left hand side of the site.

If you created a custom Document Template clicking the new button on the Custom Document Library toolbar will open the custom Document Template you created.