2010-08-01 29 views
11

Tôi đang sử dụng mô hình đối tượng phía khách hàng được quản lý trong SharePoint 2010. Và tôi muốn có được loginaName của AssignedTo người dùng trong danh sách Tác vụ.Cách lấy đối tượng Người dùng Sharepoint từ trường "AssignedTo" bằng mô hình đối tượng phía máy khách?

Trong mô hình đối tượng phía máy chủ, tôi sử dụng SPFieldUserValue.User.LoginName để lấy thuộc tính này nhưng trong mô hình đối tượng phía máy khách FieldUserValue.User không tồn tại.

Tôi làm cách nào để giải quyết tình huống này?

Cảm ơn

Trả lời

3

Bạn nhận được các cột mà như FieldUserValue từ danh sách, một khi bạn có mà bạn sử dụng giá trị tra cứu id và sau đó truy vấn so với Sites User Info List. Trong ví dụ bên dưới, tôi lưu trữ kết quả để ngăn không cho tìm kiếm cùng một id nhiều hơn một lần vì truy vấn có thể tốn kém.

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

Đây là mã cho điều đó. Tôi đã lấy một ví dụ về trường AssignedTo từ danh sách Tác vụ. Tôi hy vọng rằng sẽ giúp.

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

sử dụng ctx.Web.EnsureUser làm việc như một nét duyên dáng ... – onzur

+6

LookupValue mang lại cho tôi hiển thị tên và không tên đăng nhập. –

1

Tất cả mọi thứ ở trên làm việc cho tôi, nhưng thay vì:

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

tôi đã sử dụng:

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

5

Các fuv.LookupValue có thể chứa tên hiển thị, không phải là tên đăng nhập , vì vậy đề xuất của tôi là (giả sử bạn có FieldUserValue - fuv i đang n (như descibed bởi @ekhanna):

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

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

Xin lỗi điều này không hoạt động: 'Microsoft.SharePoint.Client.ServerException xảy ra/Người dùng được chỉ định 5651 không thể tìm thấy.' – PeterX

+0

Điều đó có nghĩa là không có người dùng nào có ID này. Đảm bảo bạn đang sử dụng ID của người dùng và KHÔNG chỉ mục của người dùng trong tuyển tập SiteUsers. – pholpar

+0

Vẫn đang hoạt động ngay hôm nay! Cuộc gọi tốt. –

Các vấn đề liên quan