Features 是MOSS 2007以开箱即用的一套新功能。MOSS 2007提供的许多站点定义中的Document Library 是以Feature方式提供的,基于这个开箱即用的 Document Library Feature,也可以创建自己定制Document Library。
Feature 基础
首先让我们看一下组成Feature的目录和文件。Features 存储在SharePoint服务器的如下路径下:
C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES
每个Featrue在此路径下有自己的子目录,在每一个Feature子目录下会发现名字为Feature.xml的文件,它存储这关于Featrue的metadata.
Document Library Features
以下的Featrue.xml文件是MOSS自带的用于创建 DocumentLibrary Feature:
?xml version="1.0" encoding="utf-8" ?>
<!-- _lcid="1033" _version="12.0.4017" _dal="1" -->
<!-- _LocalBinding -->
<Feature Id="00BFEA71-E717-4E80-AA17-D 0C 71B360101"
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="ListTemplates/DocumentLibrary.xml" />
</ElementManifests>
</Feature>
在这个XML文件中,以下关于Featrue的metadata 包含在Featrue 元素中。
ID: 一个GUID,用于唯一标识这个Feature;
Title:Feature 的名字,可以在网站内关于Site Featrues的页面中看到。
Description:对description的描述。
Version:Feature的版本;
Scope:其值可以是Web或Site,它指明了这个Feature是应用于整个的Site Collection还是仅仅用于单独的一个子站点。
Hidden:值可以是True或False.该设置指定了这个Feature是否在Site Feature页面上显示。
DefaultResourceFile: 资源文件名字,Feature依赖它提供其它附加的配置信息。
Feature.xml文件中的<ElementManifests>元素,这个元素包含了另一个XML文件的位置,而这个文件包含的<Elemnets>的内容是Feature要实现的。
<ElementManifest>元素指明了要使用一个名为 DocumentLibrary.xml的文件,它在ListTemplates子目录下。子目录和文件使用的是相对路径。以下是该文件的< Elements>元素内容,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>
Resource Files
资源文件存储在如下目录中:
C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/Resources
资源文件包含有键/值对的信息,用于向SharePoint提供本地化的能力。
我们看一下DisplayName的属性值的设置方法:
这个属性值是:$Resources:core,docLibList;这个值指明了docliblist的值是从core资源文件中读取的。
属性值中的$Resources:core这部分是和core.en-US.resx资源文件相对应的。Docliblist部分是和core资源文件中的docliblist<data>元素对应的。
Core资源文件中用于填充DisplayName和Description属性的xml文本如下:
<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>
Feature 的另外一个优势
在SPS2003中,ONET.XML文件非常大,包含了大量的信息。随着Features的 到来,ONET.xml缩小了,因为Features用于组件化ONET.xml的内容。就像我们已经看到的这样,以前存在与ONET.xml文件中的 <ListTemlate>元素已经被转移到了Document Library Feature.其它开箱即用的列表也使用同样的方法。
创建自已的 Document Library Feature
Step 1: Create a directory for the Feature.
Create a new directory for your Feature in the C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES 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 Files/Common Files/Microsoft Shared/web server extensions/12/Resources 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="ListTemplates/CustomDocumentLibrary.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="ListTemplates/CustomDocumentLibrary.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 documentation 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="doctemp/word/custom.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="doctemp/word/custom.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 Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/CustomDocumentLibrary 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 Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/CustomDocumentLibrary directory create a directory named DocLib.
Copy the contents of the C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/DocumentLibrary/DocLib 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 Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/1033/<NAME OF CUSTOME SITE DEFINITION>/DOCTEMP/WORD
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 MOSS 2007 documentation.)
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 Files/Common Files/Microsoft Shared/web server extensions/12/BIN”
Then type the following command to install the feature on the SharePoint server:
stsadm -o installfeature -filename "CustomDocumentLibrary/feature.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 “CustomDocumentLibrary/feature.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 "CustomDocumentLibrary/feature.xml"
Deactivate command line: stsadm –o deactivatefeature –filename “CustomDocumentLibrary/feature.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.
Congrats! You made your own custom Feature!