Back to Cleverworkarounds mainpage
 

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();

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blogmarks
  • StumbleUpon
  • Technorati

No Tags

 

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>


Today is: Monday 8 September 2008 -