Wednesday, March 27, 2013

Developing SharePoint 2010 features using Visual Studio 2010

Referred URL -

Developers can extend SharePoint by creating new features. Using features developers can develop customizations and extensions that can be capable of automated deployment, management, un-installation and upgrading. SharePoint Feature allows developer to create a set of functionality to group into a component and allows administrators to add that functionality to the site/site collection. In addition to this, administrator can disable or uninstall the feature if they wish. With SharePoint 2010, it is possible to upgrade the feature to a new version. In this article I am going to demonstrate how to create a feature using SharePoint 2010.

In SharePoint, each feature has its own folder. If your SharePoint is installed in default location, you can find all the features installed in the farm from the following location

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\FEATURES

All the features installed in SharePoint farm will have a corresponding folder here. Open any one folder you may see the files inside each folder. Let us examine the feature folder. For this purpose, I have opened the feature folder for HelpLibrary, that is available with default SharePoint installation.


Every feature will have at least one file named Feature.XML called as feature manifest. A feature folder contains all the files required to implement the feature along with the feature manifest file. Other than feature manifest file, there can be other XML files and folders exists in the feature folder. Basically a feature is a set of XML documents, 1 feature manifest file and other elements manifest files. Features may also contain other types of files such as css files, aspx files, images etc.

Basically you can create a feature by using any text editor, but in this article I am going to demonstrate how to develop a feature using Visual Studio.

For this demonstration, I am going to create a aspx page that just says “Welcome to My site” and add a menu item to the SharePoint that link to the page. In real time you may add web parts, list definitions, content types, list instances, image files etc to the feature. So let me start creating the feature.

First you open Visual studio then select file -> New project, the new project dialog will appear. From the templates section, select Visual C# -> SharePoint -> 2010. Make sure the framework selected is .net framework 3.5. I named my project as “SayHelloFeature”, you are free to choose any name, and then choose a location for the project. Click ok once you are done.


SharePoint customization wizard will appear now. Enter the url for the SharePoint portal where you want to do the debugging. When developing a feature using Visual Studio gives you an edge here. Visual Studio will do the packaging and deploying part as you might need to do this several times during development. In my case I need to deploy the feature to the entire farm, so I select the option deploy as farm solution. Click on the finish button once you are done.


Now visual studio will create a minimal SharePoint project for you. See the snapshot of the project in the solution explorer.


Right click on the Features and select Add Feature. The solution explorer will look as follows


Also Visual Studio will provide a dialog for updating the feature properties. You can provide a title and description for the feature and select the scope of the feature – means where it needs to be deployed. Since I want to deploy this feature in a site level, I select “Web” as the scope of my feature.


You can see the source of the application by clicking on the manifest link and you have the option to view and edit the feature.xml file directly.


You can set the version of the feature using the property dialog; the feature will be used by SharePoint 2010 when you upgrade the solution.

From solution explorer, right click the project, select Add -> new Item, the new item dialog appears. Select module as the item template, enter a name, I just kept the name as Module1 (default one suggested by Visual Studio). Click Add once you are done.


From the solution explorer, the view of the project looks as below.


When adding a new module, visual studio will automatically add an element manifest file and a Sample.txt file to the Module. Element manifest file is an xml file (Elements.xml – it can be any name as there is no restriction on the name, but if you change the name, make sure you update the feature definition with the corresponding name) that keeps track of all module files. In my feature, I don’t need sample.txt file, delete sample.txt by right click on it and select delete.

Now you need to add an aspx page to the Module that says Hello to the user. To add a simple aspx file, there is no straight forward method available in VS2010, you can follow the below steps.

(Visual Studio doesn’t provide a direct way to add an aspx page, so just choose text file as file type and then name it with .aspx extension)

Right click module1 and the select Add -> New Item, then select General from the template, choose text-file as the template and name the file as Hello.aspx


Now you need to add the markup for the aspx page. You need to do this manually. Don’t forget to refer the default SharePoint assemblies. I have added the below markup to the hello.aspx page, You just need to make sure that the required content place holders are there in the new page, all the other content you are free to make it your own.

<%@ Page MasterPageFile="~masterurl/default.master" meta:progid="SharePoint.WebPartPage.Document" %>
<asp:Content ID="title" runat="server" ContentPlaceHolderID="PlaceHolderPageTitle">
   Saying Hello
<asp:Content ID="addhead" runat="server" ContentPlaceHolderID="PlaceHolderAdditionalPageHead">
<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="PlaceHolderMain">
   <h3>Hello, Welcome to My Site</h3>

Now we are done with the aspx page. For the demonstration purpose, I am going to add a menu item that links to the newly created page. For such purposes, SharePoint provides custom action using which you can add menu items to SharePoint.

From the solution explorer, right click the project, select Add -> New Item. In the template list, you will not find a template with the name custom action, so select empty element; - You can use empty element when none of the templates matches a particular type of item.

For understanding more about custom actions read the below 2 posts

In the elements.xml under new empty element just added, paste the below xml

<CustomAction Id="SiteActionsToolbar" GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu" Sequence="100" Title="Say Hello"
Description="A page saying hello"><UrlAction Url="~site/SitePages/Hello.aspx"/>

Save the file, now you are done with the custom action. Let us examine the feature file. Go to the feature page and make sure feature contains all the items you have added.


Now the solution is ready to deploy. From solution explorer, right click the project and click deploy.


Now visual studio will deploy the feature to the farm and activate it. Once deployed, go to the features folder under 14 hive and you will see your feature folder there. See the snapshot of the SayHello feature folder, once deployed.


Once the feature is activated, You can see the menu item is added to the Site Actions menu of your site.


Click on the Say Hello link, you will be redirected to the page.


Visual Studio makes it easy for developers to build features for SharePoint 2010. Developers have complete control over each components of the feature. The one click deployment saves lot of developer time as they should not worry about the administrative process involved in the installation process. In addition to all these, Visual Studio will package your solution as WSP file, in single click so that you can package your solution and deploy it to production servers.

How to create Business Connectivity Services (BCS) using SharePoint Designer 2010

Referred URL -


Business Connectivity Services (BCS) is a new service introduced with SharePoint 2010 to allow SharePoint sites to connect to and manipulate external data. SharePoint 2007 had a similar facility in the form of Business Data Catalog (BDC) which made external data available within its site. However, a major problem with BDC was the difficulty in creating solutions as there was no support in the 2007 designer.  Most BDC solutions were simply for accessing external data, manipulating external data sources was extremely difficult.

With SharePoint 2010, BCS ships with out-of-box features such as solutions, services, and tools which may connect to external data an easy task. Whether you want to retrieve Outlook contacts in a list offline or edit the contents of your document file or share your excel sheet online or reuse data from dynamic InfoPath forms or just update your business presentation, BCS enables deep content sharing, editing and integration in SharePoint 2010 with SharePoint Designer and Visual Studio tools.

SQL Database:

First we need to create a sample database in SQL which is going to be connected to the SharePoint. Simply create a new database in your SQL Server and have it filled with some sample data. In my case, I have created a SQL database, called Test. In the database Test I have added one table called Employee_Details.

See the details of the table in the below figure.


Emp Name
Emp ID




Creating an External Content Type:

Before we integrate external data to the SharePoint, we need to create an external content type.  You can use the SharePoint site or SharePoint designer to create an external content type. In this example, we will use SharePoint Designer 2010. The most effective and easy way to set up a simple BCS connection is to use SharePoint Designer 2010. Following are the steps involved to create an External Content Type in SharePoint Designer 2010.

Steps Involved:

  1. Open the SharePoint Designer 2010.
  2. Click Open Site; enter the Site Name in the Open Site dialog box.
  3. Select External Content Types in the left Navigation.
  4. Click to create a new External Content Type as shown below.
  5. The External Content Type dialog will be displayed.
  6. Enter the Name and Display Name for the external content type as shown in the above figure.
  7. Select the Office Item Type as Generic List from the dropdown list.
  8. Next click the link, Click here to discover external data sources and define operations to integrate the existing customer database.
  9. The Operation Designer dialog will then be displayed. Click Add a Connection to connect to the database.
  10. Select "SQL Server" as your Data Source Type.
  11. Enter the details about your connection to your SQL Server.
  12. When the connection is made, your Data Source Explorer will be filled with the database you have specified.
  13. Now choose the table Employee_Details which we are going to connect to the SharePoint.
  14. Right click the table and select the option, Create All Operations so that you will be able to read, select, update and delete rows from the database table.
  15. Now the Operation Properties window will pop up which shows the details of the operations that can be performed in the database table.
  16. Click Next to get to the Parameters page, Select the field that you want to act as an Identifier. In my case I have selected Emp ID as an identifier.
  17. Click Finish.
  18. You'll be presented with a list of operations that your External Content Type can do, as shown in the below figure.
  19. After completing all these steps save the External Content Type.

Creating an External List:

There are few ways to create an External List in SharePoint 2010. Here I am creating an External List using SharePoint Designer 2010. To create the External List follow the below steps.

Steps Involved:

  1. Select List and Libraries in the left Navigation.
  2. Click External List that is available in the top Ribbon.
  3. External Content Types Picker wizard will pop up select the External Content Type ECT that we have created. See the below figure.
  4. Click Ok.
  5. In the Create external List wizard enter the Name and Description for the External list. See the below figure.
  6. Click OK.
  7. Navigate to the site where you have created the External List.
  8. You can able to see the External List - BCS List that we have created using SharePoint Designer 2010. See the below figure.
  9. Click on the BCS List.
  10. You may encounter "Access denied by Business Data Connectivity" error when trying to access the External List BCS List.
  11. The reason is because External List requires External Content Type and External Content Type are using Business Data Connectivity services proxy to access external Data Source. With the same principle of BDC in MOSS 2007-users are required to have BDC object permission before they can use it.

Configure Business Data Connectivity access rights:

  1. Go to Central Administration -> Application Management -> Manage Service Applications.
  2. Click on Business Data Connectivity Service.
  3. In the top Ribbon click on Manage.
  4. In Service Application Information check the External Content Type ECT.
  5. And in the top Ribbon click the Site Object Permissions.
  6. Site Object Permissions wizard will pop up add the account (Group or Users) and assign the permissions.
  7. Once you have configured Business Data Connectivity access rights navigate to the site and check the External List –BCS List that we have created.


  1. Go to the BCS List you can see the external data as shown in the below figure.
  2. You now have the ability to create new items, update existing items, delete items and do all your normal CRUD-operations (CRUD = Create, Read, Update, Delete) straight from the SharePoint 2010 list.


Thus the External data that we have created using SQL Server is connected using BCS to the SharePoint list. This is one of the simplest ways to connect external data to the SharePoint through BCS using SharePoint Designer 2010.

Creating a custom site definition in SharePoint 2010

Referred URL -

This article describes how to create a custom site definition in SharePoint 2010.  The process to create a custom site definition has not changed much since the last version of SharePoint, the only differences are the directory names and some of the XML in the WEBTEMP files. 


Site Definition basics

Site definitions are located in the following folder on the SharePoint server:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates


Each site definition has its own sub directory under this folder.

Inside each site definition sub directory the .aspx pages for the various web pages and lists that make up the site definition are stored.  Some of the new site definitions that come with SharePoint 2010 also include image files in these subdirectories.  The ONET.XML file specifies the various configurations and modules the site definition is made up of.  It is stored inside the XML subdirectory inside each site definition sub directory.

Note: I think it is interesting that the SDK refers to Site Templates as the .stp files that are generated when you save a site as a template from the web UI, and the SDK refers to file based site templates as site definitions – yet this directory is named SiteTemplates. This has been the case for a couple of versions now so I don’t think we’ll see it change in the future.

Site definitions are registered with SharePoint and made available via the WEBTEMP<NAME OF SITE DEFINITION>.XML file.  Theoretically, you could register a site definition with SharePoint with one of the out of the box WEBTEMP XML files, however this could introduce problems when service packs or newer versions of the product are released.  It is considered best practice to create your own WEBTEMP XML file to register your custom site definitions.

WEBTEMP XML files are located in the following folder on the SharePoint server:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\1033\XML


Creating a custom Site Definition in SharePoint 2010 – Manual Approach

Step 1: Clone an existing Site Definition

Step 2: Create the WEBTEMP XML fragment file to register the site definition with SharePoint

Step 3: Reset the application pool

Step 4: Create a site based on the custom site definition

Step 1: Clone an existing site definition

Navigate to the following directory on the SharePoint server in Windows Explorer:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\SiteTemplates

Copy the STS directory and paste it back into the same directory.  (You can copy any of the existing site definitions and use them as a baseline to create your own.)

Rename the Copy of STS directory to SAMPLE


Step 2: Create the WEBTEMP XML fragment file to register the site definition with SharePoint

Create a file called WEBTEMPSAMPLE.XML in the following directory:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\1033\XML

Put the following XML into the WEBTEMPSAMPLE.XML file:

<?xml version=”1.0″ encoding=”utf-8″?> <!– _lcid=”1033″ _version=”14.0.4514″ _dal=”1″ –> <!– _LocalBinding –><Templates xmlns:ows=”Microsoft SharePoint”> <Template Name=”SAMPLE” ID=”10001″>    <Configuration ID=”0″ Title=”Sample Team Site” Hidden=”FALSE”         ImageUrl=”/_layouts/images/stts.png” Description=”A sample team site.”         DisplayCategory=”Custom Site Definitions” >    </Configuration>    <Configuration ID=”1″ Title=”Sample Blank Site” Hidden=”FALSE”         ImageUrl=”/_layouts/images/stbs.png” Description=”A sample blank site.”         DisplayCategory=”Custom Site Definitions” AllowGlobalFeatureAssociations=”False” >    </Configuration>    <Configuration ID=”2″ Title=”Sample Document Workspace” Hidden=”FALSE”         ImageUrl=”/_layouts/images/stdw.png” Description=”A sample document workspace.”         DisplayCategory=”Custom Site Definitions” >        </Configuration> </Template> </Templates>

Note: The DisplayCategory attribute of the <Configuration> element dictates which tab the configuration will appear on in the create new site web dialog. You can create your own tabs by putting your own values in this attribute (like I have done in the XML above and displayed in the screenshot below).  The screenshot below demonstrates how the Configuration element’s attributes (inside the WEBTEMP file) map to the create a site dialog.

Save the file.

Step 3: Reset IIS

Open a command prompt on the SharePoint server

Type in the command: iisreset

Wait for IIS to reset

Step 4: Create a site based on the custom site definition

Open Internet Explorer

Navigate to a SharePoint site on the server where you performed the steps above.

Click the Site Actions menu

Select New Site

Click the Custom Site Definitions tab on the left side of the create a site dialog to see the new site templates you registered with the SAMPLE site definition. 

Select one of the sample site templates you created.

Enter a title and URL

Click the Create button and your new SharePoint site is created.

Creating a custom Site Definition in SharePoint 2010 – Visual Studio 2010 Approach

Visual Studio 2010 simplifies the process by providing a project type specifically tailored for this purpose.  To create a custom site definition with Visual Studio 2010 first create a new Site Definition project.  See the screenshot below for reference.

Then set the path to the SharePoint site where you would like to deploy the site definition.

Once the project is created, you can see all the files needed to support a custom site definition in the Solution Explorer.

After you make edits to the files all you need to do is press F5 and the custom site definition is packaged into a WSP and deployed to the SharePoint server.  All that is left to do is to create a new site based on the custom site definition to verify it works properly. 

Here you can see what the site template looks like when you use the out of the box Visual Studio 2010 Site Definition project type.

SharePoint 2010 - Lists and Event Handlers

Referred URL -

In this article we explore the Event Handling features of a List through code. Using the SPList in Server Object Model we can access event handling for a list.
We can use the event to perform the following activities:

  • Validate the Item
  • Log the information
  • Create associated items

There are multiple Event Types for a List:

  • List Events
  • List Item Events (Add, Edit, Delete)
  • Web Events
  • Feature Events
  • Workflow Events

Create Event Receiver Project
To begin, create an Event Receiver project in Visual Studio.

You will be prompted for the site:

By default the machine site will be shown in the dialog box. Leave the default option of the Sandboxed solution and click the Next button.

Select the List Item Events and use the event source Contacts. Check the check boxes for added, updated and deleted events and click the Finish button.
Note: The site URL can be changed later. For the time being we are using hard-coded URLs.
On clicking the Finish button, the code will be generated for the List Event Receiver and you can place a break-point in the ItemAdding event as shown below.

Now execute the application and your Event will be added and activated.
Try adding a new contact inside SharePoint:

On clicking the Save button, the breakpoint inside Visual Studio will be hit:

You can cancel the operation by using the Cancel property.
You can report an error message using the ErrorMessage property.
Cancel for Delete
You can cancel an item delete by setting the properties.Cancel = true:
public override void ItemDeleting(SPItemEventProperties properties)
       properties.ErrorMessage = "Deleting Item is not permitted!";
       properties.Cancel = true;
Try deleting an item from the Contacts inside SharePoint:

You will get the following message prompted:

Deploying the Event Receiver
You can take a note that while you stop Visual Studio, the item deletion is allowed inside SharePoint. To make the event receiver permanent, use the project Deploy option.

Now try Deleting an item without Visual Studio Debugging. You will get the same Error Message Dialog and this concludes the Event Creation and Deployment to a SharePoint site.
Properties of Project
You can change the URL and other properties created through the Wizard. Use Project > Properties to access/modify these properties.

The event receiver is actually deployed as a feature inside SharePoint. You can view the Feature properties and the associated XML file under Features special folder as shown below.
The Event code and Elements.xml reside inside the EvntReceiver1 group as shown below.