Monday, September 19, 2016

Validating SharePoint list data by using PreSaveAction involving JSOM Asynchronous call

In the previous post I had shared validating SharePoint list data that invlolved JSOM asynchronous call by overriding the PreSaveAction . The same requirement can also be accomplished without over riding the presaveaction.

Approach would be when the call is succeded or failed I am creating delegate on executeQueryAsync call with call back. On success we will posting the data manually to SharePoint list or on failure I willnt post any data to SharePoint list by returning false.



Output


Friday, September 16, 2016

Validating SharePoint list data by overriding PreSaveAction involving JSOM Asynchronous call

SharePoint supports ExecuteQueryAsync and when we call ExecuteQueryAsync it executes the object model code declared and carries on and returns the ouput at some point in future without waiting for subsequent code. Most of the time we would expect our code to act on result of call.

This is challenging particular when we use it with presaveaction that would expect true or false and ExecuteQueryAsync returns output infuture. PreSaveAction in SharePoint does validation before submitting an item to SharePoint list.

Solution: By using jquery I had added custom OnClick fumction to the existing Save button. The custom function does the validation and once it is completed I had called orginial Save function to complete the saving process.

In below code I am using JSOM ExecuteQueryAsync to fetch status of SharePoint list item field. Based on status of field and I am comparing it other field as part of business validation. During the validation I decide whether to save the item or not. If the item is valid I am calling orginal save function or else just an alert will be displayed to user that there is problem with request. I had used this code in editform.aspx.




Output