Here’s an issue that nearly brought me to my knees… almost. I was stressing out all weekend wondering why an ItemUpdating event wasn’t running in Datasheet view, even though it ran fine and dandy when used with the editform.aspx. When used with the Datasheet view, either unknown or conflict errors would prevent the user from committing any changes to the SharePoint List.

Scenario Overview: Custom event handler for a SharePoint List with lots of columns. The ItemUpdating logic needs to compare various dates and number fields to determine if the data could be submitted. Basically the raised event was performing data validation for the List Item being updated.

EditForm Scenario: The ItemUpdating event worked without a hitch. I was able to grab and work with all values coming from the property bag. The end result returned nothing unexpected.

public override void ItemUpdating(SPItemEventProperties properties)
{
int deliveryQty = int.Parse(properties.AfterProperties[IFN_UpdatedDeliveryQty].ToString()); 
}

Datasheet Scenario:Running the same code, the ItemUpdating event would cause errors in the Datasheet view and forces the user to discard their changes. So I fired up the debugger and noticed that the second property that I was trying to access returned null values in the Datasheet view even though it returned the valid objects in the EditForm. This was totally perplexing to me and for the longest time I thought that it had something to do with the Datasheet view being asynchronus and losing its context everytime an item was edited.

Then this morning, I had a friend look at the line of code that was returning the null problem: 

int deliveryQty = int.Parse(properties.AfterProperties[IFN_UpdatedDeliveryQty].ToString()); 

He suggested to instead use a different method to retrieve the values:

string deliveryQty = Convert.ToString(properties.AfterProperties[IFN_UpdatedDeliveryQty]);

I then had to update some code to accommodate and convert the strings to the ints that I had originally wanted and everything worked in both the EditForm and Datasheet view! Woohoo!

Lesson Learned: Correct me if I’m wrong, I guess for some reason MSFT didn’t implement the .ToString() method for people that wanted to retrieve information from the properties bag when an ItemUpdating event is fired from the Datasheet view. So the right way to access the AfterProperties would be to use

Convert.ToString(properties.AfterProperties[IFN_UpdatedDeliveryQty]);

instead of

int.Parse(properties.AfterProperties[IFN_UpdatedDeliveryQty].ToString());

Advertisement