Get the item a SharePoint workflow task is associated with

This is handy. SharePoint helpfully populates the meta data with the GUID of the list and the ID of the item a WF instance is associated with. These are stored in “ows_WorkflowListId” and “ows_WorkflowItemId” fields on the task list item.

 An example of using this is from an InfoPath form in the form load code behind.

SPListItem currentListItem = SPContext.Current.ListItem; if (currentListItem != null) {   object associatedWfListId = currentListItem[“ows_WorkflowListId”];   object associatedWfItemId = currentListItem[“ows_WorkflowItemId”];     if (associatedWfItemId != null && associatedWfListId != null)   {     SPListItem item = web.Lists.GetList(new Guid(associatedWfListId.ToString()), false).GetItemById(int.Parse(associatedWfItemId.ToString()));     // THE ABOVE ITEM IS THE ASSOCIATED LIST ITEM   } }

Sharepoint and TaskListContentTypeId with mixing Office workflow tasks with regular sharepoint workflow tasks. Part 2

The reason the Office Sharepoint Server Workflow Task is not visible is because it is assigned to a special group named “_Hidden” which precludes it from being displayed in the site collection content type view.

As it turns out content types are exposed and manipulable through the Sharepoint OM using the class SPContentType. You can get a list of content types from a SPWeb by invoking the getter on property ContentTypes:

SPSite site = new SPSite(“http://splaptop”); using (SPWeb web = site.OpenWeb()) {   foreach (SPContentType contentType in web.ContentTypes)   {     if (contentType.Name == “Office SharePoint Server Workflow Task”)     {       /*        * make a member of whatever group you like…        * nb. if group is == “_Hidden” (default) then it will not show up        * in the list of content types        */       contentType.Group = “MOSS”;       contentType.Update();     }   } } 
The content type will now be visible when you try to assign it to a task list.

 

Sharepoint and TaskListContentTypeId with mixing Office workflow tasks with regular sharepoint workflow tasks.

We have a workflow that is a mix of simple tasks and MOSS infopath forms server workflow tasks.

To that end we remove the TaskListContentTypeId from workflow.xml and use the CreateTaskWithContentType shape to manually set the content type on appropriate tasks. This works fine until we come to deploy on a fresh sharepoint site.

We get some kind of error in the sharepoint logs:

10/15/2007 15:29:09.76  w3wp.exe (0x145C)                        0x144C Windows SharePoint Services    Workflow Infrastructure        72ew Medium   Object reference not set to an instance of an object.   10/15/2007 15:29:09.78  w3wp.exe (0x145C)                        0x144C Windows SharePoint Services    Workflow Infrastructure        88xr Unexpected WinWF Internal Error, terminating workflow Id# c345e730-f40d-4cb9-8197-f472143c8410   10/15/2007 15:29:09.78  w3wp.exe (0x145C)                        0x144C Windows SharePoint Services    Workflow Infrastructure        98d4 Unexpected System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.NullReferenceException: Object reference not set to an instance of an object.     at Microsoft.SharePoint.Workflow.SPWinOETaskService.CreateTaskWithContentTypeInternal(Guid taskId, SPWorkflowTaskProperties properties, Boolean useDefaultContentType, SPContentTypeId ctid, HybridDictionary specialPermissions)     at Microsoft.SharePoint.Workflow.SPWinOETaskService.CreateTaskWithContentType(Guid taskId, SPWorkflowTaskProperties properties, String taskContentTypeId, HybridDictionary specialPermissions)     — End of inner exception stack trace —     at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAtt…   10/15/2007 15:29:09.78* w3wp.exe (0x145C)                        0x144C Windows SharePoint Services    Workflow Infrastructure        98d4 Unexpected …ributes, RuntimeTypeHandle typeOwner)     at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)     at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)     at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)     at System.Workflow.Activities.CallExternalMethodActivity.Execute(Acti…   10/15/2007 15:29:09.78* w3wp.exe (0x145C)                        0x144C Windows SharePoint Services    Workflow Infrastructure        98d4 Unexpected …vityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(T activity, ActivityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutor`1.Execute(Activity activity, ActivityExecutionContext executionContext)     at System.Workflow.ComponentModel.ActivityExecutorOperation.Run(IWorkflowCoreRuntime workflowCoreRuntime)     at System.Workflow.Runtime.Scheduler.Run()

Which tells us a little, but not much.

So after a few :ahem: hours of digging around. It turns out that the problem manifests itself because when a tasklist for a wf instance is created by associating a workflow with a list – it sets up content types in the task list which sharepoint uses to marshal tasks. We weren’t seeing the problem because we only started using CreateTaskWithContentType activities half way through the development cycle.

We can see this by looking at the task list, going to “Settings”…”Advanced Settings”, and checking “Allow management of content types”. OK that and scroll down the summary screen and there *should* be a content type named “Office SharePoint Server Workflow Task”. If there is not you need to associate the content type with the task list and everything should be tickety boo. (you can do this quick and dirty by adding the content type id to the workflow.xml and recreating the task list when you assign the workflow).

If you click “Add from existing site content types” the type will not be visible. I will update when I find how you do this.