Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从Sharepoint中的当前用户获取名字和姓氏_C#_Sharepoint_Sharepoint 2013 - Fatal编程技术网

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

C# 从Sharepoint中的当前用户获取名字和姓氏,c#,sharepoint,sharepoint-2013,C#,Sharepoint,Sharepoint 2013,如何在sharepoint中获取当前用户的名字和姓氏? 我的猜测是使用UserProfile属性,但我无法让它工作。我需要一个nugetpackage吗?但是我的虚拟机没有internet连接,所以我无法连接。您可以创建用户集合或用户变量。然后可以将标题文本按“”拆分。这将帮助您获得名字、姓氏以及中间名 UserCollection managerGroupUsers = _managerGroupName.Users;

如何在sharepoint中获取当前用户的名字和姓氏?
我的猜测是使用UserProfile属性,但我无法让它工作。我需要一个nugetpackage吗?但是我的虚拟机没有internet连接,所以我无法连接。

您可以创建用户集合或用户变量。然后可以将标题文本按“”拆分。这将帮助您获得名字、姓氏以及中间名

   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];

                        }

为站点创建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];

            }     

从您的粘贴库中看起来您正在制作一个新的服务器端web部件。因此,您不需要Microsoft.SharePoint.Client。前面的示例是使用客户机对象模型完成的,但您使用的是服务器对象模型。将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]));
SPUser currentUser=SPContext.Current.Web.currentUser;
字符串[]名称项=currentUser.Name.Split(“”);
添加(新的LiteralControl(“名字:“+nameSegments[0]));
添加(新的LiteralControl(“姓氏:“+nameSegments[1]));

UserCollection、\u managerGroupName和clientContext似乎对我不起作用我需要下载nuget还是添加一个using?您需要添加一个名为Microsoft.SharePoint.Client的dll。我将拥有User和UserCollection类。声明一个新变量
ClientContext ClientContext=newclientcontext(“”)替换为你网站的url我让它工作了,但我不想要显示名称我想要名字/姓氏。所以我的显示名是Administrator,我的名字是Patrik last name Larsson,我想得到的不是显示名。