{"id":3980,"date":"2012-07-27T20:31:56","date_gmt":"2012-07-27T20:31:56","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/setting-boolean-values-in-a-sharepoint-spitemeventreceiver\/"},"modified":"2016-07-28T10:51:18","modified_gmt":"2016-07-28T10:51:18","slug":"setting-boolean-values-in-a-sharepoint-spitemeventreceiver","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/setting-boolean-values-in-a-sharepoint-spitemeventreceiver\/","title":{"rendered":"Setting Boolean Values in a SharePoint SPItemEventReceiver"},"content":{"rendered":"<p>One of the confusing things about implementing an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventreceiver.aspx\">SPItemEventReceiver<\/a> that modifies properties on an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.splistitem.aspx\">SPListItem<\/a> is that while you are given an actual <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.splistitem.aspx\">SPListItem<\/a> with which to work, you&#8217;re really not supposed to use it. Instead, you should be modifying properties in the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.afterproperties\">AfterProperties<\/a> property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.aspx\">SPItemEventProperties<\/a> parameter passed into the method. <\/p>\n<p><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.afterproperties\">AfterProperties<\/a> is an <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventdatacollection\">SPItemEventDataCollection<\/a> object, which is really just a glorified hash table containing key\/value pairs representing properties on the item and the value of those properties. There are two annoyances that I have found with using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.afterproperties\">AfterProperties<\/a> to set values:<\/p>\n<p>1.) You cannot use the GUID of a field to set a value, you have to use the textual name of the field. This means you need to use either the Name \/ Static Name of the field to set the values. In our projects, the Name \/ Static Name is set to the same value, so I&#8217;m really not 100% sure if Name works, Static name works, or they both work, so you may have to play around with it.<\/p>\n<p>2.) You apparently can&#8217;t just set a Boolean value using true \/ false. You have to specify the value as &#8220;TRUE&#8221; or &#8220;FALSE&#8221; for the value to take, e.g.,<\/p>\n<p><b>\/\/DOES NOT WORK:     <br \/><\/b>properties.AfterProperties[&#8220;FieldName&#8221;] = true;     <br \/><b>\/\/WORKS:<\/b>    <br \/>properties.AfterProperties[&#8220;FieldName&#8221;] = &#8220;TRUE&#8221;; <\/p>\n<p>Based on how the fields are defined in content types, you may also have some success with &#8220;YES&#8221; and &#8220;NO&#8221; as values as well, but I know that the &#8220;TRUE&#8221; and &#8220;FALSE&#8221; values work fine. One of the notes in the documentation on Boolean values states:<\/p>\n<p><i>When a Boolean column value in a list is set to true and the <\/i><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventreceiver.itemupdating\"><i>ItemUpdating<\/i><\/a><i> method is called, the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spitemeventproperties.afterproperties\">AfterProperties<\/a> property reports -1 as the value instead.<\/i><\/p>\n<p>This may have something to do with the oddities as well.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the confusing things about implementing an SPItemEventReceiver that modifies properties on an SPListItem is that while you are given an actual SPListItem with which to work, you&#8217;re really not supposed to use it. Instead, you should be modifying properties in the AfterProperties property of the SPItemEventProperties parameter passed into the method. AfterProperties is&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3980","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3980","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3980"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3980\/revisions"}],"predecessor-version":[{"id":25566,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3980\/revisions\/25566"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3980"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}