Click here to Skip to main content
13,549,226 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

26.1K views
Posted 23 Jun 2011
Licenced CPOL

Get value collection of a SharePoint Choice Field

, 24 Jun 2011
Rate this:
Please Sign up or sign in to vote.
How to read choice values for Multi choice questions in a Survey
Multi choice questions are very common in surveys. SpFieldChoice object can be used to access choice values programmatically.
SpFieldChoice provides a string collection which stores the choice values for multi choice field type.

Following is the code:
public static List<string> GetChoiceFieldValues
(string listName,string fieldName, string siteCollection, string webSite)
        {
            List<string> fieldList;

            SPSite spSite = null;
            SPWeb spWeb = null;

            try
            {
                if (siteCollection != null)
                    spSite = new SPSite(siteCollection);
                else
                    spSite = SPContext.Current.Site;

                if (webSite != null)
                    spWeb = spSite.OpenWeb(webSite);
                else
                    spWeb = spSite.OpenWeb();

                SPList spList = spWeb.Lists[listName];

                SPFieldChoice field = (SPFieldChoice)spList.Fields[fieldName];

                fieldList = new List<string>();

                foreach (string str in field.Choices)
                {
                    fieldList.Add(str);
                }
            }
            catch (Exception ex)
            {
                LogException(ex);
                throw;
            }
            finally
            {
                if(spWeb != null)
                    spWeb.Close();

                if(spSite != null)
                    spSite.Close();
            }

            return fieldList;
        }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ddthien
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180515.1 | Last Updated 24 Jun 2011
Article Copyright 2011 by ddthien
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid