Sei sulla pagina 1di 4

27/3/2014 SPContentType.WorkflowAssociations property (Microsoft.

SharePoint)
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.workflowassociations.aspx 1/4
SPContentType.WorkflowAssociations
property
Gets an SPWorkflowAssociationCollection object that represents the workflow associations for this content type.
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
Property value
Type: Microsoft.SharePoint.Workflow.SPWorkflowAssociationCollection
The collection of workflows associated with the content type.
Remarks
The WorkflowAssociations property returns an SPWorkflowAssociationCollection object with the content types collection
of workflow associations. You can retrieve an association from the collection by using the value of the Id property as an
indexer or by passing the workflow associations name to the GetAssociationByName method.
Each workflow association in a collection must have a unique value in the Name property. When you add a new workflow
association, you should test to see if a workflow association by that name already exists in the collection. If your workflow
association is already a member of the collection and you want to update the existing association, call the Update
method instead.
You can test for the existence of a workflow association in the content types 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.
SharePoint 2013 This topic has not yet been rated
[ClientCallableAttribute]
public SPWorkflowAssociationCollection WorkflowAssociations { get; }
if (contentType.WorkflowAssociations.GetAssociationByName(workflowAssociation.Name, site.Locale) ==
{
contentType.WorkflowAssociations.Add(workflowAssociation);
}
else
{
contentType.WorkflowAssociations.Update(workflowAssociation);
}
C#
C#
27/3/2014 SPContentType.WorkflowAssociations property (Microsoft.SharePoint)
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.workflowassociations.aspx 2/4
Examples
The following example is a console application that creates a workflow association, adds it to a site content types
collection of workflow associations, and then 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) ==
{
siteContentType.WorkflowAssociations.Add(workflowAssociation);
Console.WriteLine("added.");
}
else
{
siteContentType.WorkflowAssociations.Update(workflowAssociation);
Console.WriteLine("updated.");
}
// Propagate to children of this content type.
siteContentType.UpdateWorkflowAssociationsOnChildren(false, // Do not generate full change list
C#
27/3/2014 SPContentType.WorkflowAssociations property (Microsoft.SharePoint)
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.workflowassociations.aspx 3/4
The following example is a console application that removes the workflow association that is created by the previous
example.
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();
}
}
}
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
namespace Test
{
class ConsoleApp
{
static void Main(string[] args)
{
using (SPSite site = new SPSite("http://localhost"))
{
using (SPWeb web = site.OpenWeb())
{
string ctName = "Test Document";
string wfName = "Red-Yellow-Green";
SPContentType contentType = web.ContentTypes[ctName];
if (null != contentType)
{
SPWorkflowAssociation wfAssociation =
contentType.WorkflowAssociations.GetAssociationByName(wfName, web.Locale);
if (null != wfAssociation)
{
// Remove the workflow association.
contentType.WorkflowAssociations.Remove(wfAssociation);
Console.WriteLine("The association with {0} workflow has been removed."
}
else
{
Console.WriteLine("An association with {0} workflow was not found.", wfName);
}
}
else
{
Console.WriteLine("Content type {0} does not exist.", ctName);
}
C#
27/3/2014 SPContentType.WorkflowAssociations property (Microsoft.SharePoint)
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spcontenttype.workflowassociations.aspx 4/4
Community Additions
See also
Reference
SPContentType class
SPContentType members
Microsoft.SharePoint namespace
Add(SPWorkflowAssociation)
Remove(SPWorkflowAssociation)
Other resources
Introduction to Workflows in Windows SharePoint Services
"There is already a workflow association with this name" on WorkflowAssociations.Update
The Update / Add code example is erroneous. You should not call the Update method on the new instance of the SPWorkflowAssociation,
as in this case internally a call to the Add method occurs, and due to the existing SPWorkflowAssociation instance with the same name in
the collection, an exception is thrown.
The detailed explanation and the suggested solution can be found here:
http://pholpar.wordpress.com/2013/06/02/there-is-already-a-workflow-association-with-this-name-on-workflowassociations-update/
Peter Holpar, MVP
6/2/2013
2014 Microsoft. All rights reserved.
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
}
}

Potrebbero piacerti anche