For feature, anyone familiar with SharePoint should be familiar with it. It not only controls the use of most of the functions on SharePoint, but also supports function expansion. The advantage is that management is very convenient. Although there are many feature development programs, such as using feature to customize function menus and webpart in user control mode, the feature activation and stop functions are used to execute the backgroundCodeIt is quite useful. The following is a simple example.
Step 1:
Open Vs and create a class library project named"Featuretoruncode",
SetClass1.csName mycode. CS. Add reference Microsoft. Sharepoint. dll. Add referenceUsing Microsoft. SharePoint; and inherit from the mycode classSpfeaturereceiver.
The entire mycode. CS code is as follows:
Using system;
Using system. Collections. Generic;
Using system. text;
Using Microsoft. SharePoint;
Namespace featuretoruncode
{
Public class mycode: spfeaturereceiver
{
Public override void featureactivated (spfeaturereceiverproperties properties) // when activated, modify the website title to my feature is working.
{
Spweb web = (spweb) properties. feature. parent;
Web. allowunsafeupdates = true;
Web. Title = "My feature is working ";
Web. Update ();
Web. Close ();
}< br> Public override void featuredeactivating (spfeaturereceiverproperties) // when activated, modify the website title to homepage
{< br> spweb = (spweb) properties. feature. parent;
Web. allowunsafeupdates = true;
Web. title = "Homepage";
Web. update ();
Web. close ();
}< br> Public override void featureinstalled (spfeaturereceiverproperties) // when the feature is installed,
{< br>
// Execute the code when the feature is installed
}
Public override void featureuninstalling (spfeaturereceiverproperties properties)
{
// When the feature is uninstalled, run the code
}
}< BR >}< br> remember to add a strong name to compile and generate featuretoruncode. DLL. And obtain reflector.exe
Program Set Name: featuretoruncode, version = 1.0.0.0, culture = neutral, publickeytoken = 146ae474b0fd2221
Class Name: featuretoruncode. mycode
feature. XML files.
Step 2:
Create a feature file. In the featuretoruncode Project, create the folder features, create the featuretoruncode folder in the features directory, In the Create feature in the featuretoruncode folder. XML .
feature. the XML Code is as follows:
<Feature xmlns = "http://schemas.microsoft.com/sharepoint"
E46240B7-8050-47e7-8A12-586C291B346C"
Title = "featuretoruncode"
Description = "use feature to manage and run background code"
Hidden = "false"
Scope = "Web"
Receiverclass = "featuretoruncode. mycode"
Receiverassembly = "featuretoruncode, version = 1.0.0.0, culture = neutral, publickeytoken = 146ae474b0fd2221"
>
</Feature>
Note:
In this XML file, the following metadata about featrue is contained in the featrue element. (For more information, see feature. XML files )
ID: a guid used to uniquely identify the feature. It can be generated through vs.
title: feature name, you can see it on the website Featrues page.
Description: Description of the description.
Version: feature version;
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.
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.
receiverclass: indicates the Assembly receiving class.
receiverassembly: The receiving assembly.
Save. In this way, the code and feature file have been created, and the deployment will be followed.
Step 3:
Deploy feature. FirstFeaturetoruncode. dll is dragged into the GAC, that isC: \ windows \ Assembly directory.
Go to the features directory under the project directory and setCopy the featuretoruncode folderC: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ features directory.
Open the command line on the Sharepoint Server
Enter the command to switch to the directory: cd c: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ bin
Run the following command to install feature: stsadm-O installfeature-filename.Featuretoruncode \ feature. xml.
Do not forget to ask for an iisreset server.
OK. The deployment and development of feature have been completed.
Step 4:
View results. Open the website, go to website settings, and go to website management ----> website function. You can see that a featuretoruncode feature is added, such:
Well, it's good. I'm a little excited to click the activation button. Fortunately, the Code has played a role and the website title has changed to my feature is working, for example:
at this point, the feature management background code has been completed. It's good. Try it and use your imagination. Here you can implement many functions ....