Satishkumar

Hi all,

I want to start a particular workflow programatically based on the item that is edited in a list. For this I have created all the required workflows on the list using Sharepoint . I have written Eventhandler for ItemUpdating event, and in that I try to start my workflow. The following is the code i use inside the event handler.

public override void ItemUpdating(SPItemEventProperties properties)

{

SPList oHorizon = properties.OpenWeb().Lists["Horizon Scanning"];

SPWorkflowManager manager = properties.OpenWeb().Site.WorkflowManager;

SPWorkflowAssociation asso = oHorizon.WorkflowAssociations[0];

SPWorkflow workflow = manager.StartWorkflow(properties.ListItem, asso, asso.AssociationData);

}

The error i receive is

Exception from HRESULT: 0x8102009B

at Microsoft.SharePoint.Library.SPRequest.AddWorkflowToListItem(String bstrUrl, String bstrListName, Int32 lItemID, Int32 lItemLevel, Int32 lItemVersion, Guid workflowPackageId, Guid& pWorkflowInstanceId, Guid workflowTaskListId, String bstrStatusFieldInternalName, Int32 lAuthorId)
at Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflowElev(SPListItem item, SPFile file, SPWorkflowAssociation association, SPWorkflowEvent startEvent, Boolean bAutoStart, Boolean bCreateOnly)
at Microsoft.SharePoint.Workflow.SPWorkflowManager.<>c__DisplayClass1.<StartWorkflow>b__0()
at Microsoft.SharePoint.SPSecurity.CodeToRunElevatedWrapper(Object state)
at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated 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 Microsoft.SharePoint.Workflow.SPWorkflowManager.StartWorkflow(SPListItem item, SPWorkflowAssociation association, String eventData)
at HorizonScanningEvent.WorkflowStarter.ItemUpdating(SPItemEventProperties properties)

Please let me know if there is something wrong in my code/ approach.

Thanks.




Re: SharePoint - Workflow Starting a Approval workflow programatically

Charbel CHARBEL

You can try, instead of what is posted above, to set your workflow to start when an item is updated from the document library settings.

And you can access the item from inside the workflow by using the properties of the workflow and get the item that triggered the workflow.





Re: SharePoint - Workflow Starting a Approval workflow programatically

MarkAtDLC

The only question is whether you want to use ItemUpdating or ItemUpdated and it may not have anything to do with your exception. For ItemUpdating your workflow will not have access to the changed values of the item because the item hasn't yet been updated, so if you're workflow is relying on the updated values it will not function correctly.

More pertinently, does being in the state where ItemUpdating has been signalled lock the item Could this be related to the exception




Re: SharePoint - Workflow Starting a Approval workflow programatically

Satishkumar

Thanks for the replies.

But I can know which Workflow to start only based on the Item that is edited. That is why I am starting the workflow manually.

Let me give more details.

I have a column called category in my list. The approvers for the Item edited will be determined based on value of Category. I have created one workflow for each category with the appropriate Approvers. So my idea is to start the Workflow based on the Category of the Item, in the ItemUpdating event.

But after seeing ur reply, I am thinking if its possible to change the Approvers of a workflow programatically. In that case I only need one workflow and I can set it up to start from the document library settings. Please let me know, if I can change the Approvers of a workflow at runtme

Thanks.





Re: SharePoint - Workflow Starting a Approval workflow programatically

Satishkumar

Mark,

I tried using the ItemUpdated event, but I get the same error. I also explored the possibility of changing the Approvers list in a Workflow at runtime, but I doubt its possible using the SPWorkflow object.





Re: SharePoint - Workflow Starting a Approval workflow programatically

MarkAtDLC

Have you seen this link http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=1080034&SiteID=1

The stack traces look the same. The summary is that the association data passed to StartWorkflow needs to be well-formed and compliant with the schema that is expected.




Re: SharePoint - Workflow Starting a Approval workflow programatically

Satishkumar

Yes, I had seen that Link. But i am unable to find any documentation on what exactly has to be passed as the Event data. In an another thread, it was told that just passing the AssociationData of Association object will do the job. So i have done that. Also I get the same error, when I passed empty string as Eventdata.
As i am running out of time, now I have implemeted an alternate solution by splitting my list into multiple lists, one for each category and have attached a workflow for each list to start automatically using List Settings. And I use a content query web-part, to consolidate the data from all the lists for display.
Thanks for the help.




Re: SharePoint - Workflow Starting a Approval workflow programatically

Steve Wigren

I couldn't get the event data to work with an empty string or with AssociationData, but it worked by passing it some dummy XML - I used "<Data></Data>". So I guess it just wants some well-formed XML.



Re: SharePoint - Workflow Starting a Approval workflow programatically

RyanKirchner

I am trying to get this to work as weel, but I keep getting the following error:

System.Runtime.InteropServices.COMException: Exception from HRESULT: 0x8102009B

on line:

Dim myWorkflow As SPWorkflow = site.WorkflowManager.StartWorkflow(listItem, associationTemplate, "<Data></Data>")

Can you please provide the code you have written that makes this work Also, how is your workflow configured in Designer, is it set to start automatically, or not

Thanks.

Ryan





Re: SharePoint - Workflow Starting a Approval workflow programatically

Kiran Bellala

Satish,

You dont have to create multiple workflows because the approvers differ based on what the user edits. You can still have only one workflow.

In the OnWorkflowActivated activity, read the list item and determine the approvers. Your second activity in the workflow is CreateTask. So, in the OnworkflowActivated event handler, determine the approver and set the property of the task. (set Assigned To property of the task to the appropriate approver's name).

Hope this helps






Re: SharePoint - Workflow Starting a Approval workflow programatically

Niranjan Ameta

hi

i can start the work flow from work flow manager class.

SPWorkflow _spworkFlow = _spSite.WorkflowManager.StartWorkflow(.......)

All work flow code i am wrting through Work flow Object Model. i am not using any custom work flow.

how user will approve this task from object mode.i can open the form from task list and approve it but i want to approve it from single click on any form and then it should be assigned to next approver.

regards

Niranjan ameta






Re: SharePoint - Workflow Starting a Approval workflow programatically

MaxWeber

I think a different approach may give you better results. What about creating a facade workflow that will in turn, initiate the Approval workflow and configure it for the document in question I believe, as long as you know the schema of the Approval workflow, you can create an approvers list programmatically and have it just conform to the Approval workflow. My guess is that the approval workflow is based on an InfoPath-generated schema, or on some other XSD.






Re: SharePoint - Workflow Starting a Approval workflow programatically

Niranjan Ameta

hi

i have got the solution.

actually i developed the smart document and deployed to MOSS 2007.there is custom menu bar on smart document.

when user submit the smart doc then work flow should be stated. and task is assigned to next level user.next level user opens the smart document and approve the task from custom menu by cliking approve button

all funtions like approve , reject , reassign , re call will be called only from custom menu.

so i decided to do whole thing by work flow object mode.

what i m doing ...

first i select the approve work flow the and put title.

i dont decide the work flow start option .( new item or item change) so dont click on any button.

click next and on next page direct press OK with any approver.

wheneve user clicks on sumit on smart document i call to work flow manager class to start the work flow for the perticular sps list item.

now i have got the xml xcheman. i can approve , reject and modify work flow also.

only one thing which is not in MOSS work flow is recall the work flow.

Requestor who creates the smart document assign to different approver.

requestor can recall the smart document.

suppose a , b, c, are approver.

a nd b approved the smart document.

when requestot recall the smart documen then it should be delete from c task list and and status value for all approve task list is "recall".

thanks for your input

regards

Niranjan Ameta

MCTS ( share point)