In this blog, you will see how to add a retention label and retention start date to SharePoint documents using CSOM.
I have created retentions labels for SharePoint and I want to apply the label to a specific document using CSOM. Also, I have a date time field named Closed Date and I want this date to be the retention label applied date. As we were doing migration, these dates were past dates and I need to apply these dates to the document.
We will see how to achieve this using CSOM in Console Application.
Code Snippet
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.SharePoint.Client;
- using System.Net;
- using System.Security;
-
- namespace ConsoleApp
- {
- class Program
- {
- static void Main(string[] args)
- {
-
- string siteURL = "https://m3652044.sharepoint.com/sites/VJDemo";
- string password = "84587@1234";
- string emailAddress = "[email protected]";
- string docLibName = "Documents";
- string labelName = "Confidential";
- int itemID = 2;
-
-
- ClientContext ctx = new ClientContext(siteURL);
- SecureString securePwd = new SecureString();
- foreach (var cc in password)
- {
- securePwd.AppendChar(cc);
- }
- ctx.Credentials = new SharePointOnlineCredentials(emailAddress, securePwd);
-
-
- ListItem item = ctx.Web.Lists.GetByTitle(docLibName).GetItemById(itemID);
- ctx.Load(item);
- ctx.ExecuteQuery();
-
-
-
- DateTime closedDateTime = Convert.ToDateTime(item.FieldValues["ClosedDate"]);
- item.SetComplianceTagWithMetaInfo(labelName, false, false, closedDateTime, emailAddress, false);
- item.Update();
- ctx.ExecuteQuery();
-
-
- Console.WriteLine(item.FieldValues["_ComplianceTag"]);
- Console.ReadLine();
- }
- }
- }
Result
Summary
In this blog, you saw how to add a retention label and retention start date to SharePoint documents using CSOM.