Assume that there is a situation where you have to expose only the user defined fields from a sharepoint list by filling up a dropdown. Usually when you tried to get fields collection list from a sharepoint list or a document library you will get list of columns roughly which will include around 40+ columns.
Following code snippet explains you how to populate a drop down list only with the fields that you have created.
Populatet the dropdown using a sharepoint list :
using (SPSite site = new SPSite(siteUrl)) { using (SPWeb web = site.RootWeb) { SPList list = web.Lists["MainSpecificationList"]; SPList oSPList = web.Lists["MainSpecificationList"]; for (int i = 0; i < list.Fields.Count - 1; i++) { if (!list.Fields[i].Hidden && !list.Fields[i].ReadOnlyField && list.Fields[i].Type != SPFieldType.Attachments) { if (list.Fields[i].Title.ToString() != "Title" && list.Fields[i].Title.ToString() != "Content Type" && list.Fields[i].Title.ToString() != "SpecificationPDFURL" && list.Fields[i].Title.ToString() != "PDF View URL") { ddlDropDown.Items.Add(list.Fields[i].Title.ToString()); } } } } }
Populate the dropdown using a sharepoint View:
using (SPSite site = new SPSite(siteUrl)) { using (SPWeb web = site.RootWeb) { SPView view = list.Views["SearchView"]; SPListItemCollection itemCol = list.GetItems(view); for (int Cnt = 0; Cnt <= list.Fields.Count - 1; Cnt++) { if (!list.Fields[Cnt].Hidden && !list.Fields[Cnt].ReadOnlyField && list.Fields[Cnt].Type != SPFieldType.Attachments) { if (list.Fields[Cnt].Title.ToString() != "Title" && list.Fields[Cnt].Title.ToString() != "Content Type" && list.Fields[Cnt].Title.ToString() != "SpecificationPDFURL" && list.Fields[Cnt].Title.ToString() != "PDF View URL") { ddlDropDown.Items.Add(list.Fields[Cnt].Title.ToString()); } } } } }