Bernhard's SharePoint

March 15, 2010

How to remove content type associated workflows and status fields.

Filed under: C#, ContentType, SharePoint 2007, Workflow — Tags: , , , — Bernhard @ 19:05

Usually I deploy my SharePoint applications via SharePoint solutions. I also use FeatureReceivers written in C# to initialize webs, lists and more to keep things consistant.

When associating workflows with content types or lists a workflow status field is automatically generated and attached to the target list and default view. Doing this programmatically, for example by a FeatureReceiver, it might happen that status fields are added while creating a workflow association, but will not be removed in case of buggy code.

To cleanup the application you need to call a method like below:

RemoveSiteContentTypeAssociation(site, "My workflow title");

Site content types are mighty SharePoint elements. They can be inherited to child content types und used in lists. When you attach a workflow to a content type a lot of children and list might be influenced. So cleaning up your site collection can be tricky and time consuming.

The following method does the job. It crawls through your site collection finds all target workflow associations in webs, subwebs, content types and lists and finale removes all workflow status fields from the lists and the associations of course. Result: your workflow is completely removed from the site collection.

If you suggest a better method let me know.


public static void RemoveSiteContentTypeAssociation(SPSite site, string associationName)
{
    SPWeb rootWeb = site.RootWeb;
    SPWorkflowAssociation association = null;
    Console.WriteLine("Removing site contenttype workflow association '{0}'.", associationName);

    // Remove all site content type workflow associations
    foreach (SPContentType ctype in rootWeb.ContentTypes)
    {
        // Remove site content type workflow association
        association = ctype.WorkflowAssociations.GetAssociationByName(associationName, rootWeb.Locale);
        if (association != null) ctype.RemoveWorkflowAssociation(association);
    }

    // Remove list content type workflow associations
    foreach (SPWeb web in site.AllWebs)
    {
        // Since we plan to modify some lists later we can not use the standard web.Lists collection
        System.Collections.Generic.List<Guid> lists = new System.Collections.Generic.List<Guid>();
        foreach (SPList list in web.Lists) lists.Add(list.ID);

        // Check all lists
        foreach (Guid listId in lists)
        {
            // Get list
            SPList list = web.Lists[listId];

            // Remove all matching list content type associations
            foreach (SPContentType ctype in list.ContentTypes)
            {
                // Remove list content type workflow association
                association = ctype.WorkflowAssociations.GetAssociationByName(associationName, web.Locale);
                if (association != null) ctype.RemoveWorkflowAssociation(association);
            }

            // Find all workflow status fields in the list
            System.Collections.Generic.List<Guid> fields = new System.Collections.Generic.List<Guid>();
            foreach (SPField field in list.Fields)
            {
                // Check field and save Guid on match
                if (field.Type == SPFieldType.WorkflowStatus && field.Title == associationName) fields.Add(field.Id);
            }

            // Remove the fields from the list
            foreach (Guid fieldId in fields)
            {
                SPField f = list.Fields[fieldId];   // Get the status field
                f.ReadOnlyField = false;            // Flag read/write
                f.Update(true);                     // Update first otherwise the field cannot be deleted
                f.Delete();                         // Now delete
            }

            // Save modified list in database
            list.Update();
        }
        web.Dispose();
    }
}

Advertisement

2 Comments »

  1. nifty little tool for the toold box.. great work.

    Comment by fabiangwilliams — March 15, 2010 @ 19:10

  2. This is just what I was looking for! I’ve got to delete around 500,000 workflow associations … I’ll be damned if I’m going to be doing that by hand!! :)

    Comment by Craig Porter — January 18, 2011 @ 16:23


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.