By default, we can get the number of built-in fields or internal fields if you try to retrieve the fields in a list. My below post will help to add only the custom fields to the dropdown list by avoiding all the internal, built-in and hidden fields.
using (SPSite
site = new SPSite(SPContext.Current.Site.ID))
{
SPWeb web = site.OpenWeb(SPContext.Current.Web.ID);
web.AllowUnsafeUpdates
= true;
SPList list = web.Lists[new
Guid(listId)];
var fields = from SPField f in
list.Fields
where !f.Hidden && f.AuthoringInfo == "" &&
f.Type
!= SPFieldType.Attachments &&
f.Type
!= SPFieldType.WorkflowStatus
orderby
f.Title
select new {
ListItem = new ListItem(f.Title,
f.Id.ToString()),
Readonly = f.ReadOnlyField, FieldType = f.Type };
ddlDestfield.DataSource = from g
in fields where
!g.Readonly &&
(g.FieldType == SPFieldType.Note
|| g.FieldType == SPFieldType.Text) select g.ListItem;
ddlDestfield.DataBind();
ddlDestfield.Items.Insert(0, new
ListItem("[Select]",
"none"));
}
|
Output:
No comments:
Post a Comment