I attach a file to the list item when it is updating and when new item added. Additional condition when item updating - if the file already exists, remove it and replace the new one.
I receive events ItemAdded
and ItemUpdating
. In ItemAdded
all is good, but ItemUpdating
returns error this message:
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. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: Microsoft.SharePoint.SPException: 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. Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace: [SPException: 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.] Microsoft.SharePoint.WebControls.SaveButton.SaveItem(SPContext itemContext, Boolean uploadMode, String checkInComment) +20032623 Microsoft.SharePoint.WebControls.SaveButton.OnBubbleEvent(Object source, EventArgs e) +1167 System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +70 System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +29 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2981
My code:
// Generate PDF
string pdfPath = GeneratePDF(properties);
FileInfo file = new FileInfo(@"" + pdfPath);
SPListItem attachItem = properties.ListItem;
SPAttachmentCollection attchList = attachItem.Attachments;
int countElem = attchList.Count;
string Resume = "tempdoc.pdf";
bool fileExist = false;
if (countElem > 0)
{
for (int i = 0; i < countElem; i++)
{
string currAttch = attchList[i];
if (currAttch == Resume)
{
attchList.Delete(file.Name);
fileExist = true;
break;
}
}
}
if (!fileExist)
{
// attach resume
attchList.Add(file.Name, addFile(file));
}
this.EventFiringEnabled = false;
attachItem.Web.AllowUnsafeUpdates = true;
attachItem.Update();
//attachItem.SystemUpdate(false);
attachItem.Web.AllowUnsafeUpdates = false;
this.EventFiringEnabled = true;