Need help with ItemUpdating when triggered by SPServices

Jul 31, 2012 at 10:00 PM
Edited Jul 31, 2012 at 11:24 PM

Hello,

I have written a custom Event Handler for the ItemUpdating event on a list .  I have two special fields that get updated and an email that gets sent by the Event Handler.  One field is an append changes to multiline text field (history) and the other is a number field.  When users update the list item the history gets updated and the number field is incremented according to a bitmap.  

Now here is the strange thing.  When updating the form through the EditForm both fields get updated and the email gets sent, all is as it should be.  When updating the form through SPServices the history field is updated and the email is sent but for some reason the number field does not get updated.

Any thoughts or suggestions would be greatly appreciated.

 

JT

Coordinator
Jul 31, 2012 at 11:34 PM

No idea, JT. Clearly the event is firing, but there must be something you are doing differently vis a vis the number field.

M.

Aug 1, 2012 at 8:22 PM
Edited Aug 1, 2012 at 8:23 PM

Yeah it's a bit confusing to me.  I update the list item by changing one column's drop down value both through the EditForm and SPServices.  The the following code runs as the event handler.  As I mentioned before the "History" & "Log" fields get updated but the "EmailsSent" field does not when updating via SPServices.  The methods PlannedOrUnplannedChange and EmergencyChange are the process flow for emails.  The UpdateChangeControl method simply disables event firing, does a listItem.SystemUpdate(false), then enables event firing.

// If we get here the user did not make any changes they are not allowed to make
            if (!cancelChange)
            {
                listItem["History"] = history;
                listItem["Log"] = log;

                SPSecurity.RunWithElevatedPrivileges(delegate {
                    // Planned changes do not require vp signature so we set it
                    // to "Not Required" if the vp has not already approved
                    if (changeType.Equals("Planned") && !vp.HasApproved())
                        listItem[vp.ApprovalField] = "Not Required";

                    if (!changeType.Equals("Emergency/Rush"))
                        PlannedOrUnplannedChange();
                    else
                        EmergencyChange();
                    listItem["EmailsSent"] = emailsSent;
                    UpdateChangeControl();
                });
            }

I will keep working with this and post a solution when I find one.
Aug 1, 2012 at 10:13 PM

Well, not surprising but SharePoint is so picky.  I figured it out finally.

 

The variable listItem is the following:

 

SPListItem listItem = _properties.ListItem;

 

Well because I am hooking into the ItemUpdating Event there is also another set of values in:

 

SPItemEventDataCollection afterProperties = _properties.AfterProperties;

 

In the code I provided above when I was setting the "EmailsSent" field I was using listItem which stores the "Before" values.  I changed the code to:

 

// If we get here the user did not make any changes they are not allowed to make
if (!cancelChange)
{
    afterProperties["History"] = history;
    afterProperties["Log"] = String.Empty;

    // Planned changes do not require vp signature so we set it
    // to "Not Required" if the vp has not already approved
    if (changeType.Equals("Planned") && !vp.HasApproved())
        afterProperties[vp.ApprovalField] = listItem.Fields[vp.ApprovalField].GetFieldValue("Not Required");

    SPSecurity.RunWithElevatedPrivileges(delegate {
        if (!changeType.Equals("Emergency/Rush"))
            PlannedOrUnplannedChange();
        else
            EmergencyChange();
        afterProperties["EmailsSent"] = emailsSent;
        UpdateChangeControl();
    });
}

 

And what do you know... it works fine.  Really made my day :)