Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Sunday, December 2, 2012

SPField’s to the dropdown List programmatically:


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


Note: Set f.AuthoringInfo == null [In SP2007] and Set f.AuthoringInfo == "" [In SP2010]


Output:

No comments:

Post a Comment