Skip to content

January 24, 2007

A few things you should know about CreateTaskWithContentType in SharePoint Workflow

Here are a few things that you should know when working with the CreateTaskWithContentType activity in a SharePoint workflow.

1)      If you don’t enable ContentTypes in the task list you’re get a null reference exception (from the activity itself, not your code.)

2)      The ContentType that you specify in the ContentType property isn’t automatically associated.  You’ll want to verify the content type exists in the _MethodInvoking() event.  You could call the code below to automatically enable content types and if necessary add the contentType:

/// <summary>

/// Verify — and if necessary modify — the task list to ensure that the required fields are available.

/// </summary>

/// <param name=”taskList”>The list to be verified</param>

/// <param name=”contentType”>The content type id to add</param>

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;

}

}

3)      You can’t test for whether a content type is associated with a list or not by its “short” identifier directly.  You have to use the .BestMatch method as shown above to determine whether the content type is associated or not.

Turning a GET into a POST for SPWorkflowManager.StartWorkflow()

I mentioned in a previous post that you must run StartWorkflow on a POST request and not a GET request but I didn’t have time then to figure out how to do this quickly in a web part…  Here’s how to do it…  In any WebControl (or anything that can be to the Page object) add the following line.

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), “post”, “<SCRIPT Language=\”JavaScript\”>\n<!–\ndocument.forms[0].submit();\n–>\n</SCRIPT> “, false);

This will cause the page to post back to itself once it’s loaded.  It’s a kluge, but it solves the issue

Recent Posts

Public Speaking