Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SharePoint > SharePoint Development
|
SharePoint Development Programming and development for SharePoint Portal Server and Windows SharePoint Services
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SharePoint Development section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 20th, 2014, 11:00 PM
Registered User
 
Join Date: Jan 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How do I trigger a workflow, programmatically?

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?
Code:
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
Code:
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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Time Off Workflow ChuckCTG BOOK: Workflow in SharePoint 2010: Real World Business Workflow Solutions 0 April 17th, 2012 10:54 AM
charpter 16 KB Article Review & Approve workflow can't be found when create workflow basten BOOK: Professional Alfresco: Practical Solutions for Enterprise Content Management 0 July 14th, 2011 10:32 PM
WorkFlow on cgv ZeroFactorial BOOK: ASP.NET 3.5 Enterprise Application Development with Visual Studio 2008: Problem Design Solutio 4 October 20th, 2009 04:21 AM
How to do workflow in Eclipse alokgupta14 Java Basics 0 December 14th, 2006 03:16 AM
Workflow bmains ASP.NET 1.x and 2.0 Application Design 2 May 15th, 2004 10:14 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.