Get Value from SharePoint list into a Checkbox in C#
-
08-02-2021 - |
Question
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.
Solution
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#
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange