Tuesday, December 15, 2009

How to: Creating SharePoint Features programmatically.

Create a new feature:

1. Setting up our project

Launch Visual Studio 2005 and create a new Class Library project.

File > New Project > Class Library >> DeletingEventHandler in the Name box, and then click OK.

>> In Solution Explorer, select DeletingEventHandler, and click Add Reference on the Project menu.

>> In the Add Reference dialog box, select Microsoft.SharePoint on the .NET tab and then click OK.

2. Writing our class with our custom code

>> In the Code Editor, import the Microsoft.SharePoint namespace as follows.
using Microsoft.SharePoint;

>> Change the name of the class to DeletingAction and make it inherit from the SPItemEventReceiver class, as follows.

>>public class DeletingAction : SPItemEventReceiver

>> Add the following code within the class to override the ItemDeleting method.

public override void ItemDeleting(SPItemEventProperties properties)
{
properties.Cancel = true;
properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
}

3. Signing and Installing our assembly into the GAC

>> In Solution Explorer, right-click the DeletingEventHandler node, and then click Properties.
>> In the Properties dialog box, click the Signing tab, select Sign the asembly, select Choose a strong name key file, and then click .

>> In the Create Strong Name Key dialog box, type DeletingEventHandler.snk in the Key file name box, optionally specify a password for the key, and then click OK.

>> To build the project, click Build Solution on the Build menu, or press CTRL+SHIFT+B.

>> To Generate a DLL, use WSP Builder >> Copy to GAC option. When its done you can see view the addsebly in C:\Windows\Assembly and dll will be DeletingEventHandler.dll

4. Define our resource files

SharePoint Features require not just a compiled dll, but also a set of corresponding files. These files are stored in the "12" HIVE on the SharePoint server. The file structure already exists and we will simply be adding files that are used by our feature to it.

Create a folder structure as shown below in your Solutions Explorer:
12 >> TEMPLATE >> FEATURES >> DeletingEventHandler and create 2 new xml files
elements.xml and feature.xml

Open the feature.xml file and replace any code with the following:

>> Feature.XML

No comments:

How to display Sharegate administrative dynamic reports via Power BI on SharePoint Online

After working in the SharePoint domain for a couple of years as SharePoint consultant , I am fortunate enough to help customers from man...