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());
Hii,
i like your blog. i have one question. How to display proper message or display message box by updating event in datasheet view.
It works fine for Editform.aspx but in datasheet view it says Unexpected Error.
I used properies.errormessage = “”;
properties.cancel = true;
so far you can not do it. no custom error messages for datasheetview.
Thanks very a lot for this great post;this is the form of factor that keeps me going through these day. I’ve been looking around for this site after being referred to them from a buddy and was pleased when I discovered it after searching for some time. Being a avid blogger, I’m happy to see others taking initivative and contributing to the community. Simply needed to comment to indicate my appreciation on your article because it’s very appleaing, and plenty of writers do not get authorization they deserve. I’m sure I’ll drop by again and can suggest to my friends.
Everything is very open with a precise clarification of the issues.
It was definitely informative. Your website is useful.
Many thanks for sharing!
I’ve been surfing online more than 2 hours today, and this is the best article I’ve come across. I’m a blog post fiend so I’ve actually read a lot already.In my opinion, if all site owners and bloggers made as great content as youhave, the web would bea lot more useful than ever before.