SharePoint - Can't start workflow from aspx page - wss3.0 workflow

Asked By sudhaoncyberworl on 03-Apr-07 09:41 AM
I have developed & deployed a workflow using vs2005. For the
initiation form i'm using the ASPX forms (Not Infopath)

After all setup, the initiation form is shown in the SP site when i
start the workflow. But when submitting the Initiation form i get the
error.


below is my code


Guid listID =3D new Guid(Request.QueryString["List"]);
int itemID =3D Convert.ToInt32(Request.QueryString["ID"]);
SPSite ospSite =3D new SPSite(SPContext.Current.Site.Url);
SPWeb ospWeb =3D ospSite.OpenWeb();
ospSite.AllowUnsafeUpdates =3D true;
ospWeb.AllowUnsafeUpdates =3D true;
SPList ospTaskList =3D ospWeb.Lists["Tasks"];
SPList ospHistoryList =3D ospWeb.Lists["Workflow History"];
SPListItem ospListItem =3D
ospWeb.Lists[listID].Items.GetItemById(itemID);
SPWorkflowTemplateCollection templateColl =3D
ospSite.WorkflowManager.GetWorkflowTemplatesByCategory(ospWeb, null);
SPWorkflowTemplate ospwfTemplate =3D
templateColl.GetTemplateByName("OBSWF",
System.Globalization.CultureInfo.CurrentCulture);
SPWorkflowAssociation spwfAssociation =3D
SPWorkflowAssociation.CreateListAssociation(ospwfTemplate,
ospwfTemplate.Name, ospTaskList, ospHistoryList);
ospSite.WorkflowManager.StartWorkflow(ospListItem,
spwfAssociation,"Organice Batch");


I get the below error


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D
btnSubmit_Click ::System.ArgumentNullException: Value cannot be null.
at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflowElev(SPListIte=
=AD
m
item, SPFile file, SPWorkflowAssociation association, SPWorkflowEvent
startEvent, Boolean bAutoStart, Boolean bCreateOnly)
at
Microsoft.SharePoint.Workflow.SPWorkflowManager.<>c__DisplayClass1.<StartWo=
=AD
rkflow>b__0()
at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object
state)
at
Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileg=
=AD
es>b__2()
at
Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevat=
=AD
ed
secureCode)
at
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback
secureCode, Object param)
at
Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated
secureCode)
at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem
item, SPWorkflowAssociation association, SPWorkflowEvent startEvent,
Boolean bAutoStart, Boolean bCreateOnly)
at
Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem
item, SPWorkflowAssociation association, String eventData, Boolean
isAutoStart)
at _Default.btnSubmit_Click(Object sender, EventArgs e)


Note : When i start the Workflow, i get the TemplateID in the Url
which is different from the ID that mentioned in my Workflow.xml. If
this
is the problem then what is the solution for this? If not please let
me know what is wrong?


Thanks


Prem kumar replied on 17-Mar-09 01:53 AM
Hi



I was doing a similar work of starting a workflow during 2009 March and faced the same problem. While searching google, I found this post and followed the code posted by you(Thanks to your code)but got the same error. Then I managed to startworkflow as follows.



Normally to start a workflow through SPList, we go to List settings -> Workflow settings and then we add a Workflow association to the List using SharePoints page. The same way we have to follow while starting the workflow through code also. I am just altering the code which you have written so that somebody might get benefit out of this code. Please find my changes tagged with comment line //My changes



==========Changed code==================

Guid listID = new Guid(Request.QueryString["List"]);

int itemID = Convert.ToInt32(Request.QueryString["ID"]);

SPSite ospSite = new SPSite(SPContext.Current.Site.Url);

SPWeb ospWeb = ospSite.OpenWeb();

ospSite.AllowUnsafeUpdates = true;

ospWeb.AllowUnsafeUpdates = true;

SPList ospTaskList = ospWeb.Lists["Tasks"];

SPList ospHistoryList = ospWeb.Lists["Workflow History"];



//My changes

SPList lst = ospWeb.Lists[listId];

SPListItem ospListItem = lst.Items.GetItemById(itemID);

//My changes



SPWorkflowTemplateCollection templateColl =

ospSite.WorkflowManager.GetWorkflowTemplatesByCategory(ospWeb, null);



SPWorkflowTemplate ospwfTemplate =

templateColl.GetTemplateByName("OBSWF",

System.Globalization.CultureInfo.CurrentCulture);



//My changes

//Before creating ListAssociation, we need to look whether that list has an association already for your

//workflow template. If so take that association and start your workflow using that

SPWorkflowAssociationCollection associations = lst.WorkflowAssociations;

//Routine to get already available workflow association for list

string associationName = "MyWorkflow"; //Assuming that this is the association name created using Sharepoint

//workflow settings page for list

SPWorkflowAssociation spwfAssociation = null

foreach (SPWorkflowAssociation association in associations)

{

if (string.Compare(association.Name, associationName, true) == 0)

{

spwfAssociation = association;

break;

}

}



//If there is no association namely "MyWorkflow", create a new one and add to list associations

if (spwfAssociation == null)

{

spwfAssociation = SPWorkflowAssociation.CreateListAssociation(ospwfTemplate,

associationName , ospTaskList, ospHistoryList);

lst.AllowManual = false;

lst.AddWorkflowAssociation(spwfAssociation);

}

//My changes



ospSite.WorkflowManager.StartWorkflow(ospListItem,

spwfAssociation,spwfAssociation.AssociationData);



==========Changed code==================



I implemented similar code and my workflow started well.



Thanks.