I have a standard document library. I want to prompt the user if file name already exists in document library. I want this Friendly Error (with custom error text) on "Upload Document" form when when user clicks 'OK' button. I don't want to show default SharePoint Error page.
1 Answer
You can write a List Item Receiver (Synchronous).
More specific "Item Adding" event receiver, where you can get the current file name & check it against the existing items & show a message.
UPDATE:
public override void ItemAdding(SPItemEventProperties properties)
{
base.ItemAdding(properties);
if (CheckIfExisting()) //this is where you call yout private method to check file names
{
try
{
properties.Cancel = true;
properties.ErrorMessage = "Please choose some other name.";
}
catch (InvalidCreatorIdException ex)
{
properties.Cancel = true;
properties.ErrorMessage = ex.ToString();
properties.InvalidateListItem();
throw;
}
}
}
-
I can identify duplicate in event receiver but I need to show message in UI. Is it possible that Event throws an message to UI.– RaheelApr 26, 2013 at 0:45
-
Use below code: public override void ItemAdding(SPItemEventProperties properties) { base.ItemAdding(properties); if (CheckIfExisting()) //this is custom method { try { properties.Cancel = true; properties.ErrorMessage = "Please choose some other name."; } catch (InvalidCreatorIdException ex) { properties.Cancel = true; properties.ErrorMessage = ex.ToString(); properties.InvalidateListItem(); throw; } } } May 15, 2013 at 8:26