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 have deployed an event reciever with item added event on a document library in sharepoint site.

In item added event I am calculating the number of pages for the uploaded document and updated a column named "number of pages". This code is working fine.

But when I click save button my Sharepoint site show an exception on a message box

The exception says: The file DocumentLibrary/File.pdf has been modified by SHAREPOINT\system on 06 Jan 2012 16:54:06 +0530.

Can I avoid it somehow??

share|improve this question
1  
See my answer on this question: [Race condition in SPListEventReceiver.ListAdded - Save Conflict][1]. But this works only for SharePoint 2010 [1]: sharepoint.stackexchange.com/questions/9763/… –  Matthias Jan 6 '12 at 12:17
add comment

3 Answers

up vote 1 down vote accepted

The point here is that when the file is uploaded, firstly the ItemAdding event will be raised, and only after that the page where you can change properties and press the Save button will be shown.

So after item is added and page is shown to the end user, your code in ItemAdded will be executed, and it will change the same item which user is trying to edit. Hence, it is not a surprise to get the "Item has been modified" exception.

I'd recommend you to use ItemUpdating event receiver instead of ItemAdded. You should be able retrieve the downloaded file using the following code:

SPFile file = web.GetFile(properties.AfterUrl);

Then, you can set the "Number of pages" column value using AfterProperties collection, if it is not initialized yet (you can check this in BeforeProperties).

So the final code will look something like this:

if (String.IsNullOrEmpty(properties.BeforeProperties[numberOfPagesFieldInternalName]))
{
    SPFile file = web.GetFile(properties.AfterUrl);
    var numberOfPages = /* count the number of pages */;
    properties.AfterProperties[numberOfPagesFieldInternalName] = numberOfPages.ToString();
}
share|improve this answer
    
i got ur point but the problem here will be , if the user who is uploading the file hit cancel than code will not run –  Mohit Jan 13 '12 at 10:17
    
I think it's much better than to get the exception :) Ok, I can propose a number of ways here, but I admit they all are a bit clumsy. Thus probably you can consider using approach from @Matthias. I reckon it is very interesting and definitely worth trying. –  Andrey Markeev Jan 13 '12 at 10:38
add comment

Are you disabling event firing in you receiver? e.g this.EventFiringEnabled = false;

share|improve this answer
    
no nothing like that –  Mohit Jan 13 '12 at 10:16
add comment

I was in same situation. I had to pull the meta data from pdf and fill the column value. After trying few things I came to this blog and I made update event to syncronous instead of async and it fixed the issue.

<Synchronization>Synchronous</Synchronization>

My Elements.xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Receivers>
    <Receiver>
      <Name>PdfMetaDataExtractorEventReceiverItemAdded</Name>
      <Type>ItemAdded</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>PdfMetaDataExtractorEventReceiver</Class>
      <SequenceNumber>1000</SequenceNumber>
    </Receiver>
    <Receiver>
      <Name>PdfMetaDataExtractorEventReceiverItemUpdated</Name>
      <Type>ItemUpdated</Type>
      <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
      <Class>PdfMetaDataExtractorEventReceiver</Class>
      <SequenceNumber>1000</SequenceNumber>
      <Synchronization>Synchronous</Synchronization>
    </Receiver>
  </Receivers>
</Elements>
share|improve this answer
add comment

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.