<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Cliff Green's Blog : Event Receivers</title><link>http://blogs.msdn.com/cliffgreen/archive/tags/Event+Receivers/default.aspx</link><description>Tags: Event Receivers</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Understanding event receivers in a SharePoint survey</title><link>http://blogs.msdn.com/cliffgreen/archive/2008/04/06/understanding-event-receivers-in-a-sharepoint-survey.aspx</link><pubDate>Sun, 06 Apr 2008 04:19:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8361960</guid><dc:creator>green.cliff</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/cliffgreen/comments/8361960.aspx</comments><wfw:commentRss>http://blogs.msdn.com/cliffgreen/commentrss.aspx?PostID=8361960</wfw:commentRss><description>&lt;p&gt;SharePoint provides great support for creating surveys in your sites.&amp;nbsp; Under the covers this is just a list, like everything else in SharePoint.&amp;nbsp; The difficult thing with a survey is the ability to determine when it is complete.&amp;nbsp; The survey feature gives you the ability to create multiple pages in your survey by putting page breaks between questions.&amp;nbsp; When we have a survey with multiple pages the event handlers fire in the following order...&lt;/p&gt; &lt;p&gt;After the first page is submitted the ItemAdding and ItemAdded events fire.&lt;/p&gt; &lt;p&gt;Subsequent paging clicks fire the ItemUpdating and ItemUpdated events.&lt;/p&gt; &lt;p&gt;Even after the survey is completed and the user returns to update the information the ItemUpdating and ItemUpdated events fire.&lt;/p&gt; &lt;p&gt;I built the following utility method to help figure some of this out.&amp;nbsp; With event handlers it is really helpful to determine what is available.&amp;nbsp; All it really does is take the list item XML, BeforeProperties, AfterProperties and write them out to a text file.&amp;nbsp; Typically the Before/AfterProperties give all the detail we need, but there are several fields or attributes that are hidden and made available in the XML.&amp;nbsp; One thing to note is that the BeforeProperties collection is only populated for document libraries, so we don't expect it to be populated for this exercise.&lt;/p&gt; &lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; WriteProperties( SPItemEventProperties properties, &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; fileName ) {
    Stream stream = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileStream( &lt;span style="color: #006080"&gt;@"c:\" + fileName, FileMode.Create, FileAccess.Write );
    StreamWriter writer = new StreamWriter( stream );
    writer.WriteLine( "&lt;/span&gt;******** Fields **********&lt;span style="color: #006080"&gt;" );
    writer.WriteLine( "&lt;/span&gt;List ID: &lt;span style="color: #006080"&gt;" + properties.ListId.ToString() );
    writer.WriteLine( "&lt;/span&gt;List Item ID: &lt;span style="color: #006080"&gt;" + properties.ListItemId.ToString() );
    
    
    if( properties.ListItem != null ) {
        SPListItem item = properties.ListItem;
        writer.WriteLine( item.Xml );
    }
    
    writer.WriteLine( "&lt;/span&gt;******** Before Properties ********&lt;span style="color: #006080"&gt;" );
    foreach ( DictionaryEntry entry in properties.BeforeProperties ) {
        writer.WriteLine( String.Format( "&lt;/span&gt;{0}: {1}&lt;span style="color: #006080"&gt;", entry.Key, entry.Value ) );
    }

    writer.WriteLine();
    writer.WriteLine();

    writer.WriteLine( "&lt;/span&gt;******** After Properties ********&lt;span style="color: #006080"&gt;" );
    foreach ( DictionaryEntry entry in properties.AfterProperties ) {
        writer.WriteLine( String.Format( "&lt;/span&gt;{0}: {1}", entry.Key, entry.Value ) );
    }
    writer.Flush();
    writer.Close();
    stream.Dispose();
}&lt;/pre&gt;&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;To examine the survey events I wrote an ItemAdded and ItemUpdated event receiver and just made a call to the WriteProperties method to output the XML and AfterProperties collection.&amp;nbsp; I created a survey that has three questions each separated by a page break.&amp;nbsp; After completing the first question and clicking the 'Next' button the ItemAdding and ItemAdded events fire as noted previously.&amp;nbsp; From the ItemAdded event we see a hidden field in the Xml output called 'ows_owshiddenversion'.&amp;nbsp; The value of this property is '1'.&amp;nbsp; Another field in the Xml is 'ows_Completed' and it has a value of '255'.&amp;nbsp; Neither of the fields mean much to us right now, but becomes important in the next steps.&amp;nbsp; &lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;In completing the second question (page) the ItemUpdating and ItemUpdated events are fired.&amp;nbsp; The 'ows_owshiddenversion' field is incremented to '2' in the item.Xml and is added to the AfterProperties collection, although the value there is still '1'.&amp;nbsp; The 'ows_Completed' field retains its previous value of '255'.&amp;nbsp; I added some code to the ItemUpdated event in order to track the value of the Completed field.&amp;nbsp; The updated ItemUpdated event is shown below.&lt;/div&gt;
&lt;div&gt;&amp;nbsp;&lt;/div&gt;
&lt;div&gt;&lt;pre style="padding-right: 0px; padding-left: 0px; font-size: 8pt; padding-bottom: 0px; margin: 0em; overflow: visible; width: 100%; color: black; border-top-style: none; line-height: 12pt; padding-top: 0px; font-family: consolas, 'Courier New', courier, monospace; border-right-style: none; border-left-style: none; background-color: #f4f4f4; border-bottom-style: none"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;override&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ItemUpdated( SPItemEventProperties properties ) {
    WriteProperties( properties, &lt;span style="color: #006080"&gt;"surveyitemupdated.txt"&lt;/span&gt; );

    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; ( properties.ListItem[&lt;span style="color: #006080"&gt;"Completed"&lt;/span&gt;] != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; ) {
        WriteProperties( properties, &lt;span style="color: #006080"&gt;"surveycompleted"&lt;/span&gt;+ properties.ListItem[&lt;span style="color: #006080"&gt;"Completed"&lt;/span&gt;].ToString() +&lt;span style="color: #006080"&gt;".txt"&lt;/span&gt; );
    }
}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The next step is to complete the third and last question.&amp;nbsp; The button for the form changes from a 'Next' button for the previous questions to a 'Finish' button.&amp;nbsp; Completing the question and clicking the 'Finish' button again calls the ItemUpdating and ItemUpdated event.&amp;nbsp; This time the AfterProperties collection shows the 'owshiddenversion' value to be '2' and the 'Completed' field changes from '255' to '1'.&amp;nbsp; The 'ows_owshiddenversion' field is continually incremented as we change the value of our survey and go from page to page.&amp;nbsp; Since the underlying survey item is updated between page requests it just provides a type of version number.&amp;nbsp; Given that a user can come back and edit a survey at any time, and SharePoint starts the user off with the first question in the survey when 'Edit' is selected it is difficult to determine anything from this value.&lt;/p&gt;
&lt;p&gt;The one thing we can determine is when the survey is completed, although we can only tell when it is completed the first time.&amp;nbsp; After that the completed attribute or field retains the value of '1' throughout the lifetime of the list item.&amp;nbsp; So we know that it was completed at some time in the past, but not much else.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8361960" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/SharePoint/default.aspx">SharePoint</category><category domain="http://blogs.msdn.com/cliffgreen/archive/tags/Event+Receivers/default.aspx">Event Receivers</category></item></channel></rss>