Pages

Tuesday 29 April 2014

Programmatically get choice values from SharePoint List


Step 1: Create a Simple Method or use Code Directly in Existing Method.

        /// <summary>
        /// Get Choice Column Value from List
        /// </summary>
        /// <param name="List"> List Name</param>
        /// <param name="Field"> Field Name</param>
        /// <returns> List<String> Containing the Choice Column Values</returns>

        public List<string> GetChoiceColumnValues(string List, string Field)
        {
            List<string> Choices= null;
            try
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url.ToString()))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        SPList objList = web.GetList(List);
                        SPFieldChoice field = (SPFieldChoice)objList.Fields[Field];
                        Choices= new List<string>();

                        foreach (string str in field.Choices)
                        {
                            Choices.Add(str);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write("Get Values : " + ex.Message);
            }
            return Choices;
        }

how to Get Values from Strings List

List<string> objFields = GetChoiceColumnValues(SPContext.Current.Web.Url.ToString() + "/Lists/ListName", "Fieldname");
            foreach (string item in objFields)
            {
                item.ToString();

            }


No comments:

Post a Comment