Folks, I have a custom button on SharePoint form with an onClick event that is as follows:

onclick="if (!itemDuplicated()){ddwrt:GenFireServerEvent('__commit;__redirectsource')}; window.alert('Item updated.') ; window.close();"

The script to handle the click event is as follows:

                function itemDuplicated(){

                        var field1 = document.getElementById("ctl00_m_g_7d2eac47_1ef1_4ab4_96b7_38089fea54a1_ff21_ctl00_ctl00_TextField").value;

                    var siteUrl = 'http://myurl';

                    var clientContext =  new SP.ClientContext.get_current();

                    var oList = clientContext.get_web().get_lists().getByTitle("RFC Field Grouping (kerry test of concept)");

                    var camlQuery = new SP.CamlQuery();
                    camlQuery.set_viewXml(&apos;<View><Query><Where><Eq><FieldRef Name='field1'/><Value Type='Text'>&apos; + field1 + &apos;</Value></Eq></Where></Query></View>&apos;);

                    this.collListItem = oList.getItems(camlQuery);                          

                    clientContext.load(collListItem, &apos;Include(Title)&apos;);

                    clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),Function.createDelegate(this, this.onQueryFailed));

                }  //end function

in the this.onListItemsLoadSuccess function I am easily able to get the value I need from the caml query but because it is running Async it never gets considered and the itemDuplicated function always returns false.

I have read a lot about this and how it's a difficult thing to accomplish, making asynchronous calls synchronous (not recommended) and using callbacks, etc

I have tried a bunch of different things to no avail. Is there any way to accomplish this?


Change the onclick event to just call your function


Then rewrite the onListItemsLoadSuccess function to handle your if statement

function onListItemsLoadSuccess()
   //Check if duplicated and then run code if we have duplicate
  • Thanks so much for taking the time to answer. This is a great suggestion and one I had explored initially. However, the problem I ran into was trying to submit the form with javascrpt. I.e. If no duplicates were found, then I want to simply submit the form. Plugging GenFireServerEvent('__commit;__redirectsource') into the javascript didn't work...nor did the standard document.forms[0].submit() or document.getElementByID("aspnetForm).submit() This is such a simple thing but yet it is so difficult to accomplish. Any suggestion? Thanks again.
    – Kerry
    Nov 29 '17 at 14:39
  • ddwrt:GenFireServerEvent probably goes out of scope depending on how you wire the async callback. Try storing the function ddwrt:GenFireServerEvent in a variable you can reach from onListItemsLoadSuccess() function. If you just want to try it out and play ugly you can do: window["myfunc"] = ddwrt:GenFireServerEvent; And then call it with: window.myfunc('__commit;__redirectsource') Nov 29 '17 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.