2010-08-01 10 views
11

Sto utilizzando il modello dell'oggetto lato client gestito nel punto di condivisione 2010. E voglio ottenere loginaNome degli utenti Assegnato a nell'elenco Attività.Come ottenere oggetto utente Sharepoint dal campo "AssignedTo" utilizzando il modello di oggetti lato client?

Nel modello di oggetti lato server, utilizzo SPFieldUserValue.User.LoginName per ottenere questa proprietà ma nel modello di oggetto lato client FieldUserValue.User non esiste.

Come posso risolvere questa situazione?

Grazie

risposta

3

È possibile ottenere la colonna che, come il FieldUserValue dalla lista, una volta che hai di utilizzare il valore di ricerca id e poi esegue una query contro il Siti User Informazioni List. Nell'esempio seguente memorizzo i risultati nella cache per evitare di cercare lo stesso id più di una volta poiché la query può essere costosa.

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

Ecco il codice per questo. Ho preso un esempio di campo AssignedTo dall'elenco delle attività. Spero che aiuti.

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

utilizzando ctx.Web.EnsureUser lavorato come un fascino ... – onzur

+6

LookupValue mi dà visualizzazione del nome e non il login nome. –

1

Tutto sopra ha funzionato per me, ma invece di:

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

ho usato:

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

5

Il fuv.LookupValue puo 'contenere il nome visualizzato, non il nome di login , quindi il mio suggerimento è (assumendo che tu abbia il FieldUserValue - fuv i codice n (come descibed da @ekhanna):

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

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

Spiacente, questo non funziona: 'Microsoft.SharePoint.Client.ServerException si è verificato/L'utente specificato 5651 non è stato trovato. – PeterX

+0

Ciò significa che non vi è alcun utente con questo ID. Assicurati di utilizzare l'ID dell'utente e NON l'indice dell'utente nella raccolta SiteUsers. – pholpar

+0

Ancora funzionante oggi! Buona chiamata –

Problemi correlati