Monday, June 23, 2008

Event Handlers

If you are working with event handlers in sharepoint and having troubles updating, changing properties and roles plz remember:

in short: IF you're trying to impersonate using RunWithElevatedPrivileges, ALWAYS follow the practice of creating a NEW SPSite inside the RunWith... block. You can grab the current site ID from the event properties object, and then instantiate a new SPSite by e.g.:

using (SPSite theSite = new SPSite(properties.OpenWeb().Site.ID)) {...}

o after some research, here is my solution: item[fieldnamehere] = fieldvaluehere; this.DisableEventFiriing(); item.SystemUpdate(false); this.EnableEventFiring();

This piece of code updates the item without firing the current event handler events, and doing the update using the SystemUpdate(false) method ensures that version number doesnt increase and the update is considered as if it was part of the user's update.

found one thing about this code. You don't need to do the Item.Update() command when you change Roles atribute of the item.
This Prevents unexpected errors or strange behaviours when you attach this event handler to an Publishing pages Library or some list with version control and Check-in Check-out control to allow item changes.
It worked well to me this way.

these sentences saved my afternoon, read them in their respective blogs

EventHandler Impersonation

Preventing Event Handler Recursion

Uploading a file eventhandler

event handler to set permissions

great great resources