Create List Instance using Features in SharePoint 2010 & Add Feature Event Receivers & TestingRequirement:
Create a new Link list with pre-populated list items as a feature based solution.
1. Create a New Empty SharePoint Project, Give it a name, I've named it as Crescent.Portal.DashboardLinks
2. In the next dialog box, Select either Farm solution or Sandboxed solution whichever applicable to your requirements. I've selected "Deploy as a Farm Solution"
3. Right click the Project in visual studio solution explorer >> Add >> New Item >> List Instance
4. Enter the List Instance settings: Link List Name, List to Instantiate, URL. Click Finish.
5. Now our project will look like the below screen. Look at the "FeatureId" attribute. This is the Feature ID of the Link List feature. In case, If you want to create a List instance for your own List definition, You can use the Feature ID of the particular List. (You can just get into 14 Hive, Templates and pick the relevant feature folder's Feature.xml file to get the feature Id)
6. Rename the Feature from "Feature1" to "DashboardLinks" in solution explorer. Get into the Feature designer (Right click the "DashboardLinks" and choose "View Designer"), Give it a meaning full title (Here I've given "Dashboard Links Feature") that will appear on the site features page. Add the DashboardLinks ListInstance Element to the feature.
7. Our requirement is to Pre-populate the Dashboard Links list. So, Let’s add the <Data> element with values to theElements.xml as below:
<?xml version="1.0" encoding="utf-8"?>
<ListInstance Title="Dashboard Links"
<Field Name="URL">http://Crescent.SharePoint.com/sites/PMO/Dashboard, PMO Dashboard</Field>
<Field Name="Comments">PMO Dashboard Link</Field>
<Field Name="URL">http://Crescent.SharePoint.com/sites/functional/Dashboard, Functional Dashboard</Field>
<Field Name="Comments">Functional Dashboard Link</Field>
<Field Name="URL">http://Crescent.SharePoint.com/sites/operations/Dashboard, Operational Dashboard</Field>
<Field Name="Comments">Operations Dashboard Link</Field>
8. Build and deploy the Project (Just right click the Project in visual studio, choose Build and then choose Deploy)
9. Verify the feature is activated by going to Site actions >> Site settings >> Manage Site features (Under Site actions tab)
10. See it in action!
You might be interested in:
Track changes and get email when SharePoint farm settings change
Add Feature Event Receivers
Feature event receivers are methods that execute when one of the following feature-related events occurs in SharePoint:
- A feature is installed.
- A feature is activated.
- A feature is deactivated.
- A feature is removed.
This walkthrough demonstrates how to add an event receiver to a feature in a SharePoint project. It demonstrates the following tasks:
- Creating an empty project with a feature event receiver.
- Handling the Feature Deactivating
- Using the SharePoint project object model to add an announcement to the Announcements list.
|Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements|
You need the following components to complete this walkthrough:
- Supported editions of Microsoft Windows and SharePoint. For more information, see Requirements for Developing SharePoint Solutions.
- Visual Studio 2010.
Creating a Feature Event Receiver Project
First, create a project to contain the feature event receiver.
To create a project with a feature event receiver
- Display the New Project dialog box by pointing to New on the File menu, and then selecting New Project.
- Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
- In the Templates pane, select Empty Project.
This project type is for feature event receivers because there is no project template for them.
- In the Name box, type FeatureEvtTest and then click OK to display the SharePoint Customization Wizard.
- On the Specify the site and security level for debugging page, enter the URL for the SharePoint server site to which you want to add the new custom field item, or use the default location (http://<system name>/).
- In the what is the trust level for this SharePoint solution? section, click Deploy as a farm solution and then click Finish.
For more information about sandboxed solutions versus farm solutions, see Sandboxed Solution Considerations.
Adding an Event Receiver to the Feature
Next, add an event receiver to the feature and add code that executes when the feature is deactivated.
At TekSlate, we offer resources that help you in learning various IT courses. We avail both written material and demo video tutorials. To gain in-depth knowledge and be on par with practical experience, then explore SharePoint Training Course.
To add an event receiver to the features
- Create a feature by right-clicking the Features node and selecting Add Feature.
Notice that a feature named Feature1 appears under the Features node.
- Add an event receiver to the feature by right-clicking Feature1 in the Features node and selecting Add Event Receiver.
This adds a code file under Feature1. In this case, it is named either Feature1.EventReceiver.cs or Feature1.EventReceiver.vb, depending on your project's development language.
- The event receiver class contains four commented-out methods that act as events. Replace theFeatureDeactivating method with the following:
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
// Get reference to SharePoint site.
SPSite site = new SPSite("http://localhost");
SPWeb web = site.OpenWeb("/");
// Get reference to Announcements list.
SPList announcementsList = web.Lists["Announcements"];
// Add new announcement to Announcements list.
SPListItem oListItem = announcementsList.Items.Add();
oListItem["Title"] = "Deactivated Feature: " + properties.Definition.DisplayName;
oListItem["Body"] = properties.Definition.DisplayName + " was deactivated on: " + DateTime.Now.ToString();
catch (Exception e)
Console.WriteLine("Error: " + e.ToString());
Testing the Feature Event Receiver
Next, deactivate the feature to test whether the FeatureDeactivating method outputs an announcement to the SharePoint Announcements list.
To test the feature event receiver
- Set the project's Active Deployment Configuration property to No Activation.
Setting this property prevents the feature from activating in SharePoint and lets you debug feature event receivers. For more information, see Debugging SharePoint Solutions.
- Press F5 to run the project and deploy it to SharePoint.
- At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.
- Under the Site Actions section of the Site Settings page, click the Manage site features link.
- In the Site Features page, click the Activate button next to the FeatureEvtTest Feature1 Feature.
- In the Site Features page, click the Deactivate button next to the FeatureEvtTest Feature1 Feature. Click the confirmation link to deactivate the Feature.
- Click the Announcements link on the Navigation bar.
Notice that an announcement appears in the Announcements list after the feature is deactivated.
SharePoint Features can be scoped to the Farm, Web Application, Site Collection, and Web Site level depending on the purpose of the feature. The Feature scope is determined by the setting of the Scope attribute in the Feature element defined in the feature.xml file.
A sample Feature element tag is given below:
Web Site scoped Feature (Scope="Web"):
A Web Site scoped Feature is one that can be activated only at the individual Web site level. List templates, list instances, custom actions, event receivers, etc are the some common elements for web site scoped features. Web Site scoped features can be activated by using one of the following two methods:
- Run the following STSADM command:
stsadm -o installfeature -name FeatureFolderName –url http://servername/site/subsite
- Open the Site Features page in the SharePoint site (Site Actions -> Site Settings -> Modify All Site Settings -> Site Features) and click the Activate button.
Site Collection scoped Feature (Scope="Site"):
A Site Collection scoped Feature is one that can be activated at the site collection level and contains items that apply to the site collection as a whole (for example, content types that are shared across the site collection), as well as items that can be activated per site (for example, list instances, etc). Site Collection scoped features can be activated by using one of the following two methods:
- Run the following STSADM command:
stsadm -o installfeature -name FeatureFolderName –url http://servername/site/sitecollection
- Open the Site Features page in the SharePoint site (Site Actions -> Site Settings -> Modify All Site Settings -> Site Collection Features) and click the Activate button.
Web Application scoped Feature (Scope="WebApplication"):
A Web Application scoped Feature is one that can be activated at the web application level and contains items like administrative web application links, delegate control registrations, feature/site template association, document form registrations, etc. A farm Feature can contain, for example, links to /_layouts pages and files, /_admin pages, and other elements. Web Applicationscoped features can be activated by using one of the following two methods:
- Run the following STSADM command:
stsadm -o installfeature -name FeatureFolderName -url http://servername
- Open the "Manage Web Application Features" page in the Central Admin application(SharePoint Central Administration -> Application Management -> Manage Web Application Features) and click the Activate button.
Farm scoped Feature (Scope="Farm"):
A Farm scoped Feature can be activated at the server farm level. A farm Feature contains a number of elements that are critical for implementing applications and logic anywhere within a deployment. A farm Feature can contain, for example, links to /_layouts pages and files, /_admin pages, and other elements. Farm scoped features can be activated by using one of the following two methods:
- Run the following STSADM command:
stsadm -o installfeature -name FeatureFolderName
- Open the "Manage Farm Features" page in the Central Admin application(SharePoint Central Administration -> Operations -> Manage Farm Features) and click the Activate button.
Note: Farm scoped feature is activated automatically once it is installed on the server farm.
SharePoint Element Types for Features:
The following table describes various element types for Features and shows their possible settings for scope.
|Content Type||Contains a schema definition you can reuse and apply to multiple list definitions.||Site|
|Content Type Binding||Content type binding enables you to provision a content type on a list defined in the Onet.xml schema. Lists defined in the Onet.xml schema cannot be modified directly.||Site|
|Control||A delegate control contains a registration for a well-known control installed on a Web page. This lets you replace existing controls, such as the Windows SharePoint Services search control, with another control.||Farm, WebApplication,Site, Web|
|Custom Action||You can define the following kinds of custom actions:|
|Farm, WebApplication,Site, Web|
|Custom Action Group||Defines a group of custom actions.||Farm, WebApplication,Site, Web|
|Document Converter||Contains the definition of a document converter. A document converter is a custom executable file that takes a document of one file type, and generates a copy of that file in another file type.||WebApplication|
|Feature/Site Template Association||Binds a Feature to a site definition configuration so that created sites are provisioned with the Feature.||Farm, WebApplication,Site|
|Field||Contains a field definition that can be reused among multiple lists.||Site|
|Hide Custom Action||Hides a custom action that has been added through another custom action.||Farm, WebApplication,Site, Web|
|List Instance||Provisions a SharePoint site with a specific list of data.||Site, Web|
|List Template||Contains a list definition or template, which defines a list that can be provisioned in a SharePoint site.||Site, Web|
|Module||Contains a set of files with which to provision sites.||Site, Web|
|Receiver||Contains an item event receiver registration.||Web|
|Workflow||Contains the definition for a workflow in a list.||Site|
For Indepth understanding of SharePoint click on
- SharePoint Tutorials
- Introduction to SharePoint
- Application Pages
- SharePoint Security
- Introduction to Web Parts
- Microsoft Dynamics CRM Training In Hyderabad