Get available SharePoint list field options in a field type (Multi)Choice from codebehind




In a list with a field type “Choice” you can know available options of this field from codebehind without reading all list values and not making them unique with Distinct(). Especially, you can’t just read and make Distinct() list values if your field allows multiple choice or if you don't have all options.

It’s easy with SharePoint class SPFieldChoice and method "Choices" that returns the Choices that are used in the multichoice field.

For example, our list is on current SPWeb, our list is called "Products" and it’s in the current SPWeb and our field is called "Brand"

//let our list is on current SPWeb 
SPWeb thisweb = SPContext.Current.Web; 

//Our list is called "Products" and it’s in the current SPWeb and our field is called "Brand" 
SPFieldChoice spChoiceField = (SPFieldChoice)thisweb.Lists["Products"].Fields["Brand"];           
      foreach (string  ch in spChoiceField.Choices) 
         { 
           Controls.Add(new LiteralControl("<br />" + ch)); 
         }

 

So we use only SharePoint object model and we don’t need to think out anything.




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment





MarkiMarta.com. Notes of web-specialist
Since 2009
18+