Apr 29, 2011

Get sharepoint user name or email address

c# get sharepoint user name or email address
-------
static public string GetUserEMail(object user, ClientContext context)
{
Dictionary userNameCache = new Dictionary();
if (user == null)
{
return string.Empty;
}

var username = string.Empty;
var useremail = string.Empty;
var spUser = user as FieldUserValue;
if (spUser != null)
{
if (!userNameCache.TryGetValue(spUser.LookupId, out useremail))
{
var userInfoList = context.Web.SiteUserInfoList;
context.Load(userInfoList);
var query = new CamlQuery { ViewXml = "" + spUser.LookupId + "" };
var users = userInfoList.GetItems(query);
context.Load(users, items => items.Include(
item => item.Id,
item => item["Name"]));
var principal = users.GetById(spUser.LookupId);
context.Load(principal);
context.ExecuteQuery();
username = principal["Name"] as string;
useremail = username.Substring(username.IndexOf("\\") + 1) + "@test.com";
userNameCache.Add(spUser.LookupId, useremail);
}
}
return useremail;
}

0 意見: