如何从sharepoint

时间:2015-06-12 07:46:07

标签: asp.net-mvc sharepoint sharepoint-online

我是API的新手。尝试从sharepoint中提取用户配置文件我使用以下代码但不知道servername?域名?和用户名?

const string serverUrl = "http://sharepoint.com/";
            const string targetUser = "ttgdev-my.sharepoint.com\\testuser1@ttgdev.guru";

            // Connect to the client context.
            ClientContext clientContext = new ClientContext(serverUrl);

            // Get the PeopleManager object and then get the target user's properties.
            PeopleManager peopleManager = new PeopleManager(clientContext);
            PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser);

            // Load the request and run it on the server.
            // This example requests only the AccountName and UserProfileProperties
            // properties of the personProperties object.
            clientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
            clientContext.ExecuteQuery();

            foreach (var property in personProperties.UserProfileProperties)
            {
                Console.WriteLine(string.Format("{0}: {1}",
                    property.Key.ToString(), property.Value.ToString()));
            }
            Console.ReadKey(false);

请指导我它会给我错误 {“属性或字段'UserProfileProperties'尚未初始化。尚未请求或请求尚未执行。可能需要明确请求。”} 在以下一行

 clientContext.ExecuteQuery();

1 个答案:

答案 0 :(得分:1)

最有可能的是它与accountName变量的格式有关。 PeopleManager.GetPropertiesFor method期望i:0#.f|membership|jdow@contoso.onmicrosoft.com 参数以适当的格式指定,对于SharePoint Online,应以声明格式指定,例如:

targetUser
  

有关声明格式的详细信息,请点击this article

因此,在您的情况下,ttgdev-my.sharepoint.com\\testuser1@ttgdev.guru值应从i:0#.f|membership|testuser1@ttgdev.guru替换为using (var ctx = TokenHelper.GetClientContextWithAccessToken(webUri.ToString(), accessToken)) { // Get the PeopleManager object and then get the target user's properties. var peopleManager = new PeopleManager(ctx); PersonProperties personProperties = peopleManager.GetPropertiesFor(targetUser); //Retrieve picture property var result = peopleManager.GetUserProfilePropertyFor(accountName, "PictureURL"); ctx.ExecuteQuery(); Console.WriteLine("Picture Url: {0}",result.Value); }

以下示例演示了如何通过CSOM API检索用户个人资料图片:

{{1}}