Creating Workflows to Transfer Records Upon Expiration in SharePoint Server 2007

SharePoint 2007

Summary:  Learn how you can create a workflow to transfer records and then associate the workflow as the action of an expiration policy in Microsoft Office SharePoint Server 2007.

Office Visual How To

Applies to:  Microsoft Office SharePoint Server 2007, Windows SharePoint Services 3.0, Microsoft Visual Studio 2008

Microsoft Corporation

July 2008


When records expire, it is a common practice to transfer them from the records repository to another location as part of a disposition process. In this Microsoft Office Visual How To, you create a workflow to transfer records and then associate the workflow as the action of an expiration policy in Microsoft Office SharePoint Server 2007.

When records expire, the workflow uses the Content Migration API to transfer the file and associated metadata to the file system as a CAB file, write the event to the Office SharePoint Server audit log, and then send a notification e-mail message.

Code It

The first step is to create a SharePoint Sequential Workflow project in Microsoft Visual Studio 2008. This project contains the logic to transfer the record, write to the audit log, and send an e-mail message to a group of users. Upon association of the workflow to a SharePoint library, you want to capture information to use during the transfer process. The same form is also used for initiation. This information includes the transfer location, the notification message to be used in both the audit log and e-mail message, and the e-mail address for sending the e-mail message. Then, you create a Microsoft Office InfoPath 2007 form to capture and display the information to the user.

Figure 1. Infopath form

InfoPath form

You set up the layout of the fields as text fields on your InfoPath form, and then you add a Submit button, as shown in Figure 2. You configure the Submit button to submit to the host application and close the form.

Figure 2. Added Submit button on InfoPath form

Added Submit button on InfoPath form

You then prepare the form for the workflow process following the typical steps:

  1. Web-enable the form.

  2. Use Xsd.exe to create a class, and add the class to your workflow project.

  3. Add the form to your workflow project.

  4. Configure the Feature.xml and Workflow.xml files to use the form for association and initiation.

For information about setting up and working with a SharePoint sequential workflow, see the following:

Next, you add the logic to the workflow process. In addition to the OnWorkflowActivated activity that is provided by the project template, you add a Code activity and a SendEmail activity. The activities do the following:

  • onWorkflowActivated1 - OnWorkflowActivity activity used to retrieve the values from the association/initiation form.

  • codeActivity1 - Code activity used to transfer the file and metadata and write to the audit log.

  • sendEmail1 - SendEmail activity used to send an e-mail message.

Figure 3 shows the sequential workflow.

Figure 3. SharePoint sequential workflow

Sequential workflow in SharePoint Server

Retrieving the Values from the InfoPath Form

The onWorkflowActivity1 activity is configured to invoke the onWorkflowActivated method. Within the activity, you retrieve the values from the association/initiation form. You create three private variables to hold the values. You obtain the values by deserializing the InitiationData property of the workflowProperties object by making use of the partial class generated from the InfoPath form.

private string transferLocation = string.Empty;
        private string notificationMessage = string.Empty;
        private string notificationGroup = string.Empty;

        private void onWorkflowActivated(object sender, ExternalDataEventArgs e)
            workflowId = workflowProperties.WorkflowId;
            XmlSerializer serializer = new XmlSerializer(typeof(myFields));
            XmlTextReader reader = new XmlTextReader
                (new System.IO.StringReader(workflowProperties.InitiationData));
                myFields initiationForm = (myFields)serializer.Deserialize(reader);
                transferLocation = initiationForm.transferLocation;
                notificationMessage = initiationForm.notificationMessage;
                notificationGroup = initiationForm.notificationGroup;
            catch (Exception ex)

                // Add exception handling.

Transferring the File and Writing to the Audit Log

The next activity, codeActivity1, performs the transfer, or export, of the file and writes to the audit log. In this Code activity, you use the Content Management API to export the file and metadata to the file system. The classes that are used for the export—the SPExportObject class, the SPExportSettings class, and the SPExport class—are in the Microsoft.SharePoint.Deployment namespace.

using Microsoft.SharePoint.Deployment;

To export your file, you create an SPExport object that represents the item to transfer. The item to transfer is the item that is being processed by the workflow and is obtained as an SPListItem. Then, you define all of the export settings using an SPExportSettings object. In this example, you generate your own file name, which will be the name of the file with an appended .cab extension. You set the location to export the file using the transferLocation variable, which is defined from the value in the initiation form. Next, you add the SPExportObject to the SPExportSettings object, which you then use to instantiate a new SPExport object. Then, you call the Run method on the SPExport object, which creates the CAB file and places it in the transfer location.

        private void TransferRecord(object sender, EventArgs e)
                string webUrl = workflowProperties.Web.Url;
                SPListItem item = workflowProperties.Item;

                // Create an object for the record to transfer.
                SPExportObject exportObject = new SPExportObject();
                exportObject.Type = SPDeploymentObjectType.ListItem;
                exportObject.Url = webUrl + "/" + item.Url;

                // Set the export settings.
                SPExportSettings exportSettings = new SPExportSettings();
                exportSettings.ExportMethod = SPExportMethodType.ExportAll;
                exportSettings.IncludeSecurity = SPIncludeSecurity.All;
                exportSettings.IncludeVersions = SPIncludeVersions.All;

                exportSettings.AutoGenerateDataFileName = false;
                exportSettings.BaseFileName = item.Name + ".cab";
                exportSettings.SiteUrl = webUrl;
                exportSettings.FileLocation = transferLocation;

                // Export the record.
                SPExport export = new SPExport(exportSettings);

                // Write transfer event to audit log.
                item.Audit.WriteAuditEvent("Record Transfer", 
                    "Information Management Policy Workflow", 
                    "<TransferMessage>" + notificationMessage + "</TransferMessage>");
            catch (Exception ex)
                // Add exception handling.

After the file is exported, you write a message to the audit log by calling the Audit.WriteAuditEvent method on the SPListItem that you are processing. You pass in the event name and event source, which are "Record Transfer" and "Information Management Policy Workflow," and an XML string that represents the message obtained from the InfoPath form.

Sending an E-Mail Message

In the final activity, you send an e-mail message. Within the SendEmail1 activity, you set the properties for the To, Subject, and Body fields of the e-mail message. The To field is the e-mail address that is obtained from the Notification Group field in the initiation form. The notification message obtained from the initiation, which was written to the audit log, is also used as the body of the e-mail message.

        public String sendEmail1_To1 = default(System.String);
        public String sendEmail1_Subject1 = default(System.String);
        public String sendEmail1_Body1 = default(System.String);

        private void sendEmail(object sender, EventArgs e)
            sendEmail1_To1 = notificationGroup;
            sendEmail1_Subject1 = "Record Transfer Notification";
            sendEmail1_Body1 = notificationMessage;

Associating the Workflow as the Action of an Expiration Policy

When the workflow is completed, package it and deploy it as a SharePoint Feature named TransferWorkflow. The next step is to associate this workflow with a library in your Records Center. During association, you name the workflow "Record Transfer" and set the fields in the association form, as shown in Figure 4.

Figure 4. Record Transfer form

Record transfer form

The final step is to use this workflow as the action to perform when an item expires. In this example, you use a custom expiration formula that immediately expires an item. For information about how to set up a custom expiration formula, see Creating a Custom Expiration Formula Based On Metadata in SharePoint Server 2007 (Part 1 of 2). For your expiration policy, configure your "Record Transfer" workflow to start when an item expires, as shown in Figure 5.

Figure 5. Set expiration policy to Record Transfer workflow

Set expiration policy to Record Transfer workflow

When the expiration timer runs, it immediately marks all items in the library as expired and uses the workflow to export them to the defined location. Then, the workflow exports the file, writes an entry to the audit log, and sends a notification e-mail message.

Read It

In this example, you use the Content Migration API to export a file to the file system. It conveniently exports the file and associated metadata as a group in a CAB file. From within the workflow activity, you have access to the item and all of its metadata, so if you want, you can use alternative methods for file export. However, by using the Content Migration API, you are able to not only export the file and associated information—including metadata, security, and role information—into a single content migration package, but you can also easily import this information into another Windows SharePoint Services site, such as an archival repository. Additionally, the content migration package is a CAB file that can be opened by appending a .cab extension to the file name. This CAB file contains the original file and several XML files that describe what has been exported.

In this example, after the file is transferred, you do not process the file that remains in the library. You can easily extend the workflow to meet specific retention requirements, such as deleting the file or retaining only the file metadata.

By writing to the audit log, you can keep detailed track of the life-cycle events of the record. With SharePoint Server, you can generate reports based on the captured audit information. Using this capability, you can easily generate a report that contains your custom entry showing that the record was transferred, as shown in Figure 6.

Figure 6. Audit log report

Audit log report
See It

Creating Workflows to Transfer Records

Watch the Video

Video Length: 00:10:05

File Size: 7.27 MB WMV

Explore It

Community Additions