0

There is a field in my list called Authority Name and its type is Person. I am writing ItemAdding event receiver where I want to change this fields value.

For e.g. if user is adding Mike in this field then I want to replace it with John. I have tried both SPUser and SPFieldUserValue but getting error in both.

If I try following where myspuser is a SPUser object then I get error

properties.AfterProperties["Authority_x0020_Name"] = myspuser; 

Error

Invalid data has been used to update the list item. The field you are trying to update may be read only

If I try following where I am trying to conver SPUser to SPFieldUserValue then I get error

SPFieldUserValue myspfielduservalue = new SPFieldUserValue(context.Web, myspuser); 

Error

Value does not fall within the expected range

Is it not possible to update Person or Group field in ItemAdding event?

2
  • Is this person field is single user or multi-user field? Commented Oct 22, 2024 at 9:53
  • Single user field Commented Oct 22, 2024 at 9:56

1 Answer 1

0

I fixed it by using the following code:

SPFieldUserValue myspfielduservalue = new SPFieldUserValue(context.Web, myspuser.ID + ";#"+ myspuser.LoginName); properties.AfterProperties["Authority_x0020_Name"] = myspfielduservalue; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.