Finding a workflow using an activity

Had an issue come up where we wanted to find all the workflows in a SharePoint 2007 environment that were using a certain workflow activity.  Each list has a workflow association collection that tells you the workflows that are configured for the list.  From the workflow association, we were able to find that each workflow association has a BaseTemplate object that points to the assembly and class name for the workflow.  With that information, we were able to use reflection to get the ActivityCollection

Here’s an example of what that code looks like.  This finds both SharePoint Designer and Visual Studio based workflows (also includes the OOB workflows).  The output is comma-delimited, showing the following data :

URL of the web, List Title, Workflow Association Name, type of Workflow Activity.

Once you get the output, you can open the comma-delimited file in Excel, and filter out whatever data you want.  For example, you could filter out the workflows on a particular web.  You could also find any workflow association that uses a CodeActivity.

    1: //Prior to this, you loop through the SPLists in each SPWeb you care about
    2: foreach (SPWorkflowAssociation wa in list.WorkflowAssociations)
    3: {
    4:     //Grab the XML from the BaseTemplate of the workflow association
    5:     XmlDocument templateXML = new XmlDocument();
    6:     templateXML.LoadXml(wa.BaseTemplate.Xml);
    8:     //Grab the name of the assembly and the class that the workflow association is pointing to
    9:     string assemblyName = templateXML.GetElementsByTagName("WorkflowTemplateIdSet")[0].Attributes["CodeBesideAssm"].Value;
   10:     string className = templateXML.GetElementsByTagName("WorkflowTemplateIdSet")[0].Attributes["CodeBesideClass"].Value;
   12:     //Load the assembly
   13:     Assembly assm = Assembly.Load(assemblyName);
   15:     //Loop through the modules in the assembly
   16:     foreach (Module mod in assm.GetLoadedModules())
   17:     {
   18:         //Loop through the types in the module
   19:         foreach (Type type in mod.GetTypes())
   20:         {
   21:             //Grab the Properties out of the type
   22:             object objs = null;
   23:             objs = type.InvokeMember("Properties", BindingFlags.CreateInstance, null, null, null);
   25:             try
   26:             {
   27:                 //Cast the object as a WorkflowActivity, grab the Activities collection
   28:                 ActivityCollection activities = ((System.Workflow.Activities.SequentialWorkflowActivity)objs).Activities;
   30:                 //Loop through the activities and write them out
   31:                 foreach (Activity act in activities)
   32:                 {
   33:                     Console.WriteLine(web.Url + ", " + list.Title + " , " + wa.Name + ", " + act.GetType().ToString());
   34:                 }
   35:             }
   36:             catch { continue; }
   38:         }    //end foreach on objects
   40:     }    //end foreach modules
   42: }  //end Workflow Association Loop