我将从sharepoint站点返回用户名作为字符串。这是通过以下代码成功完成的,但我也使用它获取了域。我怎样才能通过sharepoint或以编程方式删除用户名而不是域名?域/用户名
private string CurrentUserName()
{
string userName = "NA";
SPContext currentContext;
try
{
//Getting the current context
currentContext = SPContext.Current;
}
catch (InvalidOperationException)
{
currentContext = null;
}
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
userName = SPContext.Current.Web.CurrentUser.LoginName;
}
else
{
}
return userName;
}
答案 0 :(得分:4)
假设以这种格式返回用户
domain\username
您可以执行以下操作:
string userWithoutDomain = userName.Substring(userName.IndexOf('\\') + 1);
如果格式是这样的
username@domain
然后以下工作
string userWithoutDomain = user.Substring(0, user.IndexOf('@'));
您可能应该测试您拥有的格式并根据该格式提取用户名。如果用户名既不包含@也不包含\,则只返回整个字符串。
答案 1 :(得分:0)
更改用户信息列表中的名称列。您将获得SPContext.Current.Web.CurrentUser.Name 因为登录名不能改变。
答案 2 :(得分:-2)
请使用以下代码
site = new SPSite(SPContext.Current.Site.ID);
web = site.OpenWeb();
userName = web.CurrentUser.Name.ToString() ;