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 an ItemUpdated event receiver in a document library in SharePoint 2010.

When I try to upload a document with a name that already exists in my library I get an error message "The specified name is already in use". That is fine.

But the ItemUpdated event is fired anyway and I don't want my code to run if no document was added. Does anyone know how to work around this?

share|improve this question
add comment

2 Answers 2

up vote 0 down vote accepted

Yup, it is possible, however you will have to make changes to your event receiver,

public override void ItemAdding(SPItemEventProperties properties)
{
  if (CheckIfDocumentAlreadyExists(properties.Item.File.Name))
  {
    properties.Cancel = true;
    properties.ErrorMessage = "Some error has occured....";
  }
}

and then create a method named as CheckIFDocumentAlreadyExits and put code where it will check if file exists or not,

http://stackoverflow.com/questions/359672/how-to-determine-if-a-file-exists-in-a-sharepoint-spfolder

share|improve this answer
add comment

My method:

private bool CheckIfDocumentAlreadyExists(SPItemEventProperties properties)
{
    bool documentExists = false;
    if (!string.IsNullOrEmpty(properties.AfterUrl))
    {
        var existingDocument = properties.Web.GetFile(properties.AfterUrl);
        documentExists = existingDocument.Exists;
    }
    return documentExists;
}
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.