I have a SP 2010 state machine Site workflow with lots of custom InfoPath forms.

In some cases I need the user that's interacting with the workflow (many users can interact with the Workflow, not just the originator).

In the InfoPath forms I can use the user profile service to get a hold on the user. But how about in my site workflow code (i.e. my class deriving from StateMachineWorkflowActivity)?

Is the user profile service an option for this also or is there some API call I can make?


NOTE: This solution doesn't work for Site workflows

You can use this code snippet (Get current user in workflow sharepoint context (C#))

public static SPUser GetCurrentUserInWorkflow(Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties)
   string ModifiedbyUserName = Convert.ToString(workflowProperties.Item.GetFormattedValue("Modified By"));
   string[] strChar = { "ID=" };
   string[] strChars = ModifiedbyUserName.Split(strChar, StringSplitOptions.None);
   Int32 iUserID = Convert.ToInt32(strChars[1].Substring(0, strChars[1].IndexOf("\"")));
   SPUserCollection usersInSite = workflowProperties.Web.SiteUsers;
   int countUsers = usersI nSite.Count;
   for (int iCnt = 0; iCnt <= countUsers - 1; iCnt++)
     if (usersInSite[iCnt].ID == iUserID)
       return workflowProperties.Web.SiteUsers[iCnt];                    
   return null;

Basically you are checking last user who modified workflow item. However this will not work if you need to find current user on workflow start but then you can use:


You can find more detailed explanation here: How to get currently logged-in user in workflow

You only need to call user profile service if you require some additional user info for your workflow (eg. Department, Work Phone...)

|improve this answer|||||
  • Thanks for the answer but I'm not sure it will work in my case. As I've said, I have a site workflow and this code workflowProperties.Item seems to work with a list workflow. How about the current user on a site workflow? – JohnDoDo Jan 11 '12 at 8:27
  • Sorry. I obviously missed that site word :( I guess you can use somewhat similar code to check last editor of workflow task or something else that was changed in particular state or maybe history. I will try to explore this issue... – Vedran Rasol Jan 11 '12 at 9:11
  • If you are using infopath forms and you have your user there you can pass this value to your code like explained here msdn.microsoft.com/en-us/library/ms566880.aspx. If you are using workflow tasks you can then use lastedited method on that task. – Vedran Rasol Jan 11 '12 at 10:53
  • It is my understanding that association and initiation form data is used when the workflow starts, not for further interactions. The extended properties and ItemMetadata are used for the exchange tasks to workflow and vice-versa. I'm currently working on such a communication and see how that goes. – JohnDoDo Jan 11 '12 at 16:06

I've managed to make this work by using the InfoPath userName() function then passing the value back to my workflow with the extended properties of the task the opened form is associated to.

Not sure if this is the best (only?) way, but it works.

Just one thing to be aware of. Some fields in the external properties are GUIDs so you might need the SPBuiltInFieldId class.

EDIT: In case you are looking for a solution for List workflow, Vedran Rasol's answer provides details in that direction.

|improve this answer|||||
  • You should mark this as answer. – Vedran Rasol Jan 11 '12 at 20:16
  • @Vedran Rasol: I can accept my own answer only after 2 days. I will then. P.S. I wish you wouldn't have deleted your answer. Although it didn't work for site workflow it was an option for list workflows. Maybe you could add it as a comment to the question or something?! – JohnDoDo Jan 12 '12 at 8:50
  • as requested my answer is back :) – Vedran Rasol Jan 12 '12 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.