1

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?

2 Answers 2

0

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,

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

0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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