Content Organizer Event Reciever for Workflows
-
lunes, 23 de abril de 2012 21:19
Hello,
We have a document center site collection with the content organizer enabled. We would like to have a workflow that triggers on documents once they arrive in their target libraries.
To make this happen we created a workflow in SharePoint Designer 2010 which affects documents of a certain content type and specific metadata values.
This did not initially work, because of the content organizer taking over and mucking things up. We then added an event receiver which now allows the documents to go through the workflow. The problem is the original submitter of the document no longer gets emails announcing workflow start or completion. Obviously that means our fix is not really the best solution possible.
Does anyone know how to set it so that the workflow will start as the author/editor of the document, instead of "System Account"?
Source code for event receiver:
using System;using System.Security.Permissions; using Microsoft.SharePoint; using Microsoft.SharePoint.Security; using Microsoft.SharePoint.Utilities; using Microsoft.SharePoint.Workflow; namespace SPDWorkflowActivator.SPDWorkflowActivator { /// <summary> /// List Item Events /// </summary> public class SPDWorkflowActivator : SPItemEventReceiver { /// <summary> /// An item was added. /// </summary> public override void ItemAdded(SPItemEventProperties properties) { StartWorkflow(properties.ListItem); base.ItemAdded(properties); } /// <summary> /// An item was updated. /// </summary> public override void ItemUpdated(SPItemEventProperties properties) { StartWorkflow(properties.ListItem); base.ItemUpdated(properties); } private void StartWorkflow(SPListItem addedItem) { SPWorkflowManager manager = addedItem.ParentList.ParentWeb.Site.WorkflowManager; SPWorkflowAssociationCollection assoCollection = addedItem.ContentType.WorkflowAssociations; foreach (SPWorkflowAssociation asso in assoCollection) { manager.StartWorkflow(addedItem, asso, asso.AssociationData.); } } }}
Matt Olson, MS Content Development Specialist MCITP, MCTS
Todas las respuestas
-
miércoles, 25 de abril de 2012 14:06
Try replacing your code with the following code and see if that helps.
private void StartWorkflow(SPListItem addedItem) { String authorUser = addedItem["Author"].ToString(); int userID = Convert.ToInt32(authorUser.Substring(0, authorUser.IndexOf(";#"))); SPUser Author = addedItem.ParentList.ParentWeb.AllUsers.GetByID(userID); SPUserToken authorToken = Author.UserToken; using (SPSite site = new SPSite(addedItem.ParentList.ParentWebUrl, authorToken)) { SPWorkflowManager manager = site.WorkflowManager; SPWorkflowAssociationCollection assoCollection = addedItem.ContentType.WorkflowAssociations; foreach (SPWorkflowAssociation asso in assoCollection) { manager.StartWorkflow(addedItem, asso, asso.AssociationData.); } } }
Amit
-
viernes, 27 de abril de 2012 19:58
Vasu,
Has this worked for you in the past? I tried this and I still ended up with documents starting workflows as "System Account" instead of as the logged-in user.
- Matt
Matt Olson, MS Content Development Specialist MCITP, MCTS
-
lunes, 30 de abril de 2012 13:58
Hi Matt
I did not try the code when I provided you with the solution but after your comment I have tried that code myself and on the workflow status page I get the correct Initiator name (the one who is Author of the document).
To debug your situation can you try adding some log and see if you are getting correct user value for
String authorUser = addedItem["Author"].ToString();
If you are getting System account after this line then its not going to work as what this mean is your document's Created By value is System Account.
Can you please let me know your findings and I will try see if I could help resolve this issue.
Amit
-
lunes, 30 de abril de 2012 14:13
Amit,
No, I tried this, and it does not work.
Steps:
Upload a document to the drop-off library
Set up metadata that will trigger the content organizer rules
content ends up in library with workflow
Workflow starts but document submitter does not get email.
Workflow messages:
The following events have occurred in this workflow. <input class="s4-selectAllCbx" title="Select or deselect all items" type="checkbox" /> Date Occurred
Event Type
User ID
Description
Outcome
<input class="s4-itm-cbx" title="98" type="checkbox" /> 4/30/2012 9:07 AM Error
System
AccountThe workflow could not set content approval status. Enable content moderation for this list and run the workflow again. <input class="s4-itm-cbx" title="99" type="checkbox" /> 4/30/2012 9:07 AM Error
System
AccountThe e-mail message cannot be sent. Make sure the e-mail has a valid recipient. <input class="s4-itm-cbx" title="100" type="checkbox" /> 4/30/2012 9:07 AM Workflow Initiated
System
AccountSOW/Change Request document approval task was started. Participants: Approver <input class="s4-itm-cbx" title="101" type="checkbox" /> 4/30/2012 9:07 AM Task Created
System
AccountTask created for Approver. Due by: None Receiver source code:
I followed your suggestion and ran this in debug mode, too, and saw that the correct user was being picked up by the item's "Author" attribute.
Thanks,
- Matt
Matt Olson, MS Content Development Specialist MCITP, MCTS
- Editado M_Olson lunes, 30 de abril de 2012 14:14
-
lunes, 30 de abril de 2012 14:36
I think I know what is the problem.
In my initial code I made the mistake by using the SPListItem object which was created outside of impersonation. When I tried it on my local I was able to get it working because I created SPListItem object after impersonation.
Try changing the code to the following and see if that helps.
private void StartWorkflow(SPListItem addedItem) { String authorUser = addedItem["Author"].ToString(); int userID = Convert.ToInt32(authorUser.Substring(0, authorUser.IndexOf(";#"))); SPUser Author = addedItem.ParentList.ParentWeb.AllUsers.GetByID(userID); SPUserToken authorToken = Author.UserToken; using (SPSite site = new SPSite(addedItem.ParentList.ParentWebUrl, authorToken)) { using (SPWeb web = site.OpenWeb()) { SPList list = web.Lists["DROP OFF DOCUMENT LIBRARY"] SPListItem lstItem = list.GetItemById(addedItem.Id); SPWorkflowManager manager = site.WorkflowManager; SPWorkflowAssociationCollection assoCollection = lstItem.ContentType.WorkflowAssociations; foreach (SPWorkflowAssociation asso in assoCollection) { manager.StartWorkflow(lstItem, asso, asso.AssociationData.); } } } }
Amit
- Marcado como respuesta M_Olson lunes, 30 de abril de 2012 14:45
-
lunes, 30 de abril de 2012 14:46
Amit,
Thank you very much! That worked like a charm. Still having another workflow issue on this library but you have answered this question.
Thanks again,
- Matt Olson
Matt Olson, MS Content Development Specialist MCITP, MCTS

