One of friend came up with this error while updating a lookup column in a list in SharePoint event handler.
When updating the look up column, we should not assign the value directly. Instead,we need to assign the Id of the lookup value.
I have a lookup column Country which gets the value from another list named “Country”.
If I update the value as
item[“Country”]=”India”;
results in the above error : Invalid data has been used to update the list item. The field you are trying to update may be read only.
Here I wrote one helper function which will return the SPLookupFieldValueCollection which can be set to the field.
- Call this method like this
item[“Country”] = GetLookFieldIDS(“Australia”, lookupFieldList);
- If the LookupField allows multiple values
item[“Country”] = GetLookFieldIDS(“Australia,India”, lookupFieldList);
If you come to this error when creating a Custom Field within SharePoint it may be because you have the ParentType set to “Lookup” and the value that is getting saved is a string type. Make sure the value that is getting saved by your custom control is a integer type.
By: chris on November 30, 2010
at 5:39 pm
Thank you so much for this. It helped me! 😀
By: Caroline Tan on August 25, 2011
at 1:46 am
Thanks for this article.. you saved my day.. 🙂
By: Ankit on December 20, 2011
at 12:06 pm
Great.. saved my time.. thanks
By: Srinivas on January 19, 2012
at 11:58 am
Gr8 , Saved my day….Thanks
By: Aamir on February 11, 2012
at 1:57 pm
Really useful… 🙂
By: Mafas on May 24, 2012
at 6:46 am
I created a custom user field and encoutnered this issue. Instead of assigning an SPUser object to the field I assigned the integer ID of the SPUser and my field now works. Thanks!
By: Mehul on June 29, 2012
at 9:01 pm
[…] I already write a blog on updating the Lookup field using the Server Object Model. Here is the blog post for reference. Click here […]
By: Update Lookup Field using Client Object Model « *****## R.MARAN. ## ***** on July 16, 2012
at 1:28 pm
thank you very much………you really saved my day……..
By: subhash on October 4, 2012
at 8:36 am
thanks, you rock
By: nave on January 16, 2013
at 10:40 pm