Showing posts with label workflow error. Show all posts
Showing posts with label workflow error. Show all posts

Wednesday, November 11, 2009

Change Workflow Task Status from any SharePoint custom component

Scenario: I have a custom workflow running. I want to change status a task from another sharepoint custom component like webpart or so.

Approach:
If we update the workflow task directly by changing the status field value to Completed or to any other status, then usually workflow raises an exception "This task is currently locked by a running workflow and cannot be edited." To get rid of this error, you can use following routine:


private void CompleteTask(string taskTitle)
{
try
{
Hashtable taskHash = new Hashtable();
taskHash[SPBuiltInFieldId.TaskStatus] = "Completed";

string currentuser = SPContext.Current.Web.CurrentUser.Name.ToLower();
Guid siteID = SPContext.Current.Site.ID;
Guid webID = SPContext.Current.Web.ID;

SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteID))
{
using (SPWeb web = site.OpenWeb(webID))
{
web.AllowUnsafeUpdates = true;
SPList wfTasksList = web.Lists["WorkflowTasks"];
SPQuery queryWFTasks = new SPQuery();
queryWFTasks.Query = <<>>;
queryWFTasks.ViewAttributes = "Scope=\"RecursiveAll\"";

SPListItemCollection wfTaskItems = wfTasksList.GetItems(queryWFTasks);

if (wfTaskItems.Count != 0)
{
foreach (SPListItem item in wfTaskItems)
{
if (item.Title.ToLower().Equals(taskTitle.ToLower()))
{
SPWorkflowTask.AlterTask(item, taskHash, true);
break;
}
}
}

web.AllowUnsafeUpdates = false;
}
}
});
}
catch (Exception ex)
{
// Log the exception here
}
}

Sunday, September 13, 2009

SharePoint Custom Workflow Error (related to Serialization): WinWF Internal Error, terminating workflow

I created a custom workflow using VS 2008 for MOSS 2007.
In my SP site, I created a custom list CONTACTS with following columns:
Region - Single Line Text
Dept1 - Person Group
Dept2 - Person Group
Dept3 - Person Group

In this workflow, I need to retrieve users from CONTACTS list and create tasks for the users included within the groups associated to Dept columns.
I created custom workflow and ran it by associating to another list.
I got following error:
"WinWF Internal Error, terminating workflow Id# d4a5785f-22bf-42e8-bfca-d561bdb44ed7 System.Workflow.Runtime.Hosting.PersistenceException: Type 'Microsoft.SharePoint.SPUserCollection' in Assembly 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable. ---> System.Runtime.Serialization.SerializationException: Type 'Microsoft.SharePoint.SPUserCollection' in Assembly 'Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' is not marked as serializable. at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() at System.Runtime.Seri... ...alization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter) at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) at System.Workflow.ComponentModel.Activity.Save(Stream stream, IFormatter formatter) at System.Workflow.Runtime.Hosting.WorkflowPers... Workflow Infrastructure 98d4 Unexpected ...istenceService.GetDefaultSerializedForm(Activity activity) at Microsoft.SharePoint.Workflow.SPWinOePersistenceService.SaveWorkflowInstanceState(Activity instanceState, Boolean unlock) at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation) --- End of inner exception stack trace --- at System.Workflow.Runtime.WorkflowExecutor.Persist(Activity dynamicActivity, Boolean unlock, Boolean needsCompensation) at System.Workflow.Runtime.WorkflowExecutor.ProtectedPersist(Boolean unlock)"

What I understood from above exception in the SharePoint Logs:
The exception raised by WF is because of SPUserCollection object is not marked as serializable.
I made following changes to my code:
a. Marked my custom workflow class as serializable as all the objects within WF must be serializable. I did this by placing the attribute on top of the class as below:
[Serializable()]
public partial class SampleWorkflow : SequentialWorkflowActivity
{
}
b. Removed declaration of SPUserCollection objects as these obj are not serializable. To store user information for each department, i created separate hashtable and stored the data while retrieving the user information, instead of storing SPUser or SPUserCollection objects.

Reference Blog Posts, which helped me in solving this problem:
http://social.msdn.microsoft.com/Forums/en-US/sharepointworkflow/thread/01cde268-5148-4ff7-847e-1d03803c91c2
http://social.msdn.microsoft.com/Forums/en-US/sharepointworkflow/thread/9bd91cba-558c-456a-8f3c-e0e829145ec6/
http://blogs.msdn.com/sharepoint/archive/2006/11/28/developing-workflows-in-vs-part-5-code-your-workflow.aspx