1

I am creating a workflow task the listItem in sharepoint 2013. The workflow works fine when I am adding the list item the task is created and there is no error. However when I am updating the item the workflow code gives me this error:

<nativehr>0x8102009b</nativehr><nativestack></nativestack>

StackTrace

   at HBLEventReceiverWP.ApprovalWFRecevier.ApprovalWFRecevier.LookupAndStartWorkflow(SPListItem listItem, SPWorkflowManager manager, SPWorkflowAssociationCollection associationCollection, String workflowId)
   at HBLEventReceiverWP.ApprovalWFRecevier.ApprovalWFRecevier.ItemUpdated(SPItemEventProperties properties)
   at Microsoft.SharePoint.SPEventManager.RunItemEventReceiver(SPItemEventReceiver receiver, SPUserCodeInfo userCodeInfo, SPItemEventProperties properties, SPEventContext context, String receiverData)
   at Microsoft.SharePoint.SPEventManager.RunItemEventReceiverHelper(Object receiver, SPUserCodeInfo userCodeInfo, Object properties, SPEventContext context, String receiverData)
   at Microsoft.SharePoint.SPEventManager.<>c__DisplayClassa`1.<InvokeEventReceiver>b__7()
   at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param)

This is my code on itemupdated Event.

 public class ApprovalWFRecevier : SPItemEventReceiver
    {
        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);
                // get site collection web 
            using (SPWeb spWeb = properties.OpenWeb())
            {
                // get current site 
                SPSite spSite = spWeb.Site;
                // 
                spWeb.AllowUnsafeUpdates = true;
                // check if it is a component list
                bool isCompLst = CheckIsComponentList(properties.List.ToString());
                if (isCompLst == true)
                {
                    // set the list item
                    SPListItem listItem = properties.ListItem;

                    // check if item is in preview mode 
                    if (properties.List.ToString() == "SitePages")
                    {
                        if (Convert.ToBoolean(listItem["Preview"]) == false)
                        {
                            SPWorkflowManager manager = spSite.WorkflowManager;
                            //get item's parent list
                            SPList parentList = listItem.ParentList;
                            //get all workflows that are associated with the list
                            SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
                            //lookup and start the worflow
                            LookupAndStartWorkflow(listItem, manager, associationCollection, "Approval");
                        }

                    }
                    else if (CheckPreviewCol(listItem, properties) == false)
                    {
                        //obtain an instance of SPWorkflowManager 
                        //which will be later used to start the workflow
                        SPWorkflowManager manager = spSite.WorkflowManager;
                        //get item's parent list
                        SPList parentList = listItem.ParentList;
                        //get all workflows that are associated with the list
                        SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
                        //lookup and start the worflow
                        LookupAndStartWorkflow(listItem, manager, associationCollection, "Approval");

                    }
                }
            }



        }

        #region Methods

        public bool CheckIsComponentList(string listName)
        {
            try
            {
                bool IsCompLst = false;

                switch (listName)
                {
                    case "HeadingComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "ParagraphComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "FAQComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "FAQComponentDetail":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "BulletComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "BulletComponentDetail":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "ButtonsComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "TwoImageComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "ImageGridComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "ImageWithDescriptionComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "GreenTextComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "TableComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "TableComponentRows":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "CenterComponent":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    case "CenterComponentDetail":
                        // set component list to true
                        IsCompLst = true;
                        break;
                    case "SitePages":
                        // set component list to true
                        IsCompLst = true;
                        break;

                    default:
                        // set component list to true
                        IsCompLst = false;
                        break;
                }
                return IsCompLst;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public bool CheckPreviewCol(SPListItem listItem, SPItemEventProperties properties)
        {
            try
            {
                bool isPreview = true;

                bool oldValue = listItem["Preview"] != null ? Convert.ToBoolean(listItem["Preview"]) : true;
                if (oldValue == true)
                {
                    bool newValue = properties.AfterProperties["Preview"] != null ? Convert.ToBoolean(properties.AfterProperties["Preview"]) : true;

                    if (oldValue != newValue)
                        isPreview = false;
                }
                else
                    isPreview = false;

                return isPreview;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private static void LookupAndStartWorkflow(SPListItem listItem, SPWorkflowManager manager, SPWorkflowAssociationCollection associationCollection, string workflowId)
        {
            try
            {

                //iterate workflow associations and lookup the workflow to be started
                foreach (SPWorkflowAssociation association in associationCollection)
                {
                    //if the workflow association matches the workflow we are looking for,
                    //get its association data and start the workflow
                    if (association.Name.ToLower().Equals(workflowId.ToLower()))
                    {
                        //get workflow association data
                        string data = association.AssociationData;

                        //start workflow with privileges
                        SPSecurity.RunWithElevatedPrivileges(delegate()
                        {
                        SPWorkflow wf = manager.StartWorkflow(listItem, association, data, true);
                        });
                    }
                }
            }
            catch (Exception ex) 
            {
                throw ex;
            }
        }

        #endregion

    }
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

This error might mean that there already is an active workflow for this specific Item. the solution in your case would be to check whether the workflow is completed. That would be something like:

if(workflow.IsCompleted)
{
  //you can start it
  //this is pseudocode do not rely on it
}

For further information check this source.

チーズパン
  • 2,752
  • 8
  • 42
  • 63