Inform authors when a document is deleted from SharePoint with Microsoft Flow

In my post about the When a file is deleted trigger in Microsoft Flow I described the trigger that will fire when a document is deleted.

When a file is deleted

The trigger however will be quite useless if you want to do anything as there isn’t much information available within the information returned. One of my followers asked todayon the post mentioned above if it was possible to email the author when someone deletes document.

Deleted File

But no need to panic!

As so often the Send an HTTP  request to SharePoint action will come to the rescue.Get Reycle bin content with HTTP request using the REST API

The /api/web/RecycleBin REST API will give you all the documents that were deleted.

Then you can add a filter so that only the documents with the same file name are returned. In a similar way you could also include the Time deleted as a filter. But I would like to keep the example in this post simple.Send Email For Deleted File

To get the email address for the author’s email address the following expression will get the job done:

first(body('Send_an_HTTP_request_to_SharePoint')?['d']?['results'])['AuthorEmail']

11 thoughts on “Inform authors when a document is deleted from SharePoint with Microsoft Flow

  1. commented on July 1, 2019 by Johannes

    Thank you very much for your prompt help!
    I got both issues, language issues (that’s why I was not searching the net for “author”) and reading codes: Could you explain the last part of your formula: first(body(‘HTTP-Anforderung_an_SharePoint_senden’)?[‘d’]?[‘results’])[‘AuthorEmail’]
    I do not get the authorEmail, Flow tells me, that the value AuthorEmail can not be selected. I have tried that with “Author”, as this is the column’s name, but this does not work either.

    • commented on July 1, 2019 by Pieter Veenstra

      Hi Johannes,

      Can you email the output from the HTTP request action to contact@veenstra.me.uk? Most likely, there is something not right there.
      In my case I get the following:

      {
      "d": {
      "results": [
      {
      "__metadata": {
      "id": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'582be79c-57a1-4d2c-bdc0-cecdc3e00a71')",
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'582be79c-57a1-4d2c-bdc0-cecdc3e00a71')",
      "type": "SP.RecycleBinItem"
      },
      "Author": {
      "__deferred": {
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'582be79c-57a1-4d2c-bdc0-cecdc3e00a71')/Author"
      }
      },
      "DeletedBy": {
      "__deferred": {
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'582be79c-57a1-4d2c-bdc0-cecdc3e00a71')/DeletedBy"
      }
      },
      "AuthorEmail": "pieter@PieterVeenstraMVP.onmicrosoft.com",
      "AuthorName": "Pieter Veenstra",
      "DeletedByEmail": "pieter@PieterVeenstraMVP.onmicrosoft.com",
      "DeletedByName": "Pieter Veenstra",
      "DeletedDate": "2019-05-08T15:54:35Z",
      "DeletedDateLocalFormatted": "08/05/2019 16:54",
      "DirName": "Shared Documents",
      "DirNamePath": {
      "__metadata": {
      "type": "SP.ResourcePath"
      },
      "DecodedUrl": "Shared Documents"
      },
      "Id": "582be79c-57a1-4d2c-bdc0-cecdc3e00a71",
      "ItemState": 1,
      "ItemType": 1,
      "LeafName": "Document.docx",
      "LeafNamePath": {
      "__metadata": {
      "type": "SP.ResourcePath"
      },
      "DecodedUrl": "Document.docx"
      },
      "Size": "127810",
      "Title": "Document.docx"
      },
      {
      "__metadata": {
      "id": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'325a1cee-99fd-4ff3-9538-c7ad66166c55')",
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'325a1cee-99fd-4ff3-9538-c7ad66166c55')",
      "type": "SP.RecycleBinItem"
      },
      "Author": {
      "__deferred": {
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'325a1cee-99fd-4ff3-9538-c7ad66166c55')/Author"
      }
      },
      "DeletedBy": {
      "__deferred": {
      "uri": "https://pieterveenstramvp.sharepoint.com/_api/Web/RecycleBin(guid'325a1cee-99fd-4ff3-9538-c7ad66166c55')/DeletedBy"
      }
      },
      "AuthorEmail": "testuser@PieterVeenstraMVP.onmicrosoft.com",
      "AuthorName": "Test User",
      "DeletedByEmail": "pieter@PieterVeenstraMVP.onmicrosoft.com",
      "DeletedByName": "Pieter Veenstra",
      "DeletedDate": "2019-07-01T09:41:20Z",
      "DeletedDateLocalFormatted": "01/07/2019 10:41",
      "DirName": "Shared Documents",
      "DirNamePath": {
      "__metadata": {
      "type": "SP.ResourcePath"
      },
      "DecodedUrl": "Shared Documents"
      },
      "Id": "325a1cee-99fd-4ff3-9538-c7ad66166c55",
      "ItemState": 1,
      "ItemType": 1,
      "LeafName": "Document.docx",
      "LeafNamePath": {
      "__metadata": {
      "type": "SP.ResourcePath"
      },
      "DecodedUrl": "Document.docx"
      },
      "Size": "55640",
      "Title": "Document.docx"
      }
      ]
      }
      }

  2. commented on July 24, 2019 by Mish

    I also get the same error…:/
    It seems like AuthorEmail is not an accepted term.

    InvalidTemplate. Unable to process template language expressions in action ‘Send_an_email’ inputs at line ‘1’ and column ‘2642’: ‘The template language expression ‘first(body(‘Send_an_HTTP_request_to_SharePoint’)?[‘d’]?[‘results’])[‘AuthorEmail’]’ cannot be evaluated because property ‘AuthorEmail’ cannot be selected.

    Thoughts?

    • can you add a ? between the ) and the [

      • commented on August 10, 2019 by Mish

        I don’t think that’s the problem.
        Looking in my failed flow, under send the ‘http request’ section, the URI value is coming back empty.
        /_api/web/RecycleBin?$filter=LeafName eq ”
        For some reason the ‘filename with extension’ is NOT producing complete api call.
        /_api/web/RecycleBin?$filter=LeafName eq ‘@{triggerBody()?[‘FileNameWithExtension’]}’

        If it can’t find the file it can’t match an author to send a delete message.

        Thoughts?

  3. commented on October 15, 2019 by Ehab

    Hi Pieter,

    thanks for this, i have tried it but got similar error for Author Email,
    InvalidTemplate. Unable to process template language expressions in action ‘Send_an_email’ inputs at line ‘1’ and column ‘2662’: ‘The template language expression ‘first(body(‘Send_an_HTTP_request_to_SharePoint’)?[‘d’]?[‘results’])[‘AuthorEmail’]’ cannot be evaluated because property ‘AuthorEmail’ cannot be selected. Please see https://aka.ms/logicexpressions for usage details.’.

    I verify when i add ; my email i can receive email notification but the Author Email not getting detected! if you can help

    • Can you create a compose action that is set to

      first(body(‘Send_an_HTTP_request_to_SharePoint’)?[‘d’]?[‘results’])

      What does this return? If nothing is returned, then try the following in a compose:

      body(‘Send_an_HTTP_request_to_SharePoint’)?[‘d’]?[‘results’]

      • commented on October 16, 2019 by Ehab

        can you please guide how to try in Compose? do you mean in new flow to create from blank as i tried but its not opened..

      • Just inside your existing flow you can add a compose. The sole purpose of this compose will be debugging. Most likely the problem that you are hitting is about not getting the data in the shape that you expect it to arrive. This compose action will help us understand the data.

        So simply add an action. Select the compose action and then past part of the full expression. Then in run time you can see what happens.

  4. commented on October 16, 2019 by Muthupandi

    Hi Peter,

    I am trying to update the folder properties whenever file has been deleted.

    My use case:
    i have two folders, subfolder i have one files. whenever i am uploading new files, updating sub folder column(I have status column , update status column as Completed)

    Same way whenever i am trying to delete update status column is In progress.

    I am using same API. but unable to update

    /_api/web/RecycleBin?$filter=LeafName eq ‘@{triggerBody()?[‘FileNameWithExtension’]}’

    • Do I understand it correctly that you are trying to update the folder that contains a documents that was deleted?

      That sounds like you would need the original location of the file that was deleted rather than the recycle bin location. I’m not sure if that is possible.

Leave a Reply to Mish Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: