Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Power Automate - Using Flows
Unanswered

Is there a way to set the Retention Label at document library level with Power Automate?

(0) ShareShare
ReportReport
Posted on by 72

I've spent several hours trying to find a way to use Power Automate to set the default retention label on a SharePoint library (or lits).

 

I've found a way to automate & set on list items, but I want to set default on library/list:

https://www.gravityunion.com/blog/2021/5/automate-retention-in-microsoft-office-365

 

as of 3 years ago, looks like people were asking for this:

https://powerusers.microsoft.com/t5/Power-Automate-Ideas/Get-and-Set-Compliance-Labels-in-SharePoint/idi-p/156112 

 

just thought to ask if this is possible

I know this can be done via CSOM and Pnp, but could not find way to do it using Power Automate.

  • jvdlinden Profile Picture
    329 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    I was looking for a solution too. I have found an article that explains how to perform this action using different methods. Tested it myself with SharePoint REST API, and it works:

    https://www.blimped.nl/managing-and-applying-purview-retention-labels-using-code/#how-to-configure-default-retention-labels-on-folders-and-listslibraries

     

    This is my working request:

    jvdlinden_0-1703687407171.png

    Note that you need to fill in the ABSOLUTE (not relative) URL of the list/document library as shown in the image.

    Hope this helps, for me it did 🙂

  • jherschel7 Profile Picture
    27 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Is there a label already applied?  I think the SyncToItems only works when the label is null/not set.  Also, I noticed some times it takes several minutes for the label to apply, I'm guessing a scheduled job in the background runs?  

  • hspatil Profile Picture
    8 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Hi jherschel7,

     

    I am trying below script for applying labels. Under settings Label is getting applied in drop down but Check box (Apply label to existing items in the library.) is not getting selected. 

     

    Also I used in script -BlockDeletion $true -BlockEdit $true, it is not applying. After below script if I tried to get Label then still it shows False as Output:

     

    The label '30 Years Delete' is set to the specified list or library.
    Block deletion: False
    Block editing: False

     

    Script I used:

     

    $siteURL=Sample Site"

    Connect-PnPOnline -Url $siteURL -UseWebLogin

    Get-PnPLabel -List "InputList" -Verbose

    Set-PnPLabel -List "InputList" -Label "30 Years Delete" -SyncToItems $true -BlockDeletion $true -BlockEdit $true

     

    Kindly let me know do I need to add any commands.

  • jherschel7 Profile Picture
    27 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    PS - you will need to have installed the PnP SharePoint modules

  • jherschel7 Profile Picture
    27 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Run this on the list/library

    Set-PnPLabel -List "Documents" -Label "LabelName" -SyncToItems $true #TRUE sets existing items to label
  • hspatil Profile Picture
    8 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Hi jherschel7,

     

    Thank you very much for sharing the scripts. This is for existing items.

     

    If in list/library new items added then for those items also Retain Policy should be apply.

     

    How to achieve this for new items?

     

    Thanks,

    Harish Patil

  • jherschel7 Profile Picture
    27 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Here is the script I used, please test on a test site/library before running in PROD!

     

    #COMMENTS: PLEASE TEST ON A TEST SITE, RUN AT YOUR OWN RISK!
    $siteURL = "YourSharepointSiteUrl"
    $libName = "Documents"
    $Label = "YourLabelName"
    $count = 0
    
    #Connect to Site
    Connect-PnPOnline -Url $siteURL -Interactive
    
    #Get All List Items matching given query
    $ListItems = Get-PnPListItem -List $libName -PageSize 500 | Where {$_.FileSystemObjectType -eq "File"}
    
    #Loop through each Item
    foreach($ListItem in $ListItems)
    {
    	#UNCOMMENT OUT BELOW AFTER TESTING
    	#Set-PnPListItem -List $libName -Identity $ListItem.ID -Label $Label
    	$count++
    }
    
    Write-Host "files updated:" $count
  • hspatil31 Profile Picture
    8 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Hi Jon.

     

    Kindly let me know what is the script you use? I have the same requirement to apply labels on all list and libraries. Also on all items.

     

    I tried with PowerShell tag Get-PnPComplianceTag but this is not able to find. Getting below errors. Kindly help me how to resolve this issue.

     

    Get-PnPComplianceTag : The term 'Get-PnPComplianceTag' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path
    was included, verify that the path is correct and try again.

  • jherschel7 Profile Picture
    27 on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Thanks, I ended up writing a PowerShell Script to update on all files in a library where it wasn't already set.

     

    Jon

  • Community Power Platform Member Profile Picture
    on at
    Re: Is there a way to set the Retention Label at document library level with Power Automate?

    Create a folder. Then, put a retention label on it and copy every file into it.  
    The retention label will be shared with every file inside it. The problems is that you can't manage retention label for a special file. Every file of the folder will have the same retention label. 

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Markus Franz – Community Spotlight

We are honored to recognize Markus Franz as our April 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,522 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 65,890 Most Valuable Professional

Leaderboard

Featured topics

Restore a deleted flow