Thursday, 29 March 2012

Check if user field is multiple or single user



Type fieldType = sliAction.Fields[fieldName].FieldValueType;
if (fieldType.Name.Equals("SPFieldUserValue"))
{
if (sliAction[fieldName] != null)
{
SPFieldUserValueCollection userValues = new SPFieldUserValueCollection(web, sliAction[fieldName].ToString());
string usr = "";
foreach (SPFieldUserValue userV in userValues)
{
usr = usr + userV.LookupValue;
}
dr[fieldName] = usr;
}
if (sliAction["RespExt"] != null)
dr[fieldName] = sliAction["RespExt"].ToString();
}
else if(fieldType.Name.Equals("SPFieldUserValueCollection"))
{
if (sliAction[fieldName] != null)
{
SPFieldUserValueCollection userValues = new SPFieldUserValueCollection(web, sliAction[fieldName].ToString());
foreach(SPFieldUserValue spu in userValues)
dr[fieldName] = dr[fieldName] + "," + spu.User.Name;
dr[fieldName] = dr[fieldName].ToString().Substring(1, dr[fieldName].ToString().Length - 1);
}
if (sliAction["RespExt"] != null)
dr[fieldName] = sliAction["RespExt"].ToString();
}
else if (fieldType == typeof(DateTime))
{
if (sliAction[fieldName] != null)
{
string dateString = sliAction.Fields[fieldName].GetFieldValueForEdit(sliAction[fieldName]);
dr[fieldName] = dateString;
}
else
{
dr[fieldName] = "-";
}
}

else
dr[fieldName] = sliAction[fieldName];
}

No comments:

Post a Comment