SharePointCommunity
Die deutschsprachige Community für SharePoint, Microsoft 365, Teams, Yammer und mit Azure
Issue with the AfterProperties of the SPItemEventProperties

Blogs

Oliver Wirkus

Syndication

Certifications

Web 2.0

http://smits-net.de/img/linkedin_button.gif

XING

http://i80.photobucket.com/albums/j191/mikesamerica/twitter_button_zpsee74f711.png

 

 Add to Technorati Favorites

 

 Delicious Bookmark this on Delicious

 

  Locations of visitors to this page

 

Today I stumbled upon a strange behavior of SharePoint. I created an Event Receiver that handled the ItemAdding and the ItemUpdating events of a generic list. This list had a field of type ‘user’.

image

After I implemented the Event Receiver I started a test. First I added a new item to the list and the Event Receiver read the value of this user field.

The Event Receiver had been called and it read the value of the user field like this:

var userOrganizerValue = new SPFieldUserValue(properties.Web, properties.AfterProperties["CnfBuchungOrganizer"].ToString());
var userOrganizer = properties.Web.EnsureUser(userOrganizerValue.LookupValue);

The variable 'userOrganizer' had this value: {i:0#.w|devsp2013\demouser} – and this was the expected value. This string represented the user that I had added to the user field.

Let’s have a quick look on that value:

[i:] : Claims Identity

[0] : reserved

[#] : Logon

[.] : Claims Type string

[w] : ‘Windows’

[|] : Separator

[devsp2013\demouser] : Login Name

(SharePoint 2013: Claims Encoding]

 

After that I tested the code for the ItemUpdating event. Again the Event Receiver read the value for the (unchanged) user field.

var test = properties.AfterProperties["CnfBuchungOrganizer"].ToString();

But now the AfterProperties collection returned this value: 17

I did expect the value of the user field to be the same as before – but this time the AfterProperties just returned an int value only.

That’s why the next lines of code created an error (obvious to see):

var userOrganizerValue = new SPFieldUserValue(properties.Web, properties.AfterProperties["CnfBuchungOrganizer"].ToString());
var userOrganizer = properties.Web.EnsureUser(userOrganizerValue.LookupValue);

 

After I did some research I found out that the AfterEvent properties do not return a user identification in case of an ItemUpdating event – but the index of the corresponding user only.

So I changed my code this way:

var strUserID = properties.AfterProperties["CnfBuchungOrganizer"].ToString();
var userOrganizer = properties.Web.SiteUsers.GetByID(Convert.ToInt32(strUserID));

 

With this change the variable 'userOrganizer' got the expected value – finally.

 

Although I did not check this, but it looks like that the AfterProperties return an int value only for every field type that uses indexed values (Choice fields, Lookup fields) when invoked in an ItemUpdating event.

SharePoint is always good for a surprise :-)

 

TweetThisButton


Bereitgestellt 22 Nov 2013 10:16 von Oliver Wirkus
Gespeichert unter: , ,