[英]How to get Current User in Sharepoint site workflow 2010?
Am trying to find the current logged user in sharepoint site workflow 2010 while creating a project. 我在创建项目时试图在sharepoint站点工作流2010中找到当前登录的用户。 Based on the user, I would like to retrieve the current user's project manager. 根据用户,我想检索当前用户的项目经理。 Every time am trying to retrieve current user name, it's giving System Account . 每次尝试检索当前用户名时,都会给出系统帐户 。
I even tried logging in as different user but still displaying System Account as the current user. 我甚至尝试以不同的用户身份登录,但仍然显示系统帐户作为当前用户。
I tried following options : 我尝试了以下选项:
SPUser user = workflowProperties.OriginatorUser;
SPUser user = SPContext.Current.Web.CurrentUser;
SPWeb web = SPContext.Current.Web;
SPUser user = web.CurrentUser;
SPContext.Current.Web.CurrentUser.LoginName;
But everything failed. 但一切都失败了。 Am sure that am doing something wrong. 我确定我做错了什么。 I don't know the correct procedure. 我不知道正确的程序。 Some procedures give me null or Object reference not set to an instance of the object or System Account details. 某些过程会将null或Object引用设置为未设置为对象或系统帐户详细信息的实例。 I have even tried using elevated permission and its giving me null value. 我甚至尝试使用提升权限,它给我空值。
SPSecurity.CodeToRunElevated elevatedSubmit = new SPSecurity.CodeToRunElevated(delegate
{
//SPUser user = SPContext.Current.Web.CurrentUser;
//string strAssignedTo = user.Name;
string sspURL = "http://localhost/PWA/default.aspx";
SPSite site = new SPSite(sspURL);
SPWeb web = site.OpenWeb();
SPUser theUser = web.CurrentUser;
string strUserName = theUser.Name;
});
SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);
Am I supposed to add users explicitly as SPUser or any other changes before trying to retrieve current user via workflow ? 在尝试通过工作流检索当前用户之前,我是否应该明确地将用户添加为SPUser或任何其他更改?
SharePoint 2010 Get Current Username / Logged in User SharePoint 2010获取当前用户名/登录用户
check this StackExchange answer as well Get the current user interacting with a site workflow 检查此StackExchange答案以及当前用户与站点工作流程的交互
if you are wanting to get the current user when you log in you can try something like this 如果您想在登录时获得当前用户,可以尝试这样的方法
SPWeb webSite = SPControl.GetContextWeb(SPContext);
SPUser spUser = webSite.CurrentUser;
string strUserName = spUser.LoginName;
using this line below will return the OriginatorUser however if you are not logged in as Admin you will get the System Account UserName 使用下面这一行将返回OriginatorUser但是如果您没有以管理员身份登录,您将获得系统帐户用户名
//This give the Login name e.g <domain>\<name>
workflowProperties.OriginatorUser.LoginName;
** Note ** I noticed that in your code you are trying to get / assign user twice you should only need this line if you decide to use your code **注意**我注意到在您的代码中,您尝试获取/分配用户两次,如果您决定使用代码,则只需要此行
SPUser user = SPContext.Current.Web.CurrentUser;
its seem to work : 它似乎工作:
SPUser user = this.workflowProperties.OriginatorUser;
RunWithElevatedPrivileges
gives you system account
privileges in addition to the privileges you would get with a reverto call. 除了使用reverto调用获得的权限外, RunWithElevatedPrivileges
为您提供system account
权限。
is that code executed on SPSecurity.RunWithElevatedPrivileges method ?? 是在SPSecurity.RunWithElevatedPrivileges方法上执行的代码?
Here is a another trick that i found : 这是我发现的另一个技巧:
string ModifiedbyUserName = Convert.ToString(workflowProperties.Item.GetFormattedValue("Modified By"));
see this : logged-in user in workflow 看到这个: 工作流程中的登录用户
Helps it helps!! 帮助它!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.