SPContentType.WorkflowAssociations-Eigenschaft

Ruft ein SPWorkflowAssociationCollection -Objekt, das die Workflowzuordnungen für diesen Inhaltstyp darstellt.

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

Syntax

'Declaration
Public ReadOnly Property WorkflowAssociations As SPWorkflowAssociationCollection
    Get
'Usage
Dim instance As SPContentType
Dim value As SPWorkflowAssociationCollection

value = instance.WorkflowAssociations
public SPWorkflowAssociationCollection WorkflowAssociations { get; }

Eigenschaftswert

Typ: Microsoft.SharePoint.Workflow.SPWorkflowAssociationCollection
Die Auflistung von Workflows, die den Inhaltstyp zugeordnet sind.

Hinweise

Die WorkflowAssociations -Eigenschaft gibt ein SPWorkflowAssociationCollection -Objekt mit den Inhaltstyp-Auflistung von Workflowzuordnungen. Sie können eine Zuordnung aus der Auflistung abrufen, mit dem Wert der Eigenschaft Id als Indexer oder, indem Sie die Workflowzuordnung Namen an die GetAssociationByName -Methode übergeben.

Jede Workflowzuordnung in einer Auflistung muss einen eindeutigen Wert in der Eigenschaft Name haben. Wenn Sie eine neue Workflowzuordnung hinzufügen, sollten Sie testen, um festzustellen, ob bereits eine Workflowzuordnung mit diesem Namen in der Auflistung vorhanden ist. Wenn Ihre Workflowzuordnung bereits ein Element der Auflistung ist, und Sie die bestehende Zuordnung aktualisieren möchten, rufen Sie stattdessen die Update -Methode.

Sie können das Vorhandensein einer Workflowzuordnung in den Inhaltstyp-Auflistung durch Aufrufen der Methode GetAssociationByName testen. Wenn Sie der Rückgabewert dieser Methode nicht ein Nullverweis (Nothing in Visual Basic)ist, ist die Zuordnung bereits in der Auflistung. Das folgende Beispiel veranschaulicht diese Vorgehensweise.

If contentType.WorkflowAssociations.GetAssociationByName(workflowAssociation.Name, site.Locale) Is Nothing Then
    contentType.WorkflowAssociations.Add(workflowAssociation)
Else
    contentType.WorkflowAssociations.Update(workflowAssociation)
End If
if (contentType.WorkflowAssociations.GetAssociationByName(workflowAssociation.Name, site.Locale) == null)
{
    contentType.WorkflowAssociations.Add(workflowAssociation);
}
else
{
    contentType.WorkflowAssociations.Update(workflowAssociation);
}

Beispiele

Das folgende Beispiel ist eine Konsolenanwendung, die eine Workflowzuordnung erstellt, einem Websiteinhaltstyp die Auflistung von Workflowzuordnungen hinzugefügt, und klicken Sie dann die Änderung untergeordnete Elemente des Inhaltstyps.

Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Workflow

Module ConsoleApp

    Sub Main()
        Console.WriteLine()

        Dim siteCollection As SPSite = New SPSite("https://localhost")
        Dim site As SPWeb = siteCollection.OpenWeb()

        Dim siteContentType As SPContentType = site.ContentTypes("Test Document")
        Dim taskListTitle As String = "Tasks"
        Dim historyListTitle As String = "Workflow History"
        Dim workflowName As String = "Red-Yellow-Green"

        ' Get a template.
        Dim workflowTemplate As SPWorkflowTemplate = Nothing
        For Each template As SPWorkflowTemplate In site.WorkflowTemplates
            workflowTemplate = template
            ' We'll take a template everyone has
            If workflowTemplate.Name = "Three-state" Then
                Exit For
            End If
        Next template

        ' Create an association.
        Dim workflowAssociation As SPWorkflowAssociation = _
                  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) Is Nothing Then
            siteContentType.WorkflowAssociations.Add(workflowAssociation)
            Console.WriteLine("added.")
        Else
            siteContentType.WorkflowAssociations.Update(workflowAssociation)
            Console.WriteLine("updated.")
        End If

        ' Propagate to children of this content type.
        siteContentType.UpdateWorkflowAssociationsOnChildren(False, True, True, False)

        ' Clean up.
        site.Dispose()
        siteCollection.Dispose()

        Console.WriteLine()
        Console.Write("Press ENTER to continue...")
        Console.ReadLine()
    End Sub

End Module
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            Console.WriteLine();
            SPSite siteCollection = new SPSite("https://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.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
                                                                 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();
        }
    }
}

Das folgende Beispiel ist eine Konsolenanwendung, mit die die Workflowzuordnung entfernt, die im vorherigen Beispiel erstellt wird.

Imports System
Imports Microsoft.SharePoint
Imports Microsoft.SharePoint.Workflow

Module ConsoleApp

    Sub Main()
        Dim site As SPSite = New SPSite("https://localhost")
        Try
            Dim web As SPWeb = site.OpenWeb()
            Try
                Dim ctName As String = "Test Document"
                Dim wfName As String = "Red-Yellow-Green"

                Dim contentType As SPContentType = web.ContentTypes(ctName)
                If Not contentType Is Nothing Then
                    Dim wfAssociation As SPWorkflowAssociation = _
                        contentType.WorkflowAssociations.GetAssociationByName(wfName, web.Locale)
                    If Not wfAssociation Is Nothing Then
                        ' Remove the workflow association.
                        contentType.WorkflowAssociations.Remove(wfAssociation)
                        Console.WriteLine("The association with {0} workflow has been removed.", wfAssociation.Name)
                    Else
                        Console.WriteLine("An association with {0} workflow was not found.", wfName)
                    End If
                Else
                    Console.WriteLine("Content type {0} does not exist.", ctName)
                End If

            Finally
                web.Dispose()
            End Try
        Finally
            site.Dispose()
        End Try
        Console.Write(vbCrLf + "Press ENTER to continue...")
        Console.ReadLine()
    End Sub
End Module
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;

namespace Test
{
    class ConsoleApp
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("https://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.", wfAssociation.Name);
                        }
                        else
                        {
                            Console.WriteLine("An association with {0} workflow was not found.", wfName);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Content type {0} does not exist.", ctName);
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

Siehe auch

Referenz

SPContentType Klasse

SPContentType-Member

Microsoft.SharePoint-Namespace

Add(SPWorkflowAssociation)

Remove(SPWorkflowAssociation)

Weitere Ressourcen

Introduction to Workflows in Windows SharePoint Services