Applying SharePoint Retention Labels Programmatically

So you’ve created and published Retention Labels in Office 365 and now you want to apply labels by CSOM code. It’s not very well documented. The following (crude) code should help you get started applying labels to both individual items and to set as default for the whole library:

First some boilerplate initialization…

Get all labels available on the site:

Get the default label for a list:

Get labels for all items in a list:

Write label to a list item:

Set the default label for a list:

SetComplianceTag and SetListComplianceTag have parameters (isTagPolicyHold, isTagPolicyRecord and isEventBasedTag) that are not documented what they actually do. Above I have set them to false. If you know what they do, please leave a comment!

This entry was posted in Development and tagged , , , , , , , , , . Bookmark the permalink.

4 Responses to Applying SharePoint Retention Labels Programmatically

  1. Evan Econo says:

    hi Dan,

    yes its frustrating, Microsoft’s documentation is severely lacking for those paramaters (isTagPolicyHold, isTagPolicyRecord etc…) I think they relate to if the item will be treated as an uneditable/undeletable “record” or not.

    When I tested it I set those all to false.

    question: so the problem I have is I use the REST API’ setComplianceTag() to clear a list item’s retention label (setting its value to “”), hoping that after/by 7 days hence it the auto-apply rules kick in (based on a keyword KQL query defined in the auto-apply policy)

    but it seems like the auto apply doesn’t kick in in this scenario (I’ve waited more than 7 days). I would think clearing the retention label would negate any explicit precedence that the previous label would have had..that’s the only reason I could see it not auto applying the new label.

    do you think if I used setCompliaceTagWithMeta() instead or if I set any of those parameters (isPolicyHold etc…) to true it would be any different?

    any insight would be so greatly appreciated as I can’t afford to wait another 7 days to test it again

  2. Andrey says:

    Hey. Interesting article. Thank. But I still do not understand if there is a tool how to create a new label and distribute it for certain sites or places?

    • Dan says:

      You manage and distribute retention labels and policies in Office 365 Admin Center. It is quite difficult to grasp what to use and when though, so first read up on the different options. Microsoft’s documentation is a good start. I can also recommend Joanne C. Klein’s blog. She has written many good articles on this topic: https://joannecklein.com

Leave a Reply to Dan Cancel reply

Your email address will not be published. Required fields are marked *