如何在Sharepoint中找到登录用户?

Bri*_*gar 13 c# sharepoint web-parts

我开发了一个必须部署在Sharepoint服务器上的"Web部件".我需要用户的用户名,该用户已登录Web部件中的sharepoint服务器.

我如何获得该用户名?

KMå*_*Mån 28

以下为我工作:

SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;
Run Code Online (Sandbox Code Playgroud)

并检查出.

  • 或者更好:`string strUserName = SPContext.Current.Web.CurrentUser.LoginName;` (10认同)

Mik*_*son 12

您可以使用:

SPWeb web = SPControl.GetContextWeb(this.Context);
string userName = web.CurrentUser.LoginName;
Run Code Online (Sandbox Code Playgroud)

要么

string userName = this.Context.User.Identity.Name;
Run Code Online (Sandbox Code Playgroud)

您还应该检查this.Context.User.Identity.IsAuthenticated以确保在尝试提取用户名之前有用户登录.


Bri*_*gar 5

大家好,我得到了我的问题的答案希望这对你们有用...首先在您的 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;
Run Code Online (Sandbox Code Playgroud)

你的,吉加尔 <3