Create List Instance using Features in SharePoint 2010 & Add Feature Event Receivers & Testing

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

List Instance using Features in SharePoint 2010  

Requirement:  Create a new Link list with pre-populated list items as a feature based solution. Steps: 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)   1   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")

Desired to gain proficiency on SharePoint? Explore the blog post on the "SharePoint Training" course to become a pro in SharePoint.

that will appear on the site features page. Add the DashboardLinks ListInstance Element to the feature. 1 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"?> <Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <ListInstance Title="Dashboard Links" OnQuickLaunch="TRUE" TemplateType="103" FeatureId="00bfea71-2062-426c-90bf-714c59600103" Url="Lists/DashboardLinks" Description=""> <Data> <Rows> <Row> <Field Name="URL">http://Crescent.SharePoint.com/sites/PMO/Dashboard, PMO Dashboard</Field> <Field Name="Comments">PMO Dashboard Link</Field> </Row> <Row> <Field Name="URL">http://Crescent.SharePoint.com/sites/functional/Dashboard, Functional Dashboard</Field> <Field Name="Comments">Functional Dashboard Link</Field> </Row> <Row> <Field Name="URL">http://Crescent.SharePoint.com/sites/operations/Dashboard, Operational Dashboard</Field> <Field Name="Comments">Operations Dashboard Link</Field> </Row> </Rows> </Data> </ListInstance> </Elements>   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! 1   You might be interested in: Track farm changes 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.
Note
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

Prerequisites You need the following components to complete this walkthrough:

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

  1. Display the New Project dialog box by pointing to New on the File menu, and then select New Project.
  2. Expand the SharePoint node under either Visual C# or Visual Basic, and then click 2010.
  3. In the Templates pane, select Empty Project.

This project type is for feature event receivers because there is no project template for them.

  1. In the Name box, type FeatureEvtTest and then click OK to display the SharePoint Customization Wizard.
  2. 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>/).
  3. In 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.

To add an event receiver to the features

  1. Create a feature by right-clicking the Features node and selecting Add Feature.

Notice that a feature named Feature1 appears under the Features node.

  1. Add an event receiver to the feature by right-clicking Feature1 in the Features node and select 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.

  1. 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) { try { // 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(); oListItem.Update(); } 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

  1. 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.

  1. Press F5 to run the project and deploy it to SharePoint.
  2. At the top of the SharePoint Web page, click the Site Actions button and then click Site Settings.
  3. Under the Site Actions section of the Site Settings page, click the Manage site features link.
  4. In the Site Features page, click the Activate button next to the FeatureEvtTest Feature1 Feature.
  5. In the Site Features page, click the Deactivate button next to the FeatureEvtTest Feature1 Feature. Click the confirmation link to deactivate the Feature.
  6. 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: <Feature Id="F62C96CF-79FD-44be-8882-E9BFBD199184" Title="Feature Title" Description="Feature Description" Version="1.0.0.0" Scope="Site" Hidden="false"> </Feature> 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 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 install feature -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 install feature -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 Application scoped features can be activated by using one of the following two methods:

  • Run the following STSADM command: stsadm -o install feature -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 install feature -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: The 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.

Element Description 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. FarmWebApplication, SiteWeb
Custom Action You can define the following kinds of custom actions:
  • Content-type links for the content type settings page
  • Drop-down menu actions for the drop-down menu that appears for an item
  • Form toolbar buttons for New, Edit, or Display form toolbars.
  • Site Settings link for the Site Settings page.
FarmWebApplication, SiteWeb
Custom Action Group Defines a group of custom actions. FarmWebApplication,SiteWeb
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. FarmWebApplication,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. FarmWebApplication,SiteWeb
List Instance Provisions a SharePoint site with a specific list of data. SiteWeb
List Template Contains a list definition or template, which defines a list that can be provisioned in a SharePoint site. SiteWeb
Module Contains a set of files with which to provision sites. SiteWeb
Receiver Contains an item event receiver registration. Web
Workflow Contains the definition for a workflow in a list. Site

For an in-depth understanding of SharePoint click on

 

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox