2010-08-01 12 views
11

Używam zarządzanej stronie klienta model obiektowy SharePoint 2010. I chcę dostać loginaName z AssignedTo użytkownika na liście zadań.Jak zdobyć obiekt użytkownika SharePoint z pola "AssignedTo" za pomocą modelu obiektu po stronie klienta?

W modelu obiekt po stronie serwera używam SPFieldUserValue.User.LoginName, aby uzyskać tę właściwość, ale w modelu obiektu po stronie klienta FieldUserValue.User nie istnieje.

Jak mogę rozwiązać tę sytuację?

Dzięki

Odpowiedz

3

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; 
    } 
12

Oto kod dla tego. Podałem przykład pola AssignedTo z listy zadań. Mam nadzieję że to pomogło. powyżej

public static User GetUserFromAssignedToField(string siteUrl) 
    { 
     // create site context 
     ClientContext ctx = new ClientContext(siteUrl); 

     // create web object 
     Web web = ctx.Web; 
     ctx.Load(web); 

     // get Tasks list 
     List list = ctx.Web.Lists.GetByTitle("Tasks"); 
     ctx.Load(list); 

     // get list item using Id e.g. updating first item in the list 
     ListItem targetListItem = list.GetItemById(1); 

     // Load only the assigned to field from the list item 
     ctx.Load(targetListItem, 
         item => item["AssignedTo"]); 
     ctx.ExecuteQuery(); 

     // create and cast the FieldUserValue from the value 
     FieldUserValue fuv = (FieldUserValue)targetListItem["AssignedTo"]; 

     Console.WriteLine("Request succeeded. \n\n"); 
     Console.WriteLine("Retrieved user Id is: {0}", fuv.LookupId); 
     Console.WriteLine("Retrieved login name is: {0}", fuv.LookupValue); 

     User user = ctx.Web.EnsureUser(fuv.LookupValue); 
     ctx.Load(user); 
     ctx.ExecuteQuery(); 

     return user; 
    } 
+0

użyciu ctx.Web.EnsureUser pracował jak czar ... – onzur

+6

LookupValue daje mi wyświetlać nazwę i nie zalogować nazwę. –

1

Wszystko działało na mnie, ale zamiast:

FieldUserValue fuv = (FieldUserValue)targetListItem["AssignedTo"];

użyłem:

FieldUserValue[] fuv = targetListItem["AssignedTo"] as FieldUserValue[];

5

fuv.LookupValue może zawierać nazwę wyświetlaną, nie login , więc moja sugestia jest (zakładając, że masz FieldUserValue - fuv Kod n (jak descibed przez @ekhanna):

var userId = fuv.LookupId; 
var user = ctx.Web.GetUserById(userId); 

ctx.Load(user); 
ctx.ExecuteQuery(); 
+0

Niestety to nie działa: 'Wystąpił Microsoft.SharePoint.Client.ServerException/Nie można znaleźć określonego użytkownika 5651." – PeterX

+0

Oznacza to, że nie ma użytkownika z tym identyfikatorem. Upewnij się, że używasz identyfikatora użytkownika, a NIE indeksu użytkownika w kolekcji SiteUsers. – pholpar

+0

Nadal działa dzisiaj! Dobra decyzja. –

Powiązane problemy