Monday, July 19, 2010

Creating Event for List Item using event receiver(SPItemEventReceiver) Class

1)Create Class Library project using visual studio
2)Add reference-->browse

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

select Microsoft.sharepoint.dll

3)Add namespace

using Microsoft.SharePoint;

4)Inherit SPItemEventReceiver class like below

public class EventReceiversForDemolistinCMS:SPItemEventReceiver
{

}

5)Override Method properties as per our requirement.

public class EventReceiversForDemolistinCMS:SPItemEventReceiver
{


public override void ItemAdding(SPItemEventProperties properties)
{

}
public override void ItemAdded(SPItemEventProperties properties)
{

}


public override void ItemUpdating(SPItemEventProperties properties)
{

}
public override void ItemUpdated(SPItemEventProperties properties)
{

}
public override void ItemDeleting(SPItemEventProperties properties)
{

}

public override void ItemDeleted(SPItemEventProperties properties)
{

}


}


6)Here i am taking simple example.I want to raise event for an item when item is added in the list.For that i will override Itemadded event as live below.

public class EventReceiversForDemolistinCMS:SPItemEventReceiver
{


public override void ItemAdding(SPItemEventProperties properties)
{
string strtemp = properties.AfterProperties["Status"].ToString();

if (strtemp != "Completed")
{
properties.ErrorMessage = "Status should be completed";
properties.Cancel = true;
}
}
}

In the above above code getting the value of Status column field while adding item to the list.


properties.AfterProperties["Status"].ToString(); //IT will get Status filed value after ading value in the column.

In the Condition it is checking Status value ,If status value is equal to Completed it will add item to the list,If status couln is not equal to Completed it will raise an error.

7)Add Strong name to the project


right click on project-->properties-->signinig-->


select sign in assembly-->click on dropdown-->New ->any name-->un select protect my key with password-->say ok


8)Build the applciation

9)Find the dll by opening contaiing folder-->bin/debug path

10)Place in GAC




Adding eventreceviers as a feature

No comments:

Post a Comment

Content Editor Webpart not showing up in SharePoint Online ------------------------------------------------------------------------------...