0

I have one sharepoint 2010 application, in which i have to add the item to the list followed by 2 more updates within the same list item. These are the items that I have to do while saving.

  1. I need to save the item into my parent list.
  2. I have registered one event handler (Item Added event) which will trigger after step 1, and it will creates a new item into the task list.
  3. After the first step, again i am updating the same list item column for saving an URL column with the Item ID
  4. After the 2nd step. Ie Task Item added by the event handler, I need to update the Parent list column with the task list item ID

While doing these steps. I am getting Save Conflict error.

I tried to use the Reload the SpListItem and Thread.Sleep method too. But everything has failed.

Please help me to resolve this issue. Thanks in advance. For the reference I am adding my code below.

     private void SaveItemsToList()
    {
        try
        {

            SPUser spCurrentUser = SPContext.Current.Web.CurrentUser;
            if (fuReviewer.PostedFile.ContentLength > 10485760)
            {
                lblMessage.Text = "<script type='text/javascript' language='javascript'>alert('Attached file size should be less than 10 Mb.')</script>";
                return;
            }
            SPUtility.ValidateFormDigest();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
                {
                    using (SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID))
                    {
                        oSite.CatchAccessDeniedException = false;
                        //Microsoft.SharePoint.Administration.SPWebApplication webApp = oWeb.Site.WebApplication;
                        //webApp.FormDigestSettings.Enabled = false;
                        oWeb.AllowUnsafeUpdates = true;
                        oSite.AllowUnsafeUpdates = true;
                        SPList changeRequestList = oWeb.Lists[Constants.ListNames.CHANGE_REQUEST_FORM];

                        SPListItemCollection listItems = changeRequestList.Items;

                        SPListItem item = listItems.Add();
                        item[Constants.FieldNames.APPLICATION_NAME] = ddlApplicationName.SelectedItem.Text;
                        item[Constants.FieldNames.BUSINESS_AREA] = ddlBusinessArea.SelectedItem.Text;
                        item[Constants.FieldNames.PROJECT_CODE] = new SPFieldLookupValue(Int32.Parse(ddlProjectCode.SelectedItem.Value), ddlProjectCode.SelectedItem.Text);

                        item[Constants.FieldNames.CLIENT_NAME] = txtClientName.Text;

                        item[Constants.FieldNames.LOCATION] = ddlLocation.SelectedItem.Text.ToString();
                        if (!string.IsNullOrEmpty(txtDate.Text))
                        {
                            DateTime dtDate = DateTime.Now;
                            DateTime.TryParseExact(txtDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtDate);
                            item[Constants.FieldNames.DATE] = dtDate;
                        }
                        string selectedVal = string.Empty;
                        bool isOthersSelected = false;
                        if (ddlChangeCategory.SelectedItem.Value.Equals("Others"))
                            isOthersSelected = true;
                        if (isOthersSelected)
                            item[Constants.FieldNames.CHANGE_CATEGORY] = ddlChangeCategory.SelectedItem.Value + " - " + Convert.ToString(txtChangeCategory.Text);
                        else
                            item[Constants.FieldNames.CHANGE_CATEGORY] = ddlChangeCategory.SelectedItem.Value.Equals("Select") ? string.Empty : ddlChangeCategory.SelectedItem.Value;

                        item[Constants.FieldNames.CHANGE_REQUIREMENT_DESCRIPTION] = txtChangeRqmnt.Text.ToString();

                        item[Constants.FieldNames.CHANGE_CRITICALITY] = (rbtnCriticality.SelectedItem != null) ? rbtnCriticality.SelectedItem.Value : "";

                        item[Constants.FieldNames.CRITICALITY_JUSTIFICATION] = txtCriticality.Text.ToString();

                        item[Constants.FieldNames.JUSTIFICATION_ROI_COMMERCIAL_VALUE_TO_BUSINESS] = txtJustification.Text.ToString();


                        selectedVal = string.Empty;
                        isOthersSelected = false;
                        if (ddlClientRequirement.SelectedItem.Value.Equals("Others"))
                            item[Constants.FieldNames.FOR_CLIENT_REQUIREMENT] = ddlClientRequirement.SelectedItem.Value + " - " + Convert.ToString(txtChangeCategory.Text);
                        else
                            item[Constants.FieldNames.FOR_CLIENT_REQUIREMENT] = ddlClientRequirement.SelectedItem.Value.Equals("Select") ? string.Empty : ddlClientRequirement.SelectedItem.Value;

                        if (rbtnLstSecurityRqrd.SelectedItem != null)
                        {
                            item[Constants.FieldNames.INFORMATION_SECURITY_REQUIRED] = rbtnLstSecurityRqrd.SelectedItem.Value;
                            if (rbtnLstSecurityRqrd.SelectedItem.Value.Equals("1"))
                                item[Constants.FieldNames.INFORMATION_SECURITY_REQUIRED_DETAILS] = Convert.ToString(txtSecurityRqrd.Text);
                        }

                        item[Constants.FieldNames.IMPACT_OF_REJECTION_OF_THIS_REQUEST] = txtImpact.Text.ToString();

                        item[Constants.FieldNames.OTHER_CR_REF] = Convert.ToString(txtCRReference.Text);
                        item[Constants.FieldNames.REQUESTED_DATE] = DateTime.Now;
                        SPUser user = GetUserID(oWeb, pe_RequestedBy);
                        if (user != null)
                            item[Constants.FieldNames.REQUESTED_BY] = user.ID;

                        SPUser BusinessOwner = GetUserID(oWeb, pe_BusinessOwner);
                        if (BusinessOwner != null)
                            item[Constants.FieldNames.BUSINESS_OWNER] = BusinessOwner.ID;

                        item[Constants.FieldNames.APPLICATION_CODE] = ddlApplicationName.SelectedItem.Value;
                        item[Constants.FieldNames.BA_CODE] = ddlBusinessArea.SelectedItem.Value;
                        item[Constants.FieldNames.AUTHOR] = spCurrentUser;
                        item[Constants.FieldNames.REQUIREMENT_REVIEWER] = spCurrentUser;
                        item[Constants.FieldNames.REQUIREMENT_REVIEWED_TIME] = DateTime.Now;

                        item.SystemUpdate(); // **First Update**
                        SPListItem reListItem = Helper.ReloadListItem(item);
                        if (reListItem != null)
                        {
                            string formTitle = ddlApplicationName.SelectedItem.Value + "-" + ddlBusinessArea.SelectedItem.Value + "-" + DateTime.Now.Year.ToString() + "-" + item.ID.ToString().PadLeft(5, '0');
                            reListItem[Constants.FieldNames.TITLE] = formTitle;

                            SPFieldUrlValue hyper = new SPFieldUrlValue();
                            hyper.Description = "In Progress";
                            hyper.Url = SPContext.Current.Web.Url + "/_layouts/Audit/TaskHistory.aspx?ID=" + item.ID;
                            reListItem[Constants.FieldNames.FORM_STATUS] = hyper;
                            reListItem[Constants.FieldNames.STATUS] = hyper.Description;

                            hyper = new SPFieldUrlValue();
                            hyper.Description = formTitle;
                            hyper.Url = SPContext.Current.Web.Url + "/_layouts/Audit/ViewForm.aspx?ID=" + item.ID;
                            reListItem[Constants.FieldNames.CR_TITLE] = hyper;



                            SPFieldUserValue userValue = Helper.GetNextLevel(oWeb, "FSL", Convert.ToString(reListItem[Constants.FieldNames.APPLICATION_NAME]), Convert.ToString(reListItem[Constants.FieldNames.BUSINESS_AREA]));

                            SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
                            SPFieldUserValue usertoadd = null;
                            usercollection = (SPFieldUserValueCollection)reListItem[Constants.FieldNames.TAGGED_USERS];
                            if (userValue.User != null)
                                usertoadd = ConvertLoginName(oWeb, userValue.User.LoginName);
                            else
                                usertoadd = userValue;
                            if (usercollection != null)
                            {
                                if (!usercollection.Contains(usertoadd))
                                    usercollection.Add(usertoadd);
                            }
                            else
                            {
                                usercollection = new SPFieldUserValueCollection();
                                usercollection.Add(usertoadd);
                            }

                            reListItem[Constants.FieldNames.TAGGED_USERS] = usercollection;
                            usertoadd = ConvertLoginName(oWeb, spCurrentUser.LoginName);
                            usercollection.Add(usertoadd);
                            reListItem[Constants.FieldNames.REQ_TAGGED_USERS] = usercollection;
                            reListItem.SystemUpdate();//**Second Time Update**
                            if (fuReviewer.HasFile)
                                SaveAttachment(oWeb, reListItem);
                            // webApp.FormDigestSettings.Enabled = true;
                            oSite.AllowUnsafeUpdates = false;
                            oWeb.AllowUnsafeUpdates = false;
                            Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "sc", "<script type='text/javascript'>window.frameElement.commitPopup();</script>");
                        }
                    }
                }
            });
        }
        catch (Exception ex)
        {

            throw ex;
        }

    }
  • What is the exact error you are getting? – Aanchal Jun 26 '14 at 10:12
  • @Aanchal: This is what I am getting... ----------------------------------------------- Save Conflict Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes. – MAC Jun 26 '14 at 10:14
  • can you show your code? looks like your partialy updating somewhere and trying to update again but there would be a confilct! are you updating the list item and the spweb object on item added event? – Ali Jafer Jun 26 '14 at 10:18
  • @aliSharepoint: I have added the code to question part – MAC Jun 26 '14 at 10:38
  • How do you reload the SPListItem? Please add the code for "Helper.ReloadListItem(item)" as well. – Paiman Samadian Jun 27 '14 at 0:49
1

had a similar issue before when list items were updating themselft from the itemupdating event occuring.

lol confusing but a simple solution.

You need to tell sharepoint to disable any event firing (update event) when your doing the code so it wont loop back on itself:

EventFiringEnabled = false;   

do your code (update list and web)

EventFiringEnabled = true;

so for you:

private void SaveItemsToList()
{
    try
    {

        SPUser spCurrentUser = SPContext.Current.Web.CurrentUser;
        if (fuReviewer.PostedFile.ContentLength > 10485760)
        {
            lblMessage.Text = "<script type='text/javascript' language='javascript'>alert('Attached file size should be less than 10 Mb.')</script>";
            return;
        }
        SPUtility.ValidateFormDigest();
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            using (SPSite oSite = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb oWeb = oSite.OpenWeb(SPContext.Current.Web.ID))
                {
                    EventFiringEnabled = false;   

                    oSite.CatchAccessDeniedException = false;
                    //Microsoft.SharePoint.Administration.SPWebApplication webApp = oWeb.Site.WebApplication;
                    //webApp.FormDigestSettings.Enabled = false;
                    oWeb.AllowUnsafeUpdates = true;
                    oSite.AllowUnsafeUpdates = true;
                    SPList changeRequestList = oWeb.Lists[Constants.ListNames.CHANGE_REQUEST_FORM];

                    SPListItemCollection listItems = changeRequestList.Items;

                    SPListItem item = listItems.Add();
                    item[Constants.FieldNames.APPLICATION_NAME] = ddlApplicationName.SelectedItem.Text;
                    item[Constants.FieldNames.BUSINESS_AREA] = ddlBusinessArea.SelectedItem.Text;
                    item[Constants.FieldNames.PROJECT_CODE] = new SPFieldLookupValue(Int32.Parse(ddlProjectCode.SelectedItem.Value), ddlProjectCode.SelectedItem.Text);

                    item[Constants.FieldNames.CLIENT_NAME] = txtClientName.Text;

                    item[Constants.FieldNames.LOCATION] = ddlLocation.SelectedItem.Text.ToString();
                    if (!string.IsNullOrEmpty(txtDate.Text))
                    {
                        DateTime dtDate = DateTime.Now;
                        DateTime.TryParseExact(txtDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtDate);
                        item[Constants.FieldNames.DATE] = dtDate;
                    }
                    string selectedVal = string.Empty;
                    bool isOthersSelected = false;
                    if (ddlChangeCategory.SelectedItem.Value.Equals("Others"))
                        isOthersSelected = true;
                    if (isOthersSelected)
                        item[Constants.FieldNames.CHANGE_CATEGORY] = ddlChangeCategory.SelectedItem.Value + " - " + Convert.ToString(txtChangeCategory.Text);
                    else
                        item[Constants.FieldNames.CHANGE_CATEGORY] = ddlChangeCategory.SelectedItem.Value.Equals("Select") ? string.Empty : ddlChangeCategory.SelectedItem.Value;

                    item[Constants.FieldNames.CHANGE_REQUIREMENT_DESCRIPTION] = txtChangeRqmnt.Text.ToString();

                    item[Constants.FieldNames.CHANGE_CRITICALITY] = (rbtnCriticality.SelectedItem != null) ? rbtnCriticality.SelectedItem.Value : "";

                    item[Constants.FieldNames.CRITICALITY_JUSTIFICATION] = txtCriticality.Text.ToString();

                    item[Constants.FieldNames.JUSTIFICATION_ROI_COMMERCIAL_VALUE_TO_BUSINESS] = txtJustification.Text.ToString();


                    selectedVal = string.Empty;
                    isOthersSelected = false;
                    if (ddlClientRequirement.SelectedItem.Value.Equals("Others"))
                        item[Constants.FieldNames.FOR_CLIENT_REQUIREMENT] = ddlClientRequirement.SelectedItem.Value + " - " + Convert.ToString(txtChangeCategory.Text);
                    else
                        item[Constants.FieldNames.FOR_CLIENT_REQUIREMENT] = ddlClientRequirement.SelectedItem.Value.Equals("Select") ? string.Empty : ddlClientRequirement.SelectedItem.Value;

                    if (rbtnLstSecurityRqrd.SelectedItem != null)
                    {
                        item[Constants.FieldNames.INFORMATION_SECURITY_REQUIRED] = rbtnLstSecurityRqrd.SelectedItem.Value;
                        if (rbtnLstSecurityRqrd.SelectedItem.Value.Equals("1"))
                            item[Constants.FieldNames.INFORMATION_SECURITY_REQUIRED_DETAILS] = Convert.ToString(txtSecurityRqrd.Text);
                    }

                    item[Constants.FieldNames.IMPACT_OF_REJECTION_OF_THIS_REQUEST] = txtImpact.Text.ToString();

                    item[Constants.FieldNames.OTHER_CR_REF] = Convert.ToString(txtCRReference.Text);
                    item[Constants.FieldNames.REQUESTED_DATE] = DateTime.Now;
                    SPUser user = GetUserID(oWeb, pe_RequestedBy);
                    if (user != null)
                        item[Constants.FieldNames.REQUESTED_BY] = user.ID;

                    SPUser BusinessOwner = GetUserID(oWeb, pe_BusinessOwner);
                    if (BusinessOwner != null)
                        item[Constants.FieldNames.BUSINESS_OWNER] = BusinessOwner.ID;

                    item[Constants.FieldNames.APPLICATION_CODE] = ddlApplicationName.SelectedItem.Value;
                    item[Constants.FieldNames.BA_CODE] = ddlBusinessArea.SelectedItem.Value;
                    item[Constants.FieldNames.AUTHOR] = spCurrentUser;
                    item[Constants.FieldNames.REQUIREMENT_REVIEWER] = spCurrentUser;
                    item[Constants.FieldNames.REQUIREMENT_REVIEWED_TIME] = DateTime.Now;

                    item.SystemUpdate(); // **First Update**
                    SPListItem reListItem = Helper.ReloadListItem(item);
                    if (reListItem != null)
                    {
                        string formTitle = ddlApplicationName.SelectedItem.Value + "-" + ddlBusinessArea.SelectedItem.Value + "-" + DateTime.Now.Year.ToString() + "-" + item.ID.ToString().PadLeft(5, '0');
                        reListItem[Constants.FieldNames.TITLE] = formTitle;

                        SPFieldUrlValue hyper = new SPFieldUrlValue();
                        hyper.Description = "In Progress";
                        hyper.Url = SPContext.Current.Web.Url + "/_layouts/Audit/TaskHistory.aspx?ID=" + item.ID;
                        reListItem[Constants.FieldNames.FORM_STATUS] = hyper;
                        reListItem[Constants.FieldNames.STATUS] = hyper.Description;

                        hyper = new SPFieldUrlValue();
                        hyper.Description = formTitle;
                        hyper.Url = SPContext.Current.Web.Url + "/_layouts/Audit/ViewForm.aspx?ID=" + item.ID;
                        reListItem[Constants.FieldNames.CR_TITLE] = hyper;



                        SPFieldUserValue userValue = Helper.GetNextLevel(oWeb, "FSL", Convert.ToString(reListItem[Constants.FieldNames.APPLICATION_NAME]), Convert.ToString(reListItem[Constants.FieldNames.BUSINESS_AREA]));

                        SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
                        SPFieldUserValue usertoadd = null;
                        usercollection = (SPFieldUserValueCollection)reListItem[Constants.FieldNames.TAGGED_USERS];
                        if (userValue.User != null)
                            usertoadd = ConvertLoginName(oWeb, userValue.User.LoginName);
                        else
                            usertoadd = userValue;
                        if (usercollection != null)
                        {
                            if (!usercollection.Contains(usertoadd))
                                usercollection.Add(usertoadd);
                        }
                        else
                        {
                            usercollection = new SPFieldUserValueCollection();
                            usercollection.Add(usertoadd);
                        }

                        reListItem[Constants.FieldNames.TAGGED_USERS] = usercollection;
                        usertoadd = ConvertLoginName(oWeb, spCurrentUser.LoginName);
                        usercollection.Add(usertoadd);
                        reListItem[Constants.FieldNames.REQ_TAGGED_USERS] = usercollection;
                        reListItem.SystemUpdate();//**Second Time Update**
                        if (fuReviewer.HasFile)
                            SaveAttachment(oWeb, reListItem);
                        // webApp.FormDigestSettings.Enabled = true;
                        oSite.AllowUnsafeUpdates = false;
                        oWeb.AllowUnsafeUpdates = false;
                        Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "sc", "<script type='text/javascript'>window.frameElement.commitPopup();</script>");

                        EventFiringEnabled = true;

                    }
                }
            }
        });
    }
    catch (Exception ex)
    {

        throw ex;
    }

}

just like to note that you dont need to surround the whole business logical code ;) i just did for simplicity. You only need to surround it on the updating event before the allowunsafeupade and after:

EventFiringEnabled = false;   

oWeb.AllowUnsafeUpdates = True;

listItem.Update();

oWeb.AllowUnsafeUpdates = false;

EventFiringEnabled = true;

EDIT

to use it do:

    private class SPItemEventReceiverHandling : SPItemEventReceiver
    {
        public SPItemEventReceiverHandling() { }

        new public void DisableEventFiring()
        {
            base.DisableEventFiring();
        }

        new public void EnableEventFiring()
        {
            base.EnableEventFiring();
        }
    }

and then to call it within your code create the new object:

    SPItemEventReceiverHandling rh = new SPItemEventReceiverHandling();
    rh.DisableEventFiring();

    //do your stuff

    rh.EnableEventFiring();
  • I have to use the above method in the custom appplication page. not in the event Handlers page. So, Intellisense not showing the keyword EnableEventFiring. anything wrong from side? – MAC Jun 26 '14 at 10:57
  • just update answer – Ali Jafer Jun 26 '14 at 11:07

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.