Thursday, March 29, 2012

Creating List Event Receivers in SharePoint 2010 Lists

I created this list event receiver blog just for an example and to clear few basic questions I had. I have created using Visual Studio 2010 and for SharePoint 2010 list, however, this would be same for SharePoint 2007, you can check my other blog for further reference.

The steps

1.       In visual studio select Event Receiver project .

2.       Give a name like – ListDeleteEventReceiver select the type of list (Generic list

3.       In the SharePoint Customization Wizard, select the An item is being deleted option, and then click Finish.

clip_image002

4.       Then open the Elements.xml file.

5.       To make sure that the event receiver only binds to the custom list, ensure the ListTemplateId attribute in the Receivers element is set to 100.

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Receivers ListTemplateId="100" >

      <Receiver>

        <Name>EventReceiver1ItemDeleting</Name>

        <Type>ItemDeleting</Type>

        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

        <Class>ListDeleteEventReceiver.ListItemDeleteER.ListItemDeleteER</Class>

        <SequenceNumber>10000</SequenceNumber>

         </Receiver>

  </Receivers>

</Elements>

6.       In Solution Explorer, expand open ListItemDeleteER.cs class file. You should see the overridden ItemDeleting method.

7.       Insert the following code into the body of ItemDeleting method after the base.ItemDeleting(properties); statement. This code prevents the deletion of an item and display an error message.

public class ListItemDeleteER : SPItemEventReceiver

    {

       /// <summary>

       /// An item is being deleted.

       /// </summary>

       public override void ItemDeleting(SPItemEventProperties properties)

       {

           base.ItemDeleting(properties);

           try

           {

               properties.Cancel = true;

               properties.ErrorMessage = "Item cannot not be deleted!";

           }

           catch (Exception ex)

           {

               return;

           }

           finally

           {

               this.EventFiringEnabled = true;

           }

       }

    }

Deploy and check.

The most important point here is the how this works.

1.     How this event receiver would be targeted for my specific list?

2.     If I do not mention my target list then what?

To Answer these questions, I would refer to the Elements.xml file. Since I have mentioned only   <Receivers ListTemplateId="100" >.

This would target all lists with template id 100 (refer below for all the list template ids).  I will not be able to delete items from any generic list .

 

Now to make it target a specific list we handle this in various ways. I have mentioned the easiest one and have mentioned the rest, which I will try to put in later.

1.     Change in the elements.xml.

<Receivers ListUrl="Lists/TestER" >

Can loop in the override method get the list and check column and do the rest

public override void ItemDeleting(SPItemEventProperties properties)

       {

           base.ItemDeleting(properties);

           try

           {

               SPList mylist = properties.List;

Do the logic here……..

               properties.Cancel = true;

               properties.ErrorMessage = "deleted!";

           }

           catch (Exception ex)

           {

               return;

           }

           finally

           {

               this.EventFiringEnabled = true;

           }

       }

List Template Ids

The ListTemplateId defines to which List Types the Event Handler will be targeting. The table below shows which list types there are available in SharePoint

 

ID

Name

100

Generic list

101

Document library

102

Survey

103

Links list

104

Announcements list

105

Contacts list

106

Events list

107

Tasks list

108

Discussion board

109

Picture library

110

Data sources

111

Site template gallery

112

User Information list

113

Web Part gallery

114

List template gallery

115

XML Form library

116

Master pages gallery

117

No-Code Workflows

118

Custom Workflow Process

119

Wiki Page library

120

Custom grid for a list

130

Data Connection library

140

Workflow History

150

Gantt Tasks list

200

Meeting Series list

201

Meeting Agenda list

202

Meeting Attendees list

204

Meeting Decisions list

207

Meeting Objectives list

210

Meeting text box

211

Meeting Things To Bring list

212

Meeting Workspace Pages list

300

Portal Sites list

301

Blog Posts list

302

Blog Comments list

303

Blog Categories list

1100

Issue tracking

1200

Administrator tasks list

2002

Personal document library

2003

Private document library

 

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

 

Reference:

http://koenvosters.wordpress.com/2009/07/31/howto-create-an-event-handler-for-sharepointmoss-2007/

http://msdn.microsoft.com/en-us/library/ms475328(office.12).aspx

http://msdn.microsoft.com/en-us/library/ff728093.aspx

http://apartha77.blogspot.com/2011/04/deploying-item-event-receiver.html

Creating SharePoint 2010 Event Receivers in Visual Studio 2010

Error occurred in deployment step 'Activate Features': Operation is not valid due to the current state of the object.

I faced this problem while deploying an event receiver from Visual Studio 2010.  I searched and found that I had made two mistakes.

No1.  No more than Magical ….

Deploying a feature through Visual Studio 2010 kill the vssphost4.exe process first…

clip_image002

No2. I added an Event Receiver to a new Visual Studio010 project.  It was of type ItemDeleting and I renamed the namespace and the class name of the class.  Well apparently when I did that, I got out of sync with the automatically generated Elements.xml – which I couldn’t find at first. So pop open your “ItemDeleting” file in solution explorer – Double-click the Elements.xml file.  I saw something like this:

<?xml version="1.0" encoding="utf-8"?>

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Receivers ListTemplateId="100" >

      <Receiver>

        <Name>EventReceiver1ItemDeleting</Name>

        <Type>ItemDeleting</Type>

        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>

        <Class>ListDeleteEventReceiver.ListItemDeleteER.ListItemDeleteER</Class>

        <SequenceNumber>10000</SequenceNumber>

         </Receiver>

  </Receivers>

</Elements>

My problem was the <Class> element.  I had renamed my namespace to MyNamespace and my project to MyProjectName.  So my class should have been MyNamespace.MyProjectName.  That was it.

 

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)