2

I am not able to get user field on AfterProperties in EventReceiver. I am using ItemUpdating event as I want to compare old value and new value. But I am getting error while fetching user field using properties.AfterProperties[fieldname].

 SPFieldUserValueCollection newValue = new SPFieldUserValueCollection(properties.Web.Site.RootWeb, Convert.ToString(properties.AfterProperties["fieldname"]));

In newValueI am getting -1;#i:0#,w|domain\user

Getting Id as -1

Below is the screenshot

enter image description here

1
  • what error do you get?
    – Gwny
    Feb 16 '16 at 11:09
2

Your problem here is that the user from afterProperties hasn't been ensured yet and therefore doesn't have the right ID from the Users List (-1).

You could solve your Problem by calling

properties.Web.EnsureUser("claim|domain\user");

for every user before using it in SPFieldUserValueCollection.

If you just need the login name you can use a normal SPFieldLookupValueCollection instead of SPFieldUserValueCollection.

Example:

 SPFieldLookupValueCollection newValues = new SPFieldLookupValueCollection (Convert.ToString(properties.AfterProperties["fieldname"]));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.