Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am trying to write some code that updates a list item when a list edit form (EditForm.aspx) opens and closes.

I have written a function and assigned it to the onload and onbeforeunload events of the forms body element. The function gets a list item and then in the succeededCallback function of executeQueryAsync() it updates the item and calls executeQueryAsync() again.

When the form opens, the code runs without issue and the list item is updated, however when the form closes the function runs, but when it gets to executeQueryAsync(), neither succeededCallback or failedCallback functions are called and the list item is not updated.

Here is the code:

function updateItem() {

    var clientContext = SP.ClientContext.get_current();
    var web = clientContext.get_web();

    var oList = web.get_lists().getByTitle('MyList');

    var title = "TitleOfItemToUpdate";

    var query = new SP.CamlQuery();

    query.set_viewXml("<View><Query><Where><Contains><FieldRef Name='Title'/><Value Type='Text'>" + title + "</Value></Contains></Where></Query></View>");
    var listItems = oList.getItems(query);
    clientContext.load(listItems);
    clientContext.executeQueryAsync(Function.createDelegate(this, onGotItems),     Function.createDelegate(this, this.onQueryFailed));

    function onGotItems() {

        if (listItems.get_count() > 0) {

            var itemToUpdate = listItems.itemAt(0);

            var strVal = itemToUpdate.get_item('MyFieldVal');

            // Do some other stuff with strVal

            itemToUpdate.set_item('MyFieldVal', 'NewValue');

            itemToUpdate.update();
            clientContext.executeQueryAsync(Function.createDelegate(this, function () { alert("Item updated"); }), Function.createDelegate(this, this.onQueryFailed));
        }
    }
}
function onQueryFailed() {
    alert("failed");
}

The code above is a simplification of the code that i am using. It works perfectly in body.onload, however in body.onbeforeunload it enters updateCreateAccessLog and when it gets to executeQueryAsync it just stops and doesn't call either the success or failure functions passed in as delegates.

Can anybody help me?

Many Thanks,

Toby

share|improve this question
    
Is the form closing too? Or does it fall over and not save? –  Russell Dec 22 '11 at 21:03

1 Answer 1

If the form closes, the JavaScript process is not going to continue and your function is probably not going to execute.

Even though the request is asynchronous, the JavaScript memory space will disappear when a page is reloaded (edit form is saved).

Is this for auditing purposes? You could use other logs and auditing tools which would make this much easier.

If it is to set lock on an item, you can use your JS onload to set the lock, and a custom workflow to release the lock when the item is edited (or times out). You could also use a .NET event receiver to set and release locks (or update an item before/after edit is open).

share|improve this answer
    
Thanks Russell, this is to lock an item, or at least to warn concurrent users that another user already has the item open and they might not be able to save their changes. Is it possible that you could elaborate: if i used a custom workflow, how would i trigger it so that the lock is released when the edit form closes? If i used a SP event receiver, which event could i attach to that will fire when the edit form is opened/closed, i thought that these would only fire if the item is actually changed (the user might press cancel). Thanks, Toby –  toby Dec 24 '11 at 12:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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