检查 PeopleManager 上是否存在 personProperty

Check if a personProperty exists on PeopleManager

我有以下代码来从 AD 中的用户检索数据:

PeopleManager peopleManager = new PeopleManager(clientContext);
PersonProperties personProperties = peopleManager.GetPropertiesFor(loginName);
clientContext.Load(personProperties);
clientContext.Load(personProperties, p => p.AccountName, p => p.UserProfileProperties);
clientContext.ExecuteQuery();

var personalUrl=personProperties.PersonalUrl;

最后一行在 某些 上引发错误,但并非在所有用户上引发错误:

Microsoft.SharePoint.Client.ServerObjectNullReferenceException: Object reference not set to an instance of an object on server. The object is associated with method GetPropertiesFor. at Microsoft.SharePoint.Client.ClientObject.CheckUninitializedProperty(String propName) at Microsoft.SharePoint.Client.UserProfiles.PersonProperties.get_PersonalUrl()

这么简单地说:有没有一种方法可以在尝试检索值之前检查是否设置了 属性?

或者 try..catch 是解决此问题的唯一(丑陋)方法吗?

PersonProperties 对象具有 IsPropertyAvailable 方法,该方法 returns 一个布尔值,具体取决于 属性 是否已设置。

https://msdn.microsoft.com/en-us/library/ee546589.aspx

所以最后一行需要替换为:

string personalUrl=personProperties.IsPropertyAvailable("PersonalUrl")?personProperties.PersonalUrl:null;