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've been trying to add a watermark to files as they are uploaded to SharePoint. I tried using several events: ItemAdding, ItemUpdating, ItemCheckedIn.

In ItemUpdating and ItemCheckedIn the file is already in the DB. so I can add a watermark - but it's just a waste of DB read/write (The whole file is written, then I read it, then I re-save it to the db).

the ItemAdding event looks good, it does rise before the content is saved to the DB, but I do not have the content itself (SPItemEventProperties doesn't have any "content" attribute or something similar)

share|improve this question

3 Answers 3

up vote 5 down vote accepted

You can access the content from the ItemAdding event by using Request.Files property of the context. Try something like this :

HttpFileCollection files = HttpContext.Current.Request.Files;  
    foreach (String key in files.Keys)   
    {  
        if (collection[key].ContentLength > 0)   
        {  
           Stream stream = files[key].InputStream;  
           string filePath = files[key].FileName;  
        }  
    } 

For more info, read this post.

share|improve this answer

Have you tried the ItemAdded event instead? This fires after the item has finished being added to the library.

share|improve this answer
    
The real questions is - where do I get the content from in the ItemAdded event. Let's say I'm uploading a file to a document library, in the ItemAdded event, where can I see its content to add the water mark? –  Roy Reznik Jul 26 '12 at 12:49
    
You should be able to get everything you need from properties.ListItem.File (msdn.microsoft.com/en-us/library/…) –  Dave Wise Jul 26 '12 at 13:31
    
Checked it out - it's just like ItemCheckingIn and ItemUpdating - the file is already in the DB at this point, which means I'm doing extra read & rewrite for every file. that's a huge performance hit. –  Roy Reznik Jul 26 '12 at 13:59

You can achieve this task during ItemAdded Event. You juts need to set

"<Synchronization>Synchronous</Synchronization>"

in Element.xml file of Event receiver.

<Receivers  ListUrl="Lists\Doc">      
      <Receiver>
        <Name>List_EventItemAdded</Name>
        <Type>ItemAdded</Type>
        <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
        <Class>DocumentLib_EventReceiver.List_Event.List_Event</Class>
        <SequenceNumber>10000</SequenceNumber>
        **<Synchronization>Synchronous</Synchronization>**
      </Receiver>
  </Receivers>
share|improve this answer
    
Could you elaborate a little more on how to access the content of the file during this event, just to have a complete answer? Thanks. –  Norbert Aug 23 '13 at 7:01
    
Basically, After File Upload in SharePoint Document Library, you find hard to trap the ItemAdded Event. Then you need to use <Synchronization>Synchronous</Synchronization> trapping the file upload. At this time, The file is already uploaded in document library but you can set properties, or retrieve file. –  Ajeet Kumar Singh Aug 23 '13 at 7:31

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.