June 29, 2010

SharePoint Event Handlers

Event handling ?

The ability to catch certain user actions in code and respond programmmatically. These are the classes which we can inherit and write event handlers or receivers against them.

SPItemEventReceiver

SPListEventReceiver

SPWebEventReceiver

Which user actions you can catch?

ItemFileMove,IFMoving, ItemUpdated,ItemUpdating, ItemCheckedIn/Out, ItemCheckingIn/ItemChecingOut, ItemAdded,IAdding, ItemAttachementAdded/Adding, ItemDeleted/Deleting.

What is template id for customlist, document library, survey?

100 Custom List 101 Document Library 102 Survey 103 Links List 104 Announcements 105 Contacts

106 Events List 107 Tasks List 108 Discussion Board 109 Picture Library

Explain the steps for creating event handlers?

1) Create a class library project.

2) Add reference to Microsoft.Sharepoint.dll

3) Inherit the class SPItemEventReceiver

4) Override the method against which you want to attach the event handler.

e.g. for itemdeleting

namespace MyEventHandler{

public class ItemDeletingHandler : SPItemEventReceiver{

public override void ItemDeleting(SPItemEventProperties properties){

if(properties.ListTitle == “MyDocumentLibrary”){

// only the admin user has right to delete the uploaded document

if (!properties.UserLoginName.Contains(“domainnameadminUserName”)){

properties.ErrorMessage = “You don’t have sufficient privelege to delete on list “+properties.ListTitle;

properties.Cancel = true;}}}}}

5) Sign the assembly and put it in GAC i.e. c:windowsassembly

6) Create a new folder with the same name as your eventhandler at the following path

c:program filescommon filesmicrosoft sharedweb server extension12templateFeatures folder

7) Create two files feature.xml and elements.xml at the newly created folder.

Take feature.xml file of announcement and make the necessary changes to it.

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

<Feature Id=“C50FF499-B52A-471f-A9FB-36A49E2FA49F“

Title=“MyEventHandler“

Description=“For restricting delete of a list“

Scope=“Web“

xmlns=“http://schemas.microsoft.com/sharepoint/“>

<ElementManifests>

<ElementManifest Location=“Elements.xml“/>

</ElementManifests>

</Feature>

9) Within elements.xml specify

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

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

<Receivers ListTemplateId=“101“>

<Receiver>

<Name>MyEventHandler</Name>

<Type>ItemDeleting</Type>

<SequenceNumber>20000</SequenceNumber>

<Assembly>

MyEventHandler, Version=1.0.0.0,

culture=neutral, PublicKeyToken=e185cb2eba5d0774

</Assembly>

<Class>MyEventHandler.ItemDeletingHandler</Class>

<Data></Data>

<Filter></Filter>

</Receiver>

</Receivers>

</Elements>

Get the values for assembly tag by right clicking the assembly and selecting properties within the GAC.

If we have multiple eventhandler attached to a same event we can specify which handler should be executed first through SequenceNumber tag. It’s value should start from 20000

10) Now install the feature

stsadm.exe -o installfeature -filename MyEventHandlerfeature\Feature.xml

11) Activate the feature

stsadm -o activatefeature -filename MyEventHandlerfeature\Feature.xml -URL http://localhost.

No comments:

SonarQube with Jenkins Setup using Docker Images

https://funnelgarden.com/sonarqube-jenkins-docker/  https://medium.com/@hakdogan/an-end-to-end-tutorial-to-continuous-integration-and-con...