Wednesday, August 27, 2008

How to get currently logged-in user in workflow

Hi All,

you will always have simple requirement whenever you are working with workflow is to find the currently logged-in user.

It is really frustrating that it is easy to find who is the originator of the workflowproperties but you cannot find who is currently logged in user from workflowproperties even you have properties available.

Let me give you the scenario.

I assume that you have WorkflowProperties object.

So if you write ,

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

you will get everything,

but if you write,

workflowProperties.Web.CurrentUser.Name

All you get is only system account, even the properties from the name looks like it will return you current logged in user. but it actually doesn't.

so why is it not returning it? what can be the reason.

Reason is Workflow actually does not run in the same session or same context, because workflow is to be hosted and to be called from the application by setting up the connection.

So it can be called up by any other application,office applications or any other source, it cannot be always the MOSS that triggers the workflow, and even if you triggers the Workflow from MOSS environment, it will still return you System Account.

so, how to get the Currently Logged-in User?

Here is a trick that i found.

string ModifiedbyUserName = Convert.ToString(workflowProperties.
Item.GetFormattedValue("Modified By"));

This will give you the person who has modified the item, this helps only at between the worklfow not at the time of start up because at the time of start up, originatorname is the currently logged in name of the user.

Let's say, when you modify the task, then you get the Task Item and check for this property, so you will get the current User.

Here when i have used Item, then Item referce to the Item of the list which is being changed. List can be any list, then above is the way you can get the Current Logged in user.

Again the problem is it returns the entire string representing Id, Name and connected properties.

If you fetch this string and display in the Label, then it will get converted to hyperlink which will take you to the UserDisp.aspx.

so you need to tweak the string. Here is a way to tweak the string.

string[] strChar = { "ID=" };

string[] strChars = ModifiedbyUserName.Split(strChar, StringSplitOptions.None);

Int32 iUserID = Convert.ToInt32(strChars[1].Substring(0, strChars[1].IndexOf("\"")));

string strAssignedTo = string.Empty;

for (int iCnt = 0; iCnt <= workflowProperties.Web.SiteUsers.Count - 1;iCnt++)
{
if (workflowProperties.Web.SiteUsers[iCnt].ID == iUserID)
{
strAssignedTo = workflowProperties.Web.SiteUsers[iCnt].Name;
break;
}
}


Now you will get the Name of the currently logged in user. you can also get all properties of any user because now you have SiteUser in hand.

5 comments:

Vikash said...

Hi Malay Vasavada
thanks alot for this post,
i was looking for this only.
god bless u and keep the good work going

Unknown said...

Why wouldn't you just use the workflowProperties.OriginatorUser value as this is stamped with the credentials of the logged on user that executes the workflow

SharePoint Kings said...

HI Jimmy,

Thanks for taking your time and we appreciate your feedback.

I have specially mentioned this trick to solve the problem for those applications that calls the workflow.

If it is a MOSS Hosted solution, calling simple properties will work, however if it is office applications or any other web service that invokes the workflow, we don't get user directly.

sigeg said...

Just small modification. I Add one new field of person/group type. then, I copy the content of "Modified By" field to this new field. The result, I get much cleaner user name. Hope this help.

Sigeg said...

Just small modification. I add new field of person/group, then copy "modified by" field to this new field. The result, I can see much cleaner user name. Hope this help.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com