1

Can I define an "ItemAdding" and an "ItemAdded" in the same event receiver?

I'm developing a event receiver that will verify if the file name is in the correct format when it's uploaded.

So I am using a Item receiver and its ItemAdded to execute some task after the file is updated.

But, before it is updated I need to verify if the file name is in a correct format.

How can I do that?

2

You can declare both event receivers in the same Elements.xml file and then implement both ItemAdded and ItemAdding in the related .cs file. No problem at all.

If you create the EventReceiver in VS 2012 or 2013 with the step-by-step guide you can select to create both in the wizard :)

Example XML:

<?xml version="1.0" encoding="utf-8"?> 
<Elements xmlns="schemas.microsoft.com/sharepoint/">
    <Receivers ListUrl="Bibliothque CSV In" >
        <Receiver>
            <Name>fileUploadedItemAdded</Name>
            <Type>ItemAdded</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>            
            <Class>AutomatisationDesActesRemises.Itens.EventReceiver.fileUploaded.fileUpload‌​ed</Class>
            <SequenceNumber>10000</SequenceNumber>
        </Receiver>
        <Receiver>
            <Name>fileUploadedItemAdding</Name>
            <Type>ItemAdding</Type>
            <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly>            
            <Class>AutomatisationDesActesRemises.Itens.EventReceiver.fileUploaded.fileUpload‌​ed</Class>
            <SequenceNumber>10000</SequenceNumber>
        </Receiver>
    </Receivers>
</Elements>

and then in your .cs file (AutomatisationDesActesRemises.Itens.EventReceiver.fileUploaded.fileUpload‌​ed)

overwrite the ItemAdding method:

public override void ItemAdding(SPItemEventProperties properties)
{
    // Do your name check here
}
  • Im using VS2010 Roberto mu Element.XML at this moment is like that: <?xml version="1.0" encoding="utf-8"?> <Elements xmlns="schemas.microsoft.com/sharepoint"> <Receivers ListUrl="Bibliothque CSV In" > <Receiver> <Name>fileUploadedItemAdded</Name> <Type>ItemAdded</Type> <Assembly>$SharePoint.Project.AssemblyFullName$</Assembly> <Class>AutomatisationDesActesRemises.Itens.EventReceiver.fileUploaded.fileUploaded</Class> <SequenceNumber>10000</SequenceNumber> </Receiver> </Receivers> </Elements> – JulianoG Jan 3 '14 at 15:19
  • Updated with (I think) the correct XML for you. My dev environment is shut of for the holidays so this is not guaranteed to be 100% correct :) – Robert Lindgren Jan 3 '14 at 15:25
  • No problem, glad to help :) Now you have the fun job of writing the content of the method :) – Robert Lindgren Jan 3 '14 at 15:31
  • Just one question... If the file name is incorrect, I want to cancel the file upload... how can I do that? – JulianoG Jan 3 '14 at 15:32
  • You can use the two lines of codes in this question: sharepoint.stackexchange.com/questions/3528/… They work in an ItemAdding event receiver – Robert Lindgren Jan 3 '14 at 15:36
0

You can add both ItemAdding and ItemAdded but it may be a problem to achieve what you want because you want to check document's name in ItemAdding event.

SPEventReceiverProperties.AfterProperties don't behave in the same way with document libraries as they do with standard lists. This is how it used to be but you can check again, maybe something has changed with new versions and cumulative updates.

Here's a nice post about what you can get in event receiver's properties.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.