SharePoint Event Handlers – things to look out for
Just a really quick post – more event handler stuff to come… (*sigh* – I’m not a coder! Go and read my pal Sezai’s blog if you want a fix in that area).
Situation: I created a list with 2 columns
- skills
- discipline
Skills was a lookup column against a list of skills. Discipline was a single line of text.
I then changed my mind and renamed “skills” to “skilltype” and then created a new “skills” column, also a single line of text.
Net result? Three columns called:
- skills : single line of text
- skilltype : lookup
- discipline : single line of text
I then wrote an event handler that wrote values into skills and discipline columns based on certain conditions.
Here is a code snip of the inherited ItemUpdated() method:
listItem[“Skills”] = “Some skill”;
listItem[“Disclipline”] = “some discipline”;
// write the update but do not trigger events
this.DisableEventFiring();
listItem.SystemUpdate(false);
this.EnableEventFiring();
But it did not work. Attached a debugger to my event handler and saw this error:
{“Invalid data has been used to update the list item. The field you are trying to update may be read only.”}
What did it mean? READ ONLY? I was writing to the single line of text fields (skill and discipline).
It took me a while to twig to the issue. Renaming a column does not change its ‘internal’ name. Using a watch on the list item, the internal field names were:
- skills
- skills0
- discipline
Can you see what happened? Renaming ‘skills’ to skilltype’ left the internal name as ‘skills’. When I added a new ‘skills’ column, its internal name had a number appended ‘skills0’.
So if I changed my code to
listItem[“Skills0”] = “Some skill”;
It would have solved my problem. Is it clever? No! Better to change my code to refer to the internal name of the column. Fortunately, the internal name is exposed via a list item object property.
eg
listItem.Fields[“Skills”].InternalName
returns “skills0”
So I modified my lookup to the following:
listItem[listItem.Fields[“Skills”].InternalName] = “some skill”;
and the problem was solved.
Moral of the story? Play it safe and always use the internal name. Below is the corrected code snippet.
listItem[listItem.Fields[“Skills”].InternalName] = “Some Skill”;
listItem[listItem.Fields[“Disclipline”].InternalName] = “Some Discipline”;
// write the update but do not trigger events
this.DisableEventFiring();
listItem.SystemUpdate(false);
this.EnableEventFiring();
Once again, you have given the answer that I needed! I first came across your articles on branding. As always, your articles are complete, and to the point and relevant to people like me. You know how to address the needs of the Sharepoint non-programming developer.
Thanks
sir!
i owe you my gratitude for such a great post, this just solved my problem. i was hardcoding the 0 along with column name 🙂
or you could just do:
ListItem[“skill”] = blah
ListItem[“skill0”] = blah
It’s easy to find out the internal name.
I really appreciation for this valuable article. It made my day!!!
Thanks,
Himadrish
I am doing this similar kind of but I am not hard coding the values rather I want to capture the value from the Editform.aspx and update in itemadded/itemadding event of document library , please help me since i am nearing deadline
thank you!
big thanks
hi,
i am trying to use itemadding to change the title of a document on upload,
pls advice!
This type of error will happen when you try to update the field with wrong datatype as well.
Eg. Assiged To field is user where as requested by is string.
Actual solution is
newItem[newItem.Fields[“Assigned To”].InternalName] = web.AllUsers[@”Dev\InternetManager”];
This field will not accept string value.
Thanks
Nathan Pillai,London
Thanks a lot dear.
it has helped me to solve the problem related to renaming the fields.