2

I have a list "Table" this list has associated OOTB Approval Workflow. This list also has two columns "Approval 2010" and "Request Status". When an item is added to the list the workflow starts the process for approving the item and the status of column "Request Status" changes to "Submitted". When the status of column "Approval 2010" change to "Approved" the value of column "Request Status" need to change to "Approved".

I don't receive any error, but the properties.ListItem.Update(); doesn't update the value of column "Request Status".

MyCode: private void ChangeStatus(SPItemEventProperties properties) { try {

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite elevSite = new SPSite(properties.SiteId))
                using (SPWeb elevWeb = elevSite.OpenWeb(properties.RelativeWebUrl))
                {
                    SPList elevList = elevWeb.Lists[properties.ListId];
                    SPListItem elevListItem = elevList.GetItemById(properties.ListItemId);

                    //requestStatusValue = elevListItem["Request Status"].ToString();
                    string value = properties.ListItem["Approval 2010"].ToString();
                    SPListItem item = properties.ListItem;

                    using (DisabledEventsScope scope = new DisabledEventsScope())
                    {
                        if (value == "16")
                        {

                            if (properties.ListItem["Request Status"].ToString() != "Approved")
                            {

                                properties.ListItem["Request Status"] = "Approved";
                                properties.ListItem.Update();
                            }
                        }

                        else if (value == "17")
                        {
                            if (elevListItem["Request Status"].ToString() != "Rejected")
                            {
                                item["Request Status"] = "Rejected";
                                elevListItem.Update();
                            }
                        }
                        else if (value == "15")
                        {
                            if (elevListItem["Request Status"].ToString() != "Canceled")
                            {
                                item["Request Status"] = "Canceled";
                                elevListItem.Update();
                            }
                        }
                    }
                }
            });
        }
        catch (Exception ex)
        {
            Logger.Error(Title, ex.Message, Category, ex);
            properties.ErrorMessage = ex.Message;
            properties.Status = SPEventReceiverStatus.CancelWithError;
            properties.Cancel = true;
        }
        finally
        {
            Logger.Info(Title, "Exit StatusChanged", Category);
        }
    }
2
  • what is being passed in the value string variable when this event fires?
    – jpollar
    Jan 13, 2017 at 18:15
  • Also...you should be running Update() on the same ListItem that you modify.
    – jpollar
    Jan 13, 2017 at 18:17

2 Answers 2

2

Try this instead. You don't need to create two different SPListItems.

Let me know if this works or need more help.

SPListItem item = properties.ListItem;
string value = item["Approval 2010"].ToString();

using (DisabledEventsScope scope = new DisabledEventsScope())
{
    if (value == "16")
    {

        if (item["Request Status"].ToString() != "Approved")
        {

            item["Request Status"] = "Approved";
            item.Update();
        }
    }

    else if (value == "17")
    {
        if (item["Request Status"].ToString() != "Rejected")
        {
            item["Request Status"] = "Rejected";
            item.Update();
        }
    }
    else if (value == "15")
    {
        if (item["Request Status"].ToString() != "Canceled")
        {
            item["Request Status"] = "Canceled";
            item.Update();
        }
    }
}
0

You need to fire Update functionality on the listitem which is an object of "properties.ListItem". Remove "elevListItem" object from your code sample and use "item" object which is an object of "properties.ListItem".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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