Export (0) Print
Expand All

Registering Workflow Activities for Declarative Workflows

To be able to use a full-trust workflow activity in a declarative workflow, you must add an authorized type entry for the activity class to the Web.config file. Typically, you should use a feature receiver class to add and remove these entries when you deploy your workflow activities.

The Workflow Activities Reference Implementation (Workflow Activities RI) includes an empty farm-scoped feature named SiteProvisioningActivity. The event receiver class for this feature adds authorized type entries for the workflow activity classes when the feature is activated, and it removes the entries when the feature is deactivated.

Ff798302.note(en-us,PandP.10).gifNote:
Farm-scoped features are automatically activated when the solution is deployed. Because of this, the authorized type entry is automatically added to the Web.config file when the solution package is deployed to the farm.

The SiteProvisioningActivity.EventReceiver class uses a helper method named GetConfigModification to build the authorized type entry for the workflow activity classes as an SPWebConfigModification object, as shown in the following code example.

public SPWebConfigModification GetConfigModification()
{
  string assemblyValue = typeof(CreateSubSiteActivity).Assembly.FullName;
  string namespaceValue = typeof(CreateSubSiteActivity).Namespace;

  SPWebConfigModification modification = new SPWebConfigModification(
     string.Format(CultureInfo.CurrentCulture,                 
     "authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*']
     [@Authorized='True']", assemblyValue, namespaceValue), 
     "configuration/System.Workflow.ComponentModel.WorkflowCompiler/
     authorizedTypes");
            
  modification.Owner = "Patterns and Practices";
  modification.Sequence = 0;
  modification.Type = 
    SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
  modification.Value = 
    string.Format(CultureInfo.CurrentCulture, 
                  "<authorizedType Assembly=\"{0}\" Namespace=\"{1}\"                   
                   TypeName=\"*\" Authorized=\"True\" />", assemblyValue, 
                   namespaceValue);

  Trace.TraceInformation("SPWebConfigModification value: {0}",modification.Value);

  return modification;
}

For more information about the SPWebConfigModification class, see SPWebConfigModification Class on MSDN.

The FeatureActivated method uses the SPWebConfigModification object to add the authorized type entry to the Web.config files of all Web applications on the farm, with the exception of the Central Administration Web application. This is shown in the following code example.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  try
  {
    SPWebService contentService = SPWebService.ContentService;
    contentService.WebConfigModifications.Add(GetConfigModification());
    
    // Serialize the Web application state and propagate changes across the farm. 
    contentService.Update();
    
    // Save Web.config changes.
    contentService.ApplyWebConfigModifications();
  }
  catch (Exception e)
  {
    Console.WriteLine(e.ToString());
    throw;
  }
}

This adds the following entry as a child of the AuthorizedTypes element in the Web.config file.

<authorizedType Assembly="ExecutionModels.Workflow.FullTrust.Activities, ..."  
                Namespace="ExecutionModels.Workflow.FullTrust.Activities" 
                TypeName="*" 
                Authorized="True" />

The FeatureDeactivating method also uses the helper method; this time, it is used to remove the authorized type entry from the Web.config file, as shown in the following code example.

public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
  try
  {
    SPWebService contentService = SPWebService.ContentService;
    contentService.WebConfigModifications.Remove(GetConfigModification());
    
    // Serialize the Web application state and propagate changes across the farm. 
    contentService.Update();
    
    // Save Web.config changes.
    contentService.ApplyWebConfigModifications();
  }
  catch (Exception e)
  {
    Console.WriteLine(e.ToString());
    throw;
  }
}


Show:
© 2014 Microsoft