如何在Sharepoint中查找登录用户?
我开发了一个必须部署在Sharepoint服务器上的“Web部件”。 我需要用户的用户名,该用户已登录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
以确保在尝试提取用户名之前有用户登录。
嘿所有,我得到了我的问题的答案希望这对你们都有用…首先在web部分中添加对MicrosoftSharepoint.dll文件的引用。 然后用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;
你的,吉加尔<3
SPContext.Current.Web.CurrentUser
//不要忘记添加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 results = insPrincipalSearcher.FindAll(); foreach (Principal p in results) { Console.WriteLine(p.Name); }
您还可以通过_spPageContextInfo属性获取当前记录的用户ID。
_spPageContextInfo.userId
您将通过_spPageContextInfo获取当前用户的ID。 试试这可能对你有帮助。