In the first part of this series, I introduced a custom information management policy. In the previous post I created and registered the custom policyfeature. We ended up with a policy that we could create and setup. In this post, we’ll create the handler that will make the policy do some work. The sample policy that I was working on is the “policy of truth”. In case a user submits an item containing the word “truth” in combination with one of the keywords in the policy setup, the policy will do it’s work. Because I was also preparing for the beta exam 70–542, I decided to submit the item to a records repository. If you want to learn more about the Records Center in SharePoint, this item on the Records Management Team Blog is a good starting point.
I will post the most important code bits here. When the series is complete, I will post the full code (I’ll need to do some cleaning up first….)
Step 1 – Create the repository handler
First we will create a new class that will handle the list items. It will check if the item needs to be submitted and if so, it will submit the item. Our “RepositoryHandler” has a method called “HandleListItem”. This takes a SPListItem as parameter.
Step 2 - Get the keywords from the policy
First the handler needs to find out what keywords the user has set when setting up the policy:
private string GetKeywords(SPListItem item)
{ // get the keywords from the policy options
Policy policy = Policy.GetPolicy(item.ContentType);
PolicyItem policyItem = policy.Items[PolicyOfTruth.PolicyId];
string keywords = string.Empty;
using (XmlReader reader = XmlReader.Create(new System.IO.StringReader(policyItem.CustomData)))
{ reader.ReadStartElement("data"); reader.ReadStartElement("keywords"); keywords = reader.ReadString();
reader.ReadEndElement();
reader.ReadEndElement();
}
return keywords;
}
The PolicyId of the PolicyOfTruth feature is a static property that just returns the id of the policy:
public static string PolicyId
{ get { return "TST.POC.PolicyFeatures.PolicyOfTruth"; } }
Step 3 - Check if we need to submit the item
This bit of code checks if any of the keywords and the word ‘truth’ is found in one of the metadata fields of the list item. This code is very simple, because my goal was to get the policy working, not to create something for a useful scenario.
private bool HandleItem(SPListItem item, string keywords)
{ bool handle = false;
// for now just do documents in a doclib.
if (item.ParentList is SPDocumentLibrary)
{ string[] keywordItems = keywords.Split(new char[] { ';' }); foreach (string keyword in keywordItems)
{ foreach (SPField field in item.Fields)
{ if (item[field.Id] != null)
{ string value = item[field.Id].ToString();
if ((value.ToLower().IndexOf(keyword.ToLower().Trim()) > -1) &&
(value.ToLower().IndexOf("truth") > -1)) { handle = true;
break;
}
}
}
if (handle) break;
}
}
return handle;
}
Step 4 - Setup the connection to the records center
To submit the item to the records center, I used the code sample from the ECM Starter Kit (part of the MOSS SDK). First you need to get a web reference to the webservice of the records center. If you don’t have a record center yet, the first thing to do is create one. The webservice can be found on this url: http://office2007:3736/_vti_bin/OfficialFile.asmx. In my case the records center is running on port 3736 of my server called ‘office2007’. My web reference is called ‘Repository’. Setup a connection:
TST.POC.PolicyFeatures.Repository.RecordsRepository repository = new Repository.RecordsRepository();
repository.Credentials = System.Net.CredentialCache.DefaultCredentials;
repository.PreAuthenticate = true;
Step 5 – Add properties for the records center
The next thing is to add a property to the item that will get submitted to the records repository. In this property we will save the accountname of the user who initially created the item. To do this, you create a new RecordRepositoryProperty:
Repository.RecordsRepositoryProperty[] repositoryProperties = new Repository.RecordsRepositoryProperty[1];
repositoryProperties[0] = new Repository.RecordsRepositoryProperty();
repositoryProperties[0].Name = "SubmittedBy";
repositoryProperties[0].Type = "Text";
repositoryProperties[0].Value = item["Created By"].ToString();
Step 6 – Submit the document
To submit the document to the records center, you first have to read it into a byte array. Then call the SubmitFile method. This takes the byte array, the properties array we created as parameters. You also need the pass the name of the routing. I have not yet created a special routing for my items, so I used the default routing called “Unclassified Records”.
byte[] doc = item.File.OpenBinary();
string result = repository.SubmitFile(doc, repositoryProperties,
"Unclassified Records", item.Url, item.Web.CurrentUser.Name);
Step 7 – Handling the result
The SubmitFile method of the OfficialFile webservice returns a xml string. Here is a way to find out what happened. In case of success, the handler updates the item that was submitted. It saves the current datetime in a custom field that was added to the content type by the policy.
result = string.Format("<Result>{0}</Result>", result); XmlDocument xml = new XmlDocument();
xml.LoadXml(result);
XmlElement root = xml.DocumentElement;
string resultCode = root.SelectSingleNode("ResultCode").FirstChild.Value; string additionalInformation = string.Empty;
if (root.SelectSingleNode("AdditionalInformation") != null) { additionalInformation = root.SelectSingleNode("AdditionalInformation").FirstChild.Value; }
if (resultCode == "Success")
{ item[truthFieldName] = DateTime.Now;
item.Update();
}
return string.Format("Submitted to records center: {0} - {1}", resultCode, additionalInformation);
In my example code it generated a new result string that is returned by the HandleListItem method on the repository handler.
In this post we created the handler that makes our policy do some work. In the next post, I will put it all together, so that it will be a working custom information management policy.
Update 15–02–2007 – Added overview of all parts:
- Part 1 – introduction and creating the policy feature
- Part 2 – implementing the handler and submitting to a records center
- Part 3 – implementing and testing the policy
Posted
02-12-2007 9:43 PM
by
tonstegeman