4

I have a SharePoint list that contain column 'District' with some data in it. It contains 4 value, District 1, District 2, District 3 and District 4.

I want to grab that value 'District' in the SharePoint list items and put it in my ASP Form as a checkbox to eventually make a filter on a Grid View.

If anyone can help me with grabbing the value of SharePoint List items and turning it into checkbox, would really appreciate it.

2
  • Is data type of your District is Choice?? or single lines of text? Commented Dec 23, 2019 at 7:06
  • It is a single line of text.
    – SPBoy
    Commented Dec 23, 2019 at 7:17

1 Answer 1

7

In case, the District is a single line of text, try the below code

using (SPSite site = new SPSite("SiteURL"))
   {
     using (SPWeb web = site.OpenWeb())
      {
       SPList list = web.Lists["ListName"];
       SPQuery query = new SPQuery();
       query.Query = "<View><ViewFields><FieldRef Name='District'/></ViewFields></View>"; 
       SPListItemCollection Items = list.GetItems(query);
      // Bind Checkbox List
       foreach (SPListItem Item in Items)
          {
             //CheckBoxListID.Items.Add(item["District"]);
              CheckBoxListID.Items.Add(new ListItem(Item.Title));
          }
      }
}

In case, the District is your field is a choice field, Please check, Retrieve a SharePoint Choice Field Value Using Server Object Model C#

3
  • Thankyou for the answer but i got a little red line under the 'item' before the [District] in this line CheckBoxList1.Items.Add(new ListItem(item.["District)"]));, what should i replace it with?
    – SPBoy
    Commented Dec 23, 2019 at 8:01
  • Could you try this CheckBoxListID.Items.Add(item["District"]); or CheckBoxListID.Items.Add(new ListItem(Item.Title)); Commented Dec 23, 2019 at 8:06
  • Thank you so much sir, that works like a charm=))
    – SPBoy
    Commented Dec 23, 2019 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.