Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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:

workflowProperties.OriginatorUser.ID
workflowProperties.OriginatorUser.Name
workflowProperties.OriginatorUser.ID.LoginName

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...)

share|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
add comment
up vote 3 down vote accepted

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.

share|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
    
@Vedran Rasol: Thanks and +1. –  JohnDoDo Jan 12 '12 at 10:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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