I had to add a custom action to the Sharepoint expiry policy of a content type.
How to make a custom action, and add it to a policy in the Sharepoint configuration screen of the content type is not so hard - see http://jack.whyyoung.com/blog/www-sharepointblogs-com-MainFeed-aspx-GroupID-3/19732-aspx.htm for this.
But to add this policy to a content type automatically - in code int the feature event receiver of this content type - I had to search a little bit harder..
Use the Policy (in namespace Microsoft.Office.RecordsManagement.InformationPolicy) object (do not use the SPPolicy class, that's for other purposes :) ) to add the expiration policy programmatically to the content type. Here is the code:
using (SPSite site = (SPSite)properties.Feature.Parent)
{
string policyFeatureId = "Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration";
SPContentType administrativeDocumentContentType = site.RootWeb.ContentTypes[new SPContentTypeId("0x01010091C4CA4E2BA24AE9BD4F276750C0A5A9")];
//Add the custom action "AdministrativeDocumentExpirationSendEmail" to the policy resource collection
string actionmanifest = @"<?xml version=""1.0"" encoding=""utf-8"" ?><p:PolicyResource id=""Dolmen.SharePoint.Customer.ExpirationActions.AdministrativeDocumentExpirationSendEmail"" featureId=""Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration"" type=""Action"" xmlns:p=""urn:schemas-microsoft-com:office:server:policy""><p:LocalizationResources>dlccore</p:LocalizationResources><p:Name>AdministrativeDocumentExpirationSendEmail</p:Name><p:Description>Sends a mail on the Expiration date</p:Description><p:Publisher>Dolmen Computer Applications</p:Publisher><p:AssemblyName>Dolmen.SharePoint.Customer.ExpirationActions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=79b87b017c9b0654</p:AssemblyName><p:ClassName>Dolmen.SharePoint.Customer.ExpirationActions.AdministrativeDocumentExpirationSendEmail</p:ClassName></p:PolicyResource>";
//string actionmanifest = System.IO.File.ReadAllText("actionmanifest.xml");
PolicyResourceCollection.Add(actionmanifest);
if (Policy.GetPolicy(administrativeDocumentContentType) == null)
{
//if the content type hasn't got a Policy yet, create a new Policy
Policy.CreatePolicy(administrativeDocumentContentType, null);
}
Policy policyOfContentType = Policy.GetPolicy(administrativeDocumentContentType);
policyOfContentType.Name = "Administrative Document - Expiration Policy";
//Add expiration policy to the content type
if (policyOfContentType.Items[policyFeatureId] == null)
{
string customData = @"<data><formula id=""Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn""><number>0</number><property>Expiration</property><period>days</period></formula><action type=""action"" id=""Dolmen.SharePoint.Customer.ExpirationActions.AdministrativeDocumentExpirationSendEmail"" /></data>";
policyOfContentType.Items.Add(policyFeatureId, customData);
}
}
Tip: to get the customData and the policyFeatureId you can create a policy with the standard Sharepoint configuration screen and read it out in a console application using following code:
SPContentType administrativeDocumentContentType = site.RootWeb.ContentTypes[new SPContentTypeId("0x01010091C4CA4E2BA24AE9BD4F276750C0A5A9")];
Policy policy = Policy.GetPolicy(administrativeDocumentContentType);
foreach (PolicyItem item in policy.Items) {
Console.WriteLine("Custom data: " + item.CustomData + "\n policy feature id: " + item.Id);
}
6 comments:
Impressed with code and logic....great job...
Excellent,very useful information!
When i visited your blog it opened a popup-window showing pornography...
good post :)
Great job..this is what i am looking for, i tried this code and i am getting below error.
Save Conflict.
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.
Great post and amazing feature.. Thanks a lot! for sharing !!! Works like a charm.
Post a Comment