0 out of 2 rated this helpful - Rate this topic

CreateTaskWithContentType Class

Windows SharePoint Services 3
Creates a task item in a Windows SharePoint Services 3.0 task list, using a specified Windows SharePoint Services 3.0 content type.

Namespace: Microsoft.SharePoint.WorkflowActions
Assembly: Microsoft.SharePoint.WorkflowActions (in microsoft.sharepoint.workflowactions.dll)
[ActivityToolboxDisplayAttribute("VSTTSharePointWorkflowTask", true)] 
[ToolboxItemAttribute(typeof(ActivityToolboxItem))] 
[ToolboxBitmapAttribute(typeof(CreateTaskWithContentType), "Resources.CreateTaskWithContentType.bmp")] 
public sealed class CreateTaskWithContentType : CallExternalMethodActivity

You must ensure that content types are enabled on the Windows SharePoint Services 3.0 task list where you are creating the task list items. If they are not enabled on this list, the activity will throw a null reference exception.

ms414383.42184b47-6107-4882-85ec-aac3dd7921a4(en-us,office.12).gif

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;
            }
        }
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
CreateTaskWithContentType Class

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)