从类型为 人员和组 的字段中取值
从人员和组的字段中取出的值形如 17;#张三;#20;#李四;#33;#王五 ,以下方法将其转换为List<SPPrincipal>
Code
/// <summary>
/// 从人员和组的集合的字符串表示形式解析出人员和组的集合
/// </summary>
/// <param name="web">SPWeb对象</param>
/// <param name="userString">人员和组的集合的字符串表示形式</param>
/// <returns>人员和组的集合</returns>
public static List<SPPrincipal> GetUsers(SPWeb web,string userString)
{
List<SPPrincipal> rtn=new List<SPPrincipal> ();
if(!string.IsNullOrEmpty (userString ))
{
foreach (Match m in Regex.Matches(userString, "(\\d+);#([^;]+)"))
{
int id = int.Parse(m.Groups[1].Value);
SPUser user=null;
foreach (SPUser u in web.SiteUsers)
{
if (u.ID == id)
{
user = u;
break;
}
}
if (user != null)
rtn.Add(user);
else
{
SPGroup group=null;
foreach (SPGroup g in web.SiteGroups)
{
if (g.ID == id)
{
group = g;
break;
}
}
if (group != null)
rtn.Add(group);
}
}
}
return rtn;
}
/// <summary>
/// 从人员和组的集合的字符串表示形式解析出人员和组的集合
/// </summary>
/// <param name="web">SPWeb对象</param>
/// <param name="userString">人员和组的集合的字符串表示形式</param>
/// <returns>人员和组的集合</returns>
public static List<SPPrincipal> GetUsers(SPWeb web,string userString)
{
List<SPPrincipal> rtn=new List<SPPrincipal> ();
if(!string.IsNullOrEmpty (userString ))
{
foreach (Match m in Regex.Matches(userString, "(\\d+);#([^;]+)"))
{
int id = int.Parse(m.Groups[1].Value);
SPUser user=null;
foreach (SPUser u in web.SiteUsers)
{
if (u.ID == id)
{
user = u;
break;
}
}
if (user != null)
rtn.Add(user);
else
{
SPGroup group=null;
foreach (SPGroup g in web.SiteGroups)
{
if (g.ID == id)
{
group = g;
break;
}
}
if (group != null)
rtn.Add(group);
}
}
}
return rtn;
}