Hi, everybody! I encountered a extremely terrible problem. I created a workflow named Approve3 in Sharepoint Designer. The workflow executes well in Sharepoint Server 2007. But now I want to start it programmatically and the code are as follows:
this.myWeb.AllowUnsafeUpdates = true;
int itemId = Convert.ToInt32(Request.Params["itemId"]);
this.myItem = this.myList.GetItemById(itemId);
Guid WF_GUID = new Guid("642E456F-2C7C-49BD-9775-53AB4A0CE425");
this.myAssoc = this.myList.WorkflowAssociations.GetAssociationByBaseID(WF_GUID);
this.myAssoc.AssociationData = this.ddlDeaprtment.SelectedItem.Text;
this.wfManager.StartWorkflow(this.myItem, this.myAssoc, myAssoc.AssociationData);
The WF_GUID is the baseId of Approve3. I got the baseId in a Approve3.xoml.config.xml file. I write a initpage. There's a dropdownlist on that page. I need users select an item as the initiate data(Here I assign the data to myAssoc.AssociationData). But I can't start the workflow. When the StartWorkflow() executes, the page give a error message:
Operation is not valid due to the current state of the object.
In the list pages of Sharepoint, I can see that the status of Approve3 is failed to start.But after a while, status turns to be completed! I think there's some wrong with myAssoc.AssociationData, so I search the CodeSample in ECM Starter Kit. In the ASPXCollectFeedback Project, I find they write the AssociationData in a XML format. The code are sth like this:
private string getInitXmlString()
{
InitData data = new InitData();
data.Department = this.ddlDeaprtment.SelectedItem.Text;
using (MemoryStream stream = new MemoryStream())
{
XmlSerializer serializer = new XmlSerializer(typeof(InitData));
serializer.Serialize(stream, data);
stream.Position = 0;
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, bytes.Length);
return Encoding.UTF8.GetString(bytes);
}
}
InitData is a class with get and set method. But it still doesn't work! When I start the workflow, I get the same error message. I also tried another workflow without initdata. It didn't work,either.
At last, I created an approve workflow using Sharepoint Workflow Template in Sharepoint Server 2007. Using the similar code:
this.wfManager.StartWorkflow(this.myItem, this.myAssoc, myAssoc.AssociationData);
The workflow starts well without showing any initiate page(There should be a initiate page which allows users to select the appovers. The name of the page is IniWrkflIP.aspx).
I have stuck to this problem for several days. Who can help me? Thanks.
P.S. Excuse me for me poor English.