Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

In an ItemDeleting event receiver, is there a way to find out whether SPListItem.Delete or SPListItem.Recycle was called?

I would like to write different things to my log here...

share|improve this question

2 Answers 2

I thought the answer was to check if the document was in the recycle bin:

private bool DocumentWasRecycled(SPItemEventProperties properties)
{
    Guid itemGuid = properties.ListItem.UniqueId;
    SPRecycleBinItem recycledItem = null;
    try
    {
        recycledItem = properties.Web.RecycleBin.GetItemById(itemGuid);
    }
    catch { }
    return recycledItem != null;
}

But the problem is that if I do it in ItemAdding, it hasn't been put in the recycle bin yet, and if I do it in ItemAdded, I can no longer get the ID of the item...

share|improve this answer

This is a possible solution to my question

public override void ItemDeleted(SPItemEventProperties properties)
{
    string[] splittedBeforeUrl = properties.BeforeUrl.Split('/');
    string fileName = splittedBeforeUrl.Last();
    string filePath = properties.BeforeUrl.Replace(fileName, "").TrimEnd('/');
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        foreach (SPRecycleBinItem item in properties.Web.RecycleBin)
        {
            if (item.ItemType == SPRecycleBinItemType.File)
            {
                if (fileName.Contains(item.Title) && item.DirName.Contains(filePath))
                {
                    LogDeleteEvent(filePath, fileName);
                    break;
                }
            }
        }
    });
    base.ItemDeleting(properties);
}

But in my log, I also need some fields from the SPListItem, which is null in the ItemDeleted event. So I guess there is no solution...

share|improve this answer

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.