How can I tell in a Sharepoint 2010 event receiver if the list item was added in datasheet view or not?
-
10-12-2019 - |
Question
I'm a novice Sharepoint developer, so please forgive what is probably a very simple question.
I have an event receiver on a custom list in Sharepoint 2010. I need to be able to display custom error messages, and have it working fine in the standard Add New Item form. I just found out that some of the users of this list prefer to add items in datasheet view, so I found how to display a custom error message in datasheet view. The code to do the error handling is different, though, so I need some way to programmatically tell if the item was added via datasheet view. I have searched and searched and have been unable to find a way to do this. Does anyone know how?
Here's my code, with an imaginary boolean, isDataSheetView, added showing what I'd like to be able to do:
//somehow determine if it's datasheet view, and set isDataSheetView accordingly
if (isDataSheetView)
{
// Cancel with error, for datasheet view
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.ErrorMessage = errorMsg.ToString();
}
else
{
//Cancel with redirect URL, for non-datasheet entries
properties.Status = SPEventReceiverStatus.CancelWithRedirectUrl;
properties.RedirectUrl = "/_layouts/CheckReagentInventory/CasError.aspx?Error=" + HttpUtility.HtmlEncode(errorMsg.ToString());
this.EventFiringEnabled = false;
}
Thanks for any advice, Liam
Solution
This is a known limitation of the datasheet because it uses the lists.asmx web service on the backend. There really is no way to do what you want to do cleanly. Here re your options as far as I can see:
Create a custom UI using something like Kendo UI's gridview and ListData.svc so that you can add your own validation in between SharePoint and the user. Disabl datasheet on the list and then create a custom action button to access your application page with the custom UI.
Not use
CancelWithRedirect
and instead use another method to handle the error state that could be applicable to both views. One possible solution might be to add a field calledException Status
and update that field with an appropriate value to alert the user. You could also use an email alert to notify them.
My choice would probably be the first. If you'd like some additional information on the 1st option, just create another question and add the link as a comment here. I have a lot of experience with ListData.svc
and could help you out with some of its nuances.