0

I am using Sharepoint 2013. I have created a custom list. This custom list is using a custom contentype. In this custom contenttype I have a custom column. This column is a people picker. It is possible to select multiple users. In c# code I have created in vs an eventreceiver on itemupdating. I would like to add a new user to my people column. See code below how my code looks:

public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);

            var testManyUsers = properties.ListItem["TestManyUsers"];

            if (testManyUsers is SPFieldUserValueCollection)
            {
                SPFieldUserValueCollection sPFieldUserValueCollection = (SPFieldUserValueCollection)testManyUsers;

                SPUser currentUser = SPContext.Current.Web.CurrentUser;

                sPFieldUserValueCollection.Add(new SPFieldUserValue(properties.Web, currentUser.ID, currentUser.Name));

                SPField spField = properties.ListItem.Fields["TestManyUsers"];
                properties.AfterProperties[spField.InternalName] = sPFieldUserValueCollection;

            }
        }

Ive got this error:

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

2

You need to disable the readonly property, enable AllowUnsafeUpdates, update the item and update the list:

myWeb.AllowUnsafeUpdates = true;

                    //Set the read only mode of people picker column to FALSE
                    currentitem.Fields["Betrokken specialisten"].ReadOnlyField = false;


SPFieldUserValueCollection sPFieldUserValueCollection = (SPFieldUserValueCollection)values;

                            SPUser currentUser = properties.Web.CurrentUser;

                            sPFieldUserValueCollection.Add(new SPFieldUserValue(properties.Web, currentUser.ID, currentUser.Name));

                            //Setting the property of people picker column
                            currentitem["Betrokken specialisten"] = sPFieldUserValueCollection;

                            //Update List Item
                            this.EventFiringEnabled = false;

                            currentitem.Update();
                            //Update List
                            currentList.Update();

                            //Set the read only mode to TRUE
                            currentitem.Fields["Betrokken specialisten"].ReadOnlyField = true;

                            //Update the List item again
                            currentitem.Update();

                            //update the List again
                            currentList.Update();

                            this.EventFiringEnabled = true;
| improve this answer | |

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.