我已经开发了一个“Web部件”,需要部署在Sharepoint服务器上。我需要在Web部件中获取登录Sharepoint服务器的用户用户名。
我该如何获取该用户名?
我已经开发了一个“Web部件”,需要部署在Sharepoint服务器上。我需要在Web部件中获取登录Sharepoint服务器的用户用户名。
我该如何获取该用户名?
以下方法对我有效:
SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;
并检查这个。
你可以使用:
SPWeb web = SPControl.GetContextWeb(this.Context);
string userName = web.CurrentUser.LoginName;
或者string userName = this.Context.User.Identity.Name;
同时您还应该检查this.Context.User.Identity.IsAuthenticated
以确保在尝试提取用户名之前有用户已登录。
0#.w|domain\username
。但除此之外,第二个实现对我非常有效。 - GoldBishopuserid = this.Context.User.Identity.Name; userid = userid.Substring( userid.IndexOf( '|' ) + 1 );
并且能够将垃圾与所需信息分离。 - GoldBishop大家好,我找到了我的问题的答案,希望这对你们有用... 首先,在您的Web部件中添加对MicrosoftSharepoint.dll文件的引用。 然后编写 using Microsoft.SharePoint;
string username;
string sspURL = "URL where Sharepoint is deployed";
SPSite site = new SPSite(sspURL);
SPWeb web = site.OpenWeb();
SPUser user = web.CurrentUser;
username = user.LoginName;
site.AllowUnsafeUpdates = true;
亲爱的,Jigar <3
SPContext.Current.Web.CurrentUser
您还可以通过_spPageContextInfo属性获取当前已登录用户的ID。
_spPageContextInfo.userId
您可以通过_spPageContextInfo获取当前用户的ID。尝试这个可能会有帮助。
//别忘了添加 System.DirectoryServices.AccountManagement 的引用并使用 System.DirectoryServices.AccountManagement;
PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MyDomain","DC=MyDomain,DC=com");
UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext);
insUserPrincipal.Name = "*";
PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher();
insPrincipalSearcher.QueryFilter = insUserPrincipal;
PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll();
foreach (Principal p in results)
{
Console.WriteLine(p.Name);
}
string strUserName = SPContext.Current.Web.CurrentUser.LoginName;
该代码用于获取当前用户的登录名。 - iambriansreed