简体   繁体   English

从Sharepoint中的当前用户获取名字和姓氏

[英]Get first and lastname from current user in Sharepoint

How do I get the first and last name of the current user in sharepoint? 如何在sharepoint中获得当前用户的名字和姓氏? My guess is to use the UserProfile property but i cant get it to work. 我的猜测是使用UserProfile属性,但我无法使其正常工作。 Do i need a nugetpackage for that? 我需要一个nugetpackage吗? But i dont have internet connection from my virutal machine so i cant do that. 但是我的病毒机器没有互联网连接,所以我不能这样做。

You can create a UserCollection or User variable. 您可以创建UserCollection或User变量。 Then you can split the Title text by " ". 然后,您可以用“”分隔标题文本。 This will help you get the First Name, LastName as well as MiddleName. 这将帮助您获取名字,姓氏和中间名。

   UserCollection managerGroupUsers = _managerGroupName.Users;                         
                        clientContext.Load(managerGroupUsers);
                        clientContext.ExecuteQuery();
                        string[] Username=null;
                        foreach (User u in managerGroupUsers)
                        {
                             Username= u.Title.Split(' ');
                            string firstName = Username[0];
                            string LastName = Username[1];

                        }

Create the clientcontext for your site and add the following code ` 为您的站点创建clientcontext并添加以下代码`

Group _managerGroupName=null; 
    if (clientContext != null)
                        {
                            var siteColl = clientContext.Site;
                            var web = siteColl.RootWeb;
                            GroupCollection groupColl = web.SiteGroups;`

                            clientContext.Load(siteColl);
                            clientContext.Load(web);




                            clientContext.ExecuteQuery(); 


    _managerGroupName = web.AssociatedOwnerGroup;
                                clientContext.Load(_managerGroupName);
                                clientContext.ExecuteQuery();                  
    UserCollection managerGroupUsers = _managerGroupName.Users;
            clientContext.Load(managerGroupUsers);
            clientContext.ExecuteQuery();
            string[] Username = null;
            foreach (User u in managerGroupUsers)
            {
                Username = u.Title.Split(' ');
                string firstName = Username[0];
                string LastName = Username[1];

            }     

It looks like from your pastebin you're making a new server side web part. 看起来从您的pastebin您正在制作一个新的服务器端Web部件。 So you don't need Microsoft.SharePoint.Client. 因此,您不需要Microsoft.SharePoint.Client。 The previous examples are done with client object model, but you're using server object model. 前面的示例是使用客户端对象模型完成的,但是您正在使用服务器对象模型。 Replace the contents ot your onStart() function with this: 用以下内容替换onStart()函数的内容:

SPUser currentUser = SPContext.Current.Web.CurrentUser;
string[] nameSegments = currentUser.Name.Split(' ');
this.Controls.Add(new LiteralControl("<div>First Name: " + nameSegments[0]));
this.Controls.Add(new LiteralControl("<div>Last Name: " + nameSegments[1]));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM