Blog - Robert Bogue [MVP]
Rob's Notebook
SharePoint Calendar

Categories

Links

Archives

Other Blogs

Thor Projects LLC - Welcome : Blog - Robert Bogue [MVP]

A few things you should know about CreateTaskWithContentType in SharePoint Workflow

Posted by Robert Bogue on Wednesday, 24 Jan 2007 01:58 | 9 Comments | Professional

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.

Comments

Sunday, 28 Dec 2008 11:22 by Paul Turner
You must call taskList.ParentWeb.Dispose(); or you will leak an SPWeb.
Tuesday, 13 Jan 2009 02:29 by Simon
This solution doesnt work. I have still workflow error occured.
Friday, 10 Apr 2009 04:24 by Jeroen Van Bastelaere
@Paul> No he doesn't have to do that here, the SPList is handed down from another function and if you would dispose here you could end up killing the context (or smthing else, but still making the following code that uses that SPWeb crash) @Simon> This works fine for me, you might still have another problem
Thursday, 1 Oct 2009 02:31 by Eduardo
Tanks Robert, your code help me a lot! Eduardo Leite, São Paulo, Brasil
Tuesday, 11 May 2010 04:40 by Felipe Vale
It`s ok, there`s no errors in the code. But I`m still getting errors in the workflow execution at the creation of the taskWithContentType.
Tuesday, 22 Jun 2010 08:43 by Steve
Thanks for this useful tip Where is the best place to call this code ? I would say in the association page, but what about if I don't have a specific association page? steve
Tuesday, 13 Jul 2010 03:13 by ZooZ
Yeah, me too! I get an error at the CreateTaskWithContentType step :(
Tuesday, 2 Nov 2010 08:19 by Wesley
Thanks! Solved my three day journey down to the SPOETaskService(or somthing like that..) I HATE it when error messages suck!
Thursday, 21 Jun 2012 02:49 by Radheshyam Mali
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); ERRO-----get a null reference exception ---------- taskList.Update(); } } catch { throw; } } Can u help me anybody? Thank you very much.

Leave your own comment

Name

Url

Email

Comments