This documentation is archived and is not being maintained.

SPContentType.AddWorkflowAssociation Method

Windows SharePoint Services 3

Adds the specified workflow association to the content type.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPWorkflowAssociation AddWorkflowAssociation(
	SPWorkflowAssociation workflowAssociation
)

Parameters

workflowAssociation
Type: Microsoft.SharePoint.Workflow.SPWorkflowAssociation
The SPWorkflowAssociation object to add to the content type’s collection.

Return Value

Type: Microsoft.SharePoint.Workflow.SPWorkflowAssociation
Returns an SPWorkflowAssociation object that represents the workflow.

ExceptionCondition
ArgumentException

The SPWorkflowAssociation object already exists in the collection, or it is based on an inappropriate workflow template.

ArgumentNullException

The SPWorkflowAssociation object is a null reference (Nothing in Visual Basic).

The workflow association cannot already exist in the content type’s collection. If it is already a member of the collection and you want to update the association, call the UpdateWorkflowAssociation method instead.

You can test for the existence of a workflow association in the content type’s collection by calling the GetAssociationByName method. If the return value of that method is not a null reference (Nothing in Visual Basic), then the association is already in the collection. The following example demonstrates this technique.

if (contentType.WorkflowAssociations.GetAssociationByName(workflowAssociation.Name, site.Locale) == null)
{
    contentType.AddWorkflowAssociation(workflowAssociation);
}
else
{
    contentType.UpdateWorkflowAssociation(workflowAssociation);
}

The following example is a console application that creates a workflow association, adds it to a site content type’s collection of workflow associations, and propagates the change to children of the content type.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            SPSite siteCollection = new SPSite("http://localhost");
            SPWeb site = siteCollection.OpenWeb();

            SPContentType siteContentType = site.ContentTypes["Test Document"];
            string taskListTitle = "Tasks";
            string historyListTitle = "Workflow History";
            string workflowName = "Red-Yellow-Green";

            // Get a template.
            SPWorkflowTemplate workflowTemplate = null;
            foreach (SPWorkflowTemplate template in site.WorkflowTemplates)
            {
                workflowTemplate = template;

                // We'll take a template everyone has.
                if (workflowTemplate.Name == "Three-state") break;
            }

            // Create an association.
            SPWorkflowAssociation workflowAssociation = 
                SPWorkflowAssociation.CreateSiteContentTypeAssociation(workflowTemplate, 
                                                                       workflowName, 
                                                                       taskListTitle, 
                                                                       historyListTitle);

            // Add the association to the content type or update it if it already exists.
            Console.Write("Workflow association {0} has been ", workflowAssociation.Name);
            if (siteContentType.WorkflowAssociations.GetAssociationByName(workflowAssociation.Name, site.Locale) == null)
            {
                siteContentType.AddWorkflowAssociation(workflowAssociation);
                Console.WriteLine("added.");
            }
            else
            {
                siteContentType.UpdateWorkflowAssociation(workflowAssociation);
                Console.WriteLine("updated.");
            }

            // Propagate to children of this content type.
            siteContentType.UpdateWorkflowAssociationsOnChildren(false,  // Do not generate full change list
                                                                 true,   // Push down to derived content types
                                                                 true,   // Push down to list content types
                                                                 false); // Do not throw exception if sealed or readonly  

            site.Dispose();
            siteCollection.Dispose();

            Console.WriteLine();
            Console.Write("Press ENTER to continue...");
            Console.ReadLine();
        }
    }
}
Show: