Features is a new function available out of the box in Moss 2007. features are stored in the following path of the Sharepoint Server: C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ features. Each featrue has its own subdirectory under this path. Under each feature subdirectory, a file named feature. XML is found, which stores metadata about featrue.
Next I will use feature to implement a small function. Add a custom menu in "website operations". First, use vs2005 to create a helloworld class library project, and then add the folder helloworld, add feature to the folder. XML fileCodeAs follows:
< Feature ID = "B2CB42E2-4F0A-4380-AABA-1EF9CD526F20" Title = "Hello World feature" Description = "This is my first feature" Scope = "Web" Hidden = "False" Imageurl = "TPG \ canteen.gif" Receiverassembly = "Helloworld, version = 1.0.0.0, culture = neutral, publickeytoken = b38a04419cc857d9" Receiverclass = "Helloworld. featurereceiver" Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Elementmanifests >
< Elementmanifest Location = "Elements. xml" />
</ Elementmanifests >
</ Feature >
The following describes the metadata information contained in the featrue element.
ID: a guid used to uniquely identify the feature, which can be obtained using the guid generation tool;
Scope: The value can be web or site. It indicates whether the feature is applied to the entire site collection or only for a separate subsite. If scope = "Web", activate it under [website operations-website settings-website management-Website Functions, if scope = "Site", activate it under [website operations-website settings-website management-website set function.
Hidden: The value can be true or false. This setting specifies whether the feature is displayed on the site feature page.
Defaultresourcefile: name of the resource file. Feature depends on it to provide additional configuration information.
<Elementmanifests> element: This element contains the location of another XML file, and the content of <elemnets> in this file is implemented by feature.
Then we addElements. xmlFile, the Code is as follows:
< Elements Xmlns = "Http://schemas.microsoft.com/sharepoint" >
< Customaction ID = "Siteactionstoolbar" Groupid = "Siteactions" Location = "Microsoft. Sharepoint. standardmenu" Sequence = "100" Title = "Hello World" Description = "Use feature to customize Menus" Imageurl = "_ Layouts/images/crtsite.gif" >
< Urlaction URL = "Http://msdn.microsoft.com" />
</ Customaction >
</Elements>
This is our custom menu item.
After adding a class file featurereceiver. CS, the Code is as follows:
Using System;
Using Microsoft. SharePoint;
Namespace Helloworld
{
Public Class Featurereceiver: spfeaturereceiver
{
Public Override Void Featureinstalled (spfeaturereceiverproperties properties ){}
Public Override Void Featureuninstalling (spfeaturereceiverproperties properties ){}
Public Override Void Featureactivated (spfeaturereceiverproperties properties)
{
Spweb site = (Spweb) properties. feature. parent;
Site. properties [ " Originaltitle " ] = Site. title;
Site. properties. Update ();
Site. Title = " Hello World modify " ;
Site. Update ();
}
Public Override Void Featuredeactivating (spfeaturereceiverproperties properties)
{
Spweb site = (Spweb) properties. feature. parent;
Site. Title = Site. properties [ " Originaltitle " ];
Site. Update ();
}
}
}
The spfeaturereceiver class defines that when the Web part feature is installed, activated, deactivated, or uninstalled, moss triggers these events. here we need to set the receiverassembly and receiverclass attributes in feature. xml. These attributes point to the Managed class of a function receiver. Publickeytoken is the key of helloworld, which can be obtained by using "Sn-T helloworld" in the vs2005 command line.
Basically, our task is complete. Now we are about to start the deployment. You need to follow these steps:
1. Add the helloworl folder (including feature. xml andElements. xmlFile) to the C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ features folder.
2. stsadm-O installfeature-filename helloworld \ feature. XML-Force
3. Use "gacutil-ifProgramSet Name "register helloworld. DLL to GAC
4. Restart IIS: iisreset
Now we can go to the website set function to check whether the feature can be activated. When activated, the code in featureactivated will be executed.
Of course, we can use a batch process to complete this deployment process. Note the path changes:
@ Set templatedir = " C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ Template "
@ Set stsadm = " C: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ bin \ stsadm "
@ Set gacutil = " D: \ Program Files \ Microsoft Visual Studio 8 \ SDK \ V2.0 \ bin \ gacutil.exe "
echo installing helloworld. DLL in GAC
% gacutil %-if bin \ debug \ helloworld. DLL
echo copying files to template directory
xcopy/e/y template \ * % templatedir %
echo installing feature
% stsadm %-O installfeature-filename helloworld \ feature. XML-force
iisreset
REM cscript C: \ windows \ system32 \ iisapp. vbs/A " sharepointdefaappapppool " /r
The following figure shows the final implementation result.