CreateTaskWithContentType Class
Assembly: Microsoft.SharePoint.WorkflowActions (in microsoft.sharepoint.workflowactions.dll)
|
The following code sample was contributed by Robert Bogue, one of the Windows SharePoint Services 3.0 MVPs. Get to know your Windows SharePoint Services 3.0 MVPs on this Web site. |
The following code sample updates a content type.
public static void VerifyModifyTaskList(SPList taskList, string contentType) { try { SPContentTypeId contentTypeId = new SPContentTypeId(contentType); taskList.ContentTypesEnabled = true; SPContentTypeId matchContentTypeId = taskList.ContentTypes.BestMatch(contentTypeId); if (matchContentTypeId.Parent.CompareTo(contentTypeId) != 0) { SPContentType ct = taskList.ParentWeb.AvailableContentTypes[contentTypeId]; taskList.ContentTypes.Add(ct); taskList.Update(); } } catch { throw; } }
System.Workflow.ComponentModel.DependencyObject
System.Workflow.ComponentModel.Activity
System.Workflow.Activities.CallExternalMethodActivity
Microsoft.SharePoint.WorkflowActions.CreateTaskWithContentType
Description
This class represents a workflow activity that creates a task in a SharePoint list with a particular content type. As such it is similar to the Microsoft.SharePoint.WorkflowActions.CreateTask class but permits you to control the content type of the task.
Usage Scenarios
This class should be used in custom workflows that will run in the context of a SharePoint site. The class can be created and configured in XAML code or managed code. You can associate your custom workflow with a content type or a SharePoint list. Use the CreateTaskWithContentType class when you want your workflow to add a new task with a specific content type. You can use the relevant SPList object to lookup the content types that are permitted in a list.
The following code examples show how to, within a SharePoint custom workflow, create a CreateTaskWithContentType activity and add it to the workflow.
Note: If you want your workflow to respond to a user altering the task created use the OnTaskChanged class. Set the CorrelationToken for both activities to bind them to the same task.
C# Code Example
//Get the first content type in the list.
using(SPSite SiteCollection = new SPSite("http://my_site/"))
{
SPWeb myWebsite = SiteCollection.OpenWeb("MyWebSite");
SPList TaskList = myWebsite.Lists["TaskList"];
SPContentTypeCollection ContentTypes = TaskList.ContentTypes;
SPContentType myContentType = ContentTypes.Item[0];
}
//Set the properties of the task you are creating
SPWorkflowTaskProperties myTaskProperties = new SPWorkflowTaskProperties();
myTaskProperties.Title = "This is a task with a specific content type";
myTaskProperties.Description = "Created by the CreateTaskWithContentType activity";//Create the activity and set its properties
CreateTaskWithContentType CreateMyTask = new CreateTaskWithContentType();
CreateMyTask.InterfaceType = GetType(ITaskService);
CreateMyTask.Name = "CreateMyTask";
CreateMyTask.ContentTypeId = myContentType.Id.ToString();
CreateMyTask.TaskProperties = myTaskProperties;//Add the activity to the workflow
this.Activities.Add(CreateMyTask);Visual Basic.NET Code Example
'Get the first content type in the list.
Dim SiteCollection As SPSite = New SPSite("http://my_site/")
Dim myWebsite As SPWeb = SiteCollection.OpenWeb("MyWebSite")
Dim TaskList As SPList = myWebsite.Lists("TaskList")
Dim ContentTypes As SPContentTypeCollection = TaskList.ContentTypes
Dim myContentType As SPContentType = ContentTypes.Item(0)
SiteCollection.Dispose()'Set the properties of the task you are creating
Dim myTaskProperties SPWorkflowTaskProperties = New SPWorkflowTaskProperties()
myTaskProperties.Title = "This is a task with a specific content type"
myTaskProperties.Description = "Created by the CreateTaskWithContentType activity"'Create the activity and set its properties
Dim CreateMyTask As CreateTaskWithContentType = New CreateTaskWithContentType()
CreateMyTask.InterfaceType = GetType(ITaskService)
CreateMyTask.Name = "CreateMyTask"CreateMyTask.TaskProperties = myTaskProperties'Add the activity to the workflow
Me.Activities.Add(CreateMyTask)
- 4/3/2008
- Content Master Ltd
- 5/2/2008
- Thomas Lee