Saturday, October 1, 2011

Get data from SPFieldLookup(allow multiple values) and SPFieldChoice(allow multiple selections)

The following code sample shows how to get data from SPFiedLookup that allows  multiple values and SPFieldChoice(allow multiple selections) of a record. This also helps us to remove some characters such as "1;#" in lookup value and choice value.

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:

Tin said...

It's useful.

HABA said...

what a nice post! u are my life saver! thanks a lot

j said...

how do you use this code ?

Unknown said...

I've updated the code. It's simpler now, u can test it

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Affiliate Network Reviews