locked
Change file name and content on ItemAdding RRS feed

  • Question

  • I need to modify the content of some documents that are added to Sharepoint BEFORE they are actually added.

    I coded a little C# Event receiver but it's not working... I have no way to modify it... I have this for example:

     

    public override void ItemAdding(SPItemEventProperties properties)
    {
    base.ItemAdding(properties);
    SPFile spf = properties.Web.GetFile(properties.AfterUrl);
    SPSecurity.RunWithElevatedPrivileges(delegate() {
      if (spf != null) {
         string szUrl = properties.AfterUrl;
         string szNewUrl;
         szNewUrl = szUrl;
         ......
         DisableEventFiring();
         //spf.MoveTo(szNewUrl);
         //spf.Name.Replace(szUrl, szNewUrl);
         FileStream fileStream = new FileStream("c:\\newcontent.dat", FileMode.Open, FileAccess.Read);
        //spf.CheckOut();
         spf.SaveBinary(fileStream);
         spf.Update();
         fileStream.Close();
         EnableEventFiring();
       }
     });
    }

     

    But it's not working... only if I code on ItemAdded it works but unfortunately it's too late since the user adds a document and when the web page comes back the file name is still the old file name.

    Is there another approach of doing it? Maybe an IIS API or something like that?

    Thanks

    Tuesday, May 22, 2012 2:59 PM

Answers

  • Hello,

    I've you tried to use the declare the itemadded event as synchronous?

    you can do this like this

    <Receiver>
    <Name>EventReceiver1ItemAdded</Name>
    <Type>ItemAdded</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
    <Class>TutoTaxonomy.EventReceiver1.EventReceiver1</Class>
    <SequenceNumber>10000</SequenceNumber>
    <Synchronization>Synchronous</Synchronization>
    </Receiver>

    Maybe this could help you


    Best regards, Christopher.
    Blog | Mail

    • Marked as answer by Qiao Wei Wednesday, May 23, 2012 6:46 AM
    Tuesday, May 22, 2012 3:06 PM
    Answerer

All replies

  • Hello,

    I've you tried to use the declare the itemadded event as synchronous?

    you can do this like this

    <Receiver>
    <Name>EventReceiver1ItemAdded</Name>
    <Type>ItemAdded</Type>
    <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>
    <Class>TutoTaxonomy.EventReceiver1.EventReceiver1</Class>
    <SequenceNumber>10000</SequenceNumber>
    <Synchronization>Synchronous</Synchronization>
    </Receiver>

    Maybe this could help you


    Best regards, Christopher.
    Blog | Mail

    • Marked as answer by Qiao Wei Wednesday, May 23, 2012 6:46 AM
    Tuesday, May 22, 2012 3:06 PM
    Answerer
  • thanks it worked!!!
    Tuesday, May 22, 2012 6:37 PM
  • Hi

    There's one thing you are missing here , you have to recreate the objects inside the RunWithElevatedPrivileges if you want them to execute with elevated privileges. At least that's true for SPWeb and SPSite, and I'm pretty sure you have to do that for the SPFile object too. So in your case you should move the "SPFile spf=properties..." line inside the delegate. If not there's no point in using elevated priviliges here.  See sample with SPWeb here.  And also, you can safely remove the base.ItemAdding() call.


    Regards Bjoern
    Blog



    Tuesday, May 22, 2012 6:46 PM