Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > SharePoint > SharePoint Development
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 20th, 2014, 10: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.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02:16 AM
Workflow bmains ASP.NET 1.x and 2.0 Application Design 2 May 15th, 2004 10:14 PM



All times are GMT -4. The time now is 08:15 AM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.