Search This Blog

Wednesday, November 18, 2015

Retrieve value of a Sharepoint Choice Field in c# Visual Studio 2013

At this post i will elaborate how to create choice field and multi choice field  in SharePoint list then i go to visual studio to create new visual web part to retrieve it’s value to dropdownlist control or radiobuttonlist control  or checkboxlist control .

at our example we will use dropdownlist control .

create function called bind() and call it at page load event  of visual web part

code:

private void bind()
{
SPWeb site = SPContext.Current.Web;
SPList list = site.Lists["ListName"];
SPFieldChoice myChoicesfield = (SPFieldChoice)list.Fields["FieldName"];
for (int i = 0; i < myChoicesfield.Choices.Count; i++)
{
DropDownListName.Items.Add(myChoicesfield.Choices[i].ToString());
}
}

To create a checkboxlist from a multi choice field:

//create a check box list

  private void bind()
 {
     SPWeb site = SPContext.Current.Web;
     SPList list = site.Lists["ListName"];
     SPFieldMultiChoice choices = (SPFieldMultiChoice)list.Fields[FieldName];
     foreach (string str in choices.Choices)
     {
         cblCheckboxList.Items.Add(new ListItem(str, str));
     }
 }

No comments:

Post a Comment