Populate drop down with user created sharepoint fields

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());
                                }
                            }
                        }

                    }
                }
            
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: