Question

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?

Était-ce utile?

La solution 2

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.

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top