SharePoint 2013 Running workflow programmatically StartWorkflow error FAILED hr detected (hr = 0x8102008a)
Hello,
Just want to share something in regards to starting workflow using the Workflow Manager in code.
I have built a SPD reusable workflow for a content type.
I then add this workflow to my list in my site.
As I want to be able to trigger this workflow anonymously, I have to run the workflow using WorkflowManager instead of the standard auto start from UI (Otherwise the workflow will be created but will not run)
Originally I used the ItemAdded event receiver, then use this piece of code:
StartWorkflow function:
But my workflows only get triggered for some records (intermittently). When I run the debugging, I can see that a weird error is thrown:
An exception of type 'Microsoft.SharePoint.SPException' occurred in Microsoft.SharePoint.dll but was not handled in user code
FAILED hr detected (hr = 0x8102008a)
in the log : COMException: 0x8102008a
Looking around for solution I couldn't find the answer.
After some trials and errors, I noticed that my code doesn't throw that error when I call item.Update() before my StartWorkflow function.
However it sometimes throws Save Conflict Your changes conflict with those made concurrently by another user when I tested creating my list items in a quick manner.
I soon realised that it is because ItemAdded receiver is by default running asynchronously.
Since I also use my custom save functionality on my list item, I moved my code to be part of it. After the attachment functionality (described in my previous post), I just call my workflow.
Completed code:
If you still want to use ItemAdded receiver you should be able to make it work as well. Just make it run as a synchronous event rather than asynchronous.
Hope this helps,
Andreas
Just want to share something in regards to starting workflow using the Workflow Manager in code.
I have built a SPD reusable workflow for a content type.
I then add this workflow to my list in my site.
As I want to be able to trigger this workflow anonymously, I have to run the workflow using WorkflowManager instead of the standard auto start from UI (Otherwise the workflow will be created but will not run)
Originally I used the ItemAdded event receiver, then use this piece of code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | Guid siteID, webID, listID; int itemID; listID = properties.ListId; itemID = properties.ListItem.ID; using (SPWeb web = properties.OpenWeb()) { siteID = web.Site.ID; webID = web.ID; } SPSecurity.RunWithElevatedPrivileges( delegate () { using (SPSite site = new SPSite(siteId)) { using (SPWeb web = site.OpenWeb(webId)) { SPListItem item = web.GetListItem(itemID); web.AllowUnsafeUpdates = true ; StartWorkflow(item); web.AllowUnsafeUpdates = false ; } } }); |
StartWorkflow function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private void StartWorkflow(SPListItem listItem) { var workflowName = "My Workflow" ; try { SPWorkflowAssociation wfAssoc = listItem.ContentType.WorkflowAssociations.GetAssociationByName(workflowName, System.Globalization.CultureInfo.CurrentCulture); listItem.Web.Site.WorkflowManager.StartWorkflow(listItem, wfAssoc, wfAssoc.AssociationData); } catch (Exception ex) { //Log error } } |
But my workflows only get triggered for some records (intermittently). When I run the debugging, I can see that a weird error is thrown:
An exception of type 'Microsoft.SharePoint.SPException' occurred in Microsoft.SharePoint.dll but was not handled in user code
FAILED hr detected (hr = 0x8102008a)
in the log : COMException: 0x8102008a
Looking around for solution I couldn't find the answer.
After some trials and errors, I noticed that my code doesn't throw that error when I call item.Update() before my StartWorkflow function.
However it sometimes throws Save Conflict Your changes conflict with those made concurrently by another user when I tested creating my list items in a quick manner.
I soon realised that it is because ItemAdded receiver is by default running asynchronously.
Since I also use my custom save functionality on my list item, I moved my code to be part of it. After the attachment functionality (described in my previous post), I just call my workflow.
Completed code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | protected void FormContext_OnSave( object sender, EventArgs e) { Page.Validate(); if (Page.IsValid) { Guid siteId = SPContext.Current.Site.ID; Guid webId = SPContext.Current.Web.ID; SPContext.Current.ListItem.Update(); SPSecurity.RunWithElevatedPrivileges( delegate () { using (SPSite site = new SPSite(siteId)) { using (SPWeb web = site.OpenWeb(webId)) { SPListItem item = web.GetListItem(SPContext.Current.ListItemServerRelativeUrl); web.AllowUnsafeUpdates = true ; AddAttachments(item); StartWorkflow(item); web.AllowUnsafeUpdates = false ; } } }); //Redirect to certain page instead HttpContext.Current.Response.Redirect(redirectUrl); } } |
If you still want to use ItemAdded receiver you should be able to make it work as well. Just make it run as a synchronous event rather than asynchronous.
1 2 3 4 5 6 7 8 9 10 11 12 | < receivers ListTemplateId = "104" > < receiver > < name >EventReceiver1ItemAdded</ Name > < type >ItemAdded</ Type > < assembly >$SharePoint.Project.AssemblyFullName$</ Assembly > < class >Technet.EventReceiver1.EventReceiver1</ Class > < sequencenumber >10000</ SequenceNumber > < synchronization >Synchronous</ Synchronization > </ Receiver > </ Receivers > </ Elements > |
Hope this helps,
Andreas
Comments
Post a Comment