uzyskać kolumnę, która jako FieldUserValue z listy, skoro masz skorzystanie z odnośnika wartość id, a następnie kwerendy przeciwko Użytkownikowi Sites listy informacyjnej. W poniższym przykładzie buforuję wyniki, aby zapobiec wyszukiwaniu tego samego identyfikatora więcej niż raz, ponieważ zapytanie może być kosztowne.
private readonly Dictionary<int, string> userNameCache = new Dictionary<int, string>();
public string GetUserName(object user)
{
if (user == null)
{
return string.Empty;
}
var username = string.Empty;
var spUser = user as FieldUserValue;
if (spUser != null)
{
if (!userNameCache.TryGetValue(spUser.LookupId, out username))
{
var userInfoList = context.Web.SiteUserInfoList;
context.Load(userInfoList);
var query = new CamlQuery { ViewXml = "<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='ID' /><Value Type='int'>" + spUser.LookupId + "</Value></Eq></Where></Query></View>" };
var users = userInfoList.GetItems(query);
context.Load(users, items => items.Include(
item => item.Id,
item => item["Name"]));
if (context.TryExecuteQuery())
{
var principal = users.GetById(spUser.LookupId);
context.Load(principal);
context.ExecuteQuery()
username = principal["Name"] as string;
userNameCache.Add(spUser.LookupId, username);
}
}
}
return username;
}
użyciu ctx.Web.EnsureUser pracował jak czar ... – onzur
LookupValue daje mi wyświetlać nazwę i nie zalogować nazwę. –