Home > Blogs > Blog Post

How to start the default approval workflow programmatically

by Hugo Esperanca | Sep 07, 2010

Today I was trying to find a way to programmatically create a publishing page and start the default approval workflow attached to the pages library. The reason that I had to programmatically start the workflow (instead of relying on SharePoint to automatically start it for me) was because my code was running under the system account (in this case under a WCF web service IIS process) and, since SP1, SharePoint will not automatically start a workflow if the list item is being changed by the system account (see this blog post for more information).

The main requirements were:

1) Determine what is the default content approval workflow associated with the Pages library.

2) Start the approval workflow when a Page is programmatically added to the Publishing Pages list but only if approval is enabled on the list.

3) Pass the default association data to the workflow (without asking the user).

Here is the solution:

/// <summary>
/// Creates the publishing page.
/// </summary>
/// <param name="web">
/// The destination web
/// </param>
/// <param name="pageContentTypeId">
/// The page content type id.
/// </param>
/// <param name="pageLayout">
/// The page layout to be used by the new publishing page.
/// </param>
/// <param name="checkInComments">
/// The check in comments.
/// </param>
public void CreatePublishingPage(SPWeb web, string pageName, string promotedPageContentTypeId, string pageLayout, string checkInComments)
{
    PublishingPage page = null;
 
 try
    {
 // Ensure the destination web is a publishing web
 if (!PublishingWeb.IsPublishingWeb(web))
        {
 throw new ArgumentException("Destination web is not a publishing web");
        }
 
 // Get the publishing web
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
 
 // Create the page
 // This code uses a Collaboris helper class to create the publishing page.
 // If you would like to see the source code for the helper method please let us know
        page = SPPublishingHelper.CreatePublishingPage(publishingWeb, pageContentTypeId, pageLayout, pageName);
 
 // Initialise the values the values of any publishing page properties here
 //.........
 
 // Update the page
        page.Update();
 
 // Checkin and publish the page
        page.ListItem.File.CheckIn(checkInComments);
        page.ListItem.File.Publish(checkInComments);
 
 // Get the default content approval workflow id that is a associated with the list
        Guid approvalworkflowId = page.ListItem.ParentList.DefaultContentApprovalWorkflowId;
 
 // If there is an apporval workflow associated with the list then we need to start it programmatically
 if (approvalworkflowId != Guid.Empty)
        {
 // Find the right workflow (there can be several associated with the pages list)
 foreach (SPWorkflowAssociation workflowAssociation in page.ListItem.ParentList.WorkflowAssociations)
            {
 if (workflowAssociation.Id == approvalworkflowId)
                {
 // We have found the workflow so start it
                    listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, workflowAssociation, workflowAssociation.AssociationData, true);
 break;
                }
            }
        }       
    }
 catch (Exception e)
    {
 // Rollback the changes
 // If the page has been created then we need to delete it
 if (page != null)
        {
            page.ListItem.File.Delete();
        }
 
 // Build a friendly error message and use a Collaboris helper class to log the error
 // Please let us know if you would like to see the code for the SPLogger class
 string friendlyMessage = string.Format(CultureInfo.CurrentCulture, SPResources.GetString("ModuleName", "FailedToCreatePageWithUrl"), destinationUrl);
        SPLogger.LogToOperations(e, friendlyMessage);
 
 throw;
    }
}
 

 

 

Compliance using DocRead

DocRead logoDocRead for SharePoint can help you manage policy compliance by:

  • Targeting documents or policies at specific groups of users
  • Allowing a specific amount of time for users to confirm agreement 
  • Sending email reminders when policy compliance is overdue
  • Users self-certify that they have read and fully understood the policy details
  • Securely storing records of confirmed policy acceptance
  • Monitoring the user acceptance of policies via a reporting suite
  • Providing detailed reading reports and statistics
  • Report drill through to show who has not accepted the policy
  • Automatically sending historic compliance tasks and policies to new users when they are added to a group
  • Bringing policy compliance requests immediately to users attention when they log on

DocRead is simple to install and configure. It seamlessly integrates with SharePoint and can be added to any existing SharePoint site.

To find out more, visit the DocRead product site.


blog comments powered by Disqus