In this post, I use list named "Employee". I want to get the value from 3 column: Title, Skill and JoinedProjects. Skills is a SPFiedChoice column that allows multiple selections. JoinedProject is a SPLookupField with multiple values get from "Projects" list.
protected void showData()
{
SPWeb web = SPContext.Current.Web;
SPList list = web.Lists["Employee"];
SPListItemCollection items = list.Items;
string fullName, skills, projects;
foreach (SPListItem item in items)
{
fullName = item["Title"].ToString();
skills = item["Skills"].ToString();
string skillValue = "";
SPFieldMultiChoiceValue multiChoiceValue = new SPFieldMultiChoiceValue(skills);
for (int i = 0; i < multiChoiceValue.Count; i++)
{
skillValue = skillValue + multiChoiceValue[i].ToString();
if (i < multiChoiceValue.Count - 1)
{
skillValue = skillValue + ", ";
}
}
skills = skillValue;
projects = item["JoinedProjects"].ToString();
string projectValue = "";
SPFieldLookupValueCollection valueCol = new SPFieldLookupValueCollection(projects);
for (int i = 0; i < valueCol.Count; i++)
{
projectValue = projectValue + valueCol[i].LookupValue.ToString();
if (i < valueCol.Count - 1)
{
projectValue = projectValue + ", ";
}
}
projects = projectValue;
Response.Write(fullName + " | " + skills + " | " + projects + "</br>");
}
}
Result:
Hoang Thien Le | .NET, Sharepoint | AAB Hospital
Linda | PHP | AAB Hospital, The Moon Plaza
Tin Nguyen Lam Trung | .NET, JQuery, Silver Light | Lemon Hotel, New Bank System
Phon Vu | Java, PHP | New Bank System, The Moon Plaza
Betty | Java | AAB Hospital, Lemon Hotel
Le Dung | .NET, Silver Light | AAB Hospital, ABC Corp
Phan Tu | .NET, JQuery, Sharepoint | ABC Corp, Lemon Hotel
Hope this helps!
4 comments:
It's useful.
what a nice post! u are my life saver! thanks a lot
how do you use this code ?
I've updated the code. It's simpler now, u can test it
Post a Comment