i got a question regarding sharepoint workflows and event receivers. i got an event receiver that is setting metadata on an element. after that, i use a workflow to copy item metadata to a list. unfortunately the workflow does not copy the metadata set by the event receiver. i think because it is executed before the event receiver. is there a possibility to change the order, so that the workflow will execute after the event receiver? the receiver ist bound to the ItemAdded and ItemUpdated Events i a syncrounous manner.
Thank you for your help! Patrick
You can use SPWorkFlowAssociation to run workflow that associate with List or Content Type . Example ( run workflow after adding item)
public override void ItemAdded(SPItemEventProperties properties)
{
SPList parentList = properties.ListItem.ParentList;
SPWorkflowAssociation associationTemplate =
parentList.WorkflowAssociations.GetAssociationByName("Your Workflow Name",
new CultureInfo
(Convert.ToInt32(parentList.ParentWeb.RegionalSettings.LocaleId)));
SPSite siteCollection = properties.ListItem.ParentList.ParentWeb.Site;
siteCollection.WorkflowManager.StartWorkflow(properties.ListItem,
associationTemplate, String.Empty);
}
More information about SPWorkflowAssociation Check the below link
http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.workflow.spworkflowassociation.aspx
SPListItem:
The "Synchronous" events (-ing ending like ItemAdd*ing*), are always executed before the workflow.
The "Asynchronous" events (-ed ending like ItemAdd*ed*), are always executed after the execution of the workflow.
So, you have to set the "Synchronization" property of the Elements.xml file equal to "Synchronous" and the workflow will always be executed after the event receiver.
ATTENTION: Events Added and Updated run asynchronously by default, so you have to do the change in the Elements.xml .
来源:https://stackoverflow.com/questions/4483446/invoke-sharepoint-workflow-after-eventreceiver