using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Runtime;
private static void Load<T>(ClientContext context, T item)
where T:ClientObject
{
context.Load<T>(item);
context.ExecuteQuery();
}
using(var context = new ClientContext("http://MyServer/sites/MySiteCollection"))
{
var site = context.Site;
Load(context, site);
var web = site.RootWeb;
Load(context, web);
Load(context, web.Lists);
var departmentList = web.List.GetByTitle("Department");
Load(context, departmentList);
// CAML Query
var employeesFieldQuery = new CamlQuery()
{
ViewXml = "<View><Query><Where>...</Where></Query></View>"; // TODO: check this
};
var employeeList = departmentList.GetItems(employeesFieldQuery);
Load(context, employeeList);
// Filter
var employeeList = departmentList.GetItems(employeesFieldQuery);
context.Load(employeeList, list => list.Fields.Where(field => field.Title == "Employees"));
context.ExecuteQuery();
IEnumerable<FieldUserValue> employees = item["Employees"] as FieldUserValue[];
foreach(var employee in employees)
{
var groups = clientContext.Web.SiteGroups;
var group = groups.GetById(employee.LookupId);
var users = group.Users;
context.Load(users); // TODO: load only the specific user based on LoginName/LookupValue
context.ExecuteQuery();
foreach (var user in users.Where(u => u.LoginName == employee.LookupValue))
{
Console.WriteLine("User: {0} ID: {1} Email: {2} Login Name: {3}", user.Title, user.Id, user.Email, user.LoginName);
}
}