I am trying to get an Event Receiver to set the "Name" fields of files in a library to match the data in the "title" column. My ItemAdded code currently does that. However there is a SP Designer workflow currently on the library that creates the "title" field once the document hits the library. I am now trying to trigger the workflow from within my Event Receiver...that way the workflow can run before the Event Receiver code which will ensure that the "Name" field is getting the accurate (updated) information. Right now, my code calls the method that am trying to use to trigger the workflow, but am not able to step through that block cause it's errors out. Is this even the right approach?
public override void ItemAdded(SPItemEventProperties properties) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(properties.SiteId)) { using (SPWeb currentWeb = site.OpenWeb(properties.Web.ID)) { EventFiringEnabled = false; SPListItem item = properties.ListItem; TriggerWorkflow(item); if (item != null && item.File != null) { SPFile file = item.File; if (file.Exists) { string fileTitle = file.Title; file.MoveTo(item.ParentList.RootFolder.Url +"/" + item["Title"]); file.Update(); } } } } }); } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("PLANT Event Receiver", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace); } finally { this.EventFiringEnabled = true; } } public void TriggerWorkflow(SPListItem myItem) { try { SPSecurity.RunWithElevatedPrivileges(delegate() { if (myItem.ParentList.TemplateFeatureId == new Guid("d9a79fab-ac2c-40ef-962e-7ecd96e70ac4")) { SPWorkflowManager wfManager = myItem.ParentList.ParentWeb.Site.WorkflowManager; SPWorkflowAssociationCollection wfAssociationCollection = myItem.ParentList.WorkflowAssociations; foreach (SPWorkflowAssociation wfAssociation in wfAssociationCollection) { //if the current Workflow Association is the one of our workflow if (wfAssociation.BaseId == new Guid("d9a79fab-ac2c-40ef-962e-7ecd96e70ac4")) //corresponds to the WF Guid noted earlier { wfManager.StartWorkflow(myItem, wfAssociation, wfAssociation.AssociationData, true); break; //if we found it, there is not need to keep looping } } } }); } catch (Exception ex) { SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("PLANT Event Receiver", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace); } finally { } }Right now, when i step through my code, it errors out at
if (myItem.ParentList.TemplateFeatureId == new Guid("d9a79fab-ac2c-40ef-962e-7ecd96e70ac4"))I don't get any exception returned, so am clueless on what is causing it. Does anyone have an idea why this is not working? I really appreciate the help.