Home > Development, SharePoint 2007 > Getting the SPUser Property at an event

Getting the SPUser Property at an event

Lets say we have a list containing the attending people of a meeting. On the same site we also have a list of the people that are not attending the meeting (absent). To make sure people that are on the attending list aren’t put on it twice, the obvious choice is to create an eventhandler that checks if the user is already in either of the list. Piece of cake. Or not? The code to achieve this seems quite obvious:

public override void ItemAdding(SPItemEventProperties properties)
using (SPWeb web properties.OpenWeb())
                CheckForDoubles(properties, web)
private static void CheckForDoubles(SPItemEventProperties properties, SPWeb web)
            SPUser user 
SPList attendinglist web.Lists[“Attending”];
SPQuery query = new SPQuery();
query.Query = string.Format(“<Where><Eq><FieldRef Name=\”Persoon\” /><Value Type=\”User\”>{0}</Value></Eq></Where>”, user.Name);
SPListItemCollection attendingitems attendinglist.GetItems(query);
SPList absentlist web.Lists[“Absent”];
SPQuery queryafw = new SPQuery();
queryafw.Query = string.Format(“<Where><Eq><FieldRef Name=\”Persoon\” /><Value Type=\”User\”>{0}</Value></Eq></Where>”, user.Name);
// execute the query    
SPListItemCollection absentitems absentlist.GetItems(query);
((absentitems!= null &;&absentitems.Count >0) || (attendingitems != null &;&attendingitems.Count >0))
= string.Format(“The user {0} is already in the attending or absent list.”, user.Name);
properties.Status SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;

And it worked perfectly on my dev box. But for some reason, when deploying it to the acceptance/test environment, this piece of code no longer worked. After checking out the afterproperties (see my other post) I found out that instead of the ID I was getting ;1#domain\username back in the acceptance environment. I haven’t quite figured out why, but to solve it for both systems (and to prevent problems in the future), I changed the code to:

SPUser user;
(properties.AfterProperties[“Persoon”].ToString().Split(‘#’).GetLongLength(0) > 1)
user web.AllUsers.GetByID(Convert.ToInt32(properties.AfterProperties[“Persoon”]));

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: