PeopleEditor的取值及赋值
PeopleEditor的取值及赋值方法
Code
/// <summary>
/// 从PeopleEditor取值
/// </summary>
/// <param name="peopleEditor"></param>
/// <returns></returns>
public static List<SPPrincipal> GetSPPrincipalsFromPeopleEditor(PeopleEditor peopleEditor)
{
List<SPPrincipal> rtn = new List<SPPrincipal>();
SPWeb web = SPContext.Current.Web;
SPUserCollection users = web.SiteUsers;
SPGroupCollection groups = web.SiteGroups;
foreach (PickerEntity pe in peopleEditor.ResolvedEntities)
{
string principalType = pe.EntityData["PrincipalType"].ToString();
if (principalType == "User" || principalType == "SecurityGroup")
{
string loginName=pe.Key;
foreach (SPUser u in users)
{
if (u.LoginName == loginName)
{
rtn.Add(u);
break;
}
}
}
else if (principalType == "SharePointGroup")
{
string groupName = pe.Key;
foreach (SPGroup g in groups)
{
if (g.Name == groupName)
{
rtn.Add(g);
break;
}
}
}
}
return rtn;
}
/// <summary>
/// 为PeopleEditor赋值
/// </summary>
/// <param name="peopleEditor"></param>
/// <param name="spPrincipals"></param>
public static void SetPeopleEditorValue(PeopleEditor peopleEditor, List<SPPrincipal> spPrincipals)
{
string principalsStr;
if (spPrincipals==null || spPrincipals.Count == 0)
principalsStr = "";
else
{
List<string> principalsStrCollection = new List<string>();
foreach (SPPrincipal principal in spPrincipals)
{
SPUser user = principal as SPUser;
SPGroup group = principal as SPGroup;
if (user != null || group != null)
principalsStrCollection.Add(principal.Name);
}
principalsStr = string.Join(",", principalsStrCollection.ToArray());
}
peopleEditor.CommaSeparatedAccounts = principalsStr;
}
/// <summary>
/// 从PeopleEditor取值
/// </summary>
/// <param name="peopleEditor"></param>
/// <returns></returns>
public static List<SPPrincipal> GetSPPrincipalsFromPeopleEditor(PeopleEditor peopleEditor)
{
List<SPPrincipal> rtn = new List<SPPrincipal>();
SPWeb web = SPContext.Current.Web;
SPUserCollection users = web.SiteUsers;
SPGroupCollection groups = web.SiteGroups;
foreach (PickerEntity pe in peopleEditor.ResolvedEntities)
{
string principalType = pe.EntityData["PrincipalType"].ToString();
if (principalType == "User" || principalType == "SecurityGroup")
{
string loginName=pe.Key;
foreach (SPUser u in users)
{
if (u.LoginName == loginName)
{
rtn.Add(u);
break;
}
}
}
else if (principalType == "SharePointGroup")
{
string groupName = pe.Key;
foreach (SPGroup g in groups)
{
if (g.Name == groupName)
{
rtn.Add(g);
break;
}
}
}
}
return rtn;
}
/// <summary>
/// 为PeopleEditor赋值
/// </summary>
/// <param name="peopleEditor"></param>
/// <param name="spPrincipals"></param>
public static void SetPeopleEditorValue(PeopleEditor peopleEditor, List<SPPrincipal> spPrincipals)
{
string principalsStr;
if (spPrincipals==null || spPrincipals.Count == 0)
principalsStr = "";
else
{
List<string> principalsStrCollection = new List<string>();
foreach (SPPrincipal principal in spPrincipals)
{
SPUser user = principal as SPUser;
SPGroup group = principal as SPGroup;
if (user != null || group != null)
principalsStrCollection.Add(principal.Name);
}
principalsStr = string.Join(",", principalsStrCollection.ToArray());
}
peopleEditor.CommaSeparatedAccounts = principalsStr;
}