Pregunta

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??

¿Fue útil?

Solución

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();
}

Otros consejos

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

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>
Licenciado bajo: CC-BY-SA con atribución
scroll top