共用方式為


IRM and the Object Model

A couple of weeks earlier, I saw an interesting issue. It was a normal day as far as days go in support and in comes this issue, where the person in question wants to investigate or rather should I say automate the process of creating IRM settings on document libraries. I wasn't working on this issue and a colleague took this over. But then somewhere it was lurking, how the hell do you do that? The natural starting point is getting a site/web context and going down to the list and then getting dirty with the properties exposed by the particular SPList object. So after finishing what I had on my plate, I dug into figuring out how exactly to set the IRM settings for a list using the object model. So here goes:

Step 1 Enable IRM on the farm

SPWebService svc = SPFarm.Local.Services.GetValue<SPWebService>();
SPIrmSettings irmSettings = svc.IrmSettings;
irmSettings.IrmRMSEnabled = true;
//set true or false based on the situation
irmSettings.IrmRMSUseAD = true;
irmSettings.IrmRMSCertServer = "certificate server here";
irmSettings.IrmChanges = irmSettings.IrmChanges + 1;
svc.Update();

Step2 Set the IRM properties for a document library

SPSite site = new SPSite("https:////");
SPList spList = site.OpenWeb().Lists["list_name"];
SPFolder folder = spList.RootFolder;
spList.IrmEnabled = true; //corresponds to "Restrict permission to documents in this library on download"
// BELOW SET=2, RESET=0
folder.Properties["vti_irm_IrmPrint"      ] = 2; // Allow users to print documents
folder.Properties["vti_irm_IrmVBA"        ] = 2; // Allow users to access content programmatically
folder.Properties["vti_irm_IrmOffline"    ] = 2; // Users must verify their credentials every nOfflineDays
spList.IrmReject = true; // do not allow users to upload documents that do not support IRM
spList.IrmExpire = true; //sto[ restricting permissions to documents in this library on expiry date
folder.Properties["vti_irm_IrmOfflineDays"] = nOfflineDays;
// integer representing number of days after which user needs to verify credentials
folder.Properties["vti_irm_IrmExpireDate" ] = dtExpire.ToString("r", CultureInfo.InvariantCulture);
// date on which to stop restricting IRM permissions to documents
folder.Properties["vti_irm_IrmTitle"      ] = "Permission Policy Title";
folder.Properties["vti_irm_IrmDescription"] = "Permission Policy Description";
folder.Update();
spList.Update();

Phew! 

~harsh