Share via


SharePoint OM APIs to get/set 'Information Management Policy Settings'

How To:

SharePoint 2007 has settings under "Information Management Policy Settings" for Libraries and Lists. Need to be able get/set the properties in this settings via the Object Model API.

Steps:

To get the policy details:

SPSite site = new SPSite("<https://terminator:4400>");
SPWeb web = site.OpenWeb();
SPList list = web.Lists["irmdoclib"];
SPContentType ct = list.ContentTypes["Document"];
Policy policy = Policy.GetPolicy(ct);
foreach (PolicyItem item in policy.Items)
{
string name = item.Name;
string customData = item.CustomData;
}

The name will give you the “Label”, if you have enabled it, the “CustomData” will be in xml form.

To set the policy details:

SPContentType spContentType = list.ContentTypes[strContentType];

if (Policy.GetPolicy(spContentType) == null)
{
//if the content type hasn't got a Policy yet, create a new
Policy
Policy.CreatePolicy(spContentType, null);
}

Policy policy = Policy.GetPolicy(spContentType);
policy.Description = strPolicyDescription;
policy.Statement = strStatement;
policy.Update();

A few words about this code:

The Policy.GetPolicy() function, it internally creates a new policy object and sets the value of SPFile to null (ModifiedDate and ModifiedBy are the properties of SPFile). No way are you going to get these values. I don’t see anywhere a call being made to the private overloaded constructor that takes SPFile object (only one place where the SPFile object gets assigned). So these properties are not going to give you any values.

Unless you access the ModifiedBy and ModifiedDate properties, you will not get the exception. If you are getting the exception while executing the code, that may be because of some other change.