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:


Adding/Removing users to/from the group:

Note: We have to check before adding/removing users to/from the group and also check the users already exists or not in group. 
  
public void AddDeleteUsersInGroup(User user)
        {
            string GroupName = string.Empty;
            if (user.IsAdmin.HasValue && user.IsAdmin.Value)
                GroupName = "Group Owners";
            else if (user.IsApprover.HasValue && user.IsApprover.Value)
                GroupName = " Group Members";
            else
                GroupName = " Group Visitors";

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        try
                        {
                            web.AllowUnsafeUpdates = true;
                            SPUser spUser = web.EnsureUser(user.UserName);
                            if (spUser != null)
                            {
                                SPGroup spGroup = spGatingGroup(web, GroupName);
                                if (spGroup != null && (IsUserInSPGroup(spUser, GroupName.Trim())))
                                {
                                   //For adding user to the group
                                      spGroup.AddUser(spUser);
                                   //For removing user to the group
                                    spGroup.RemoveUser(spUser);
                                    spGroup.Update();
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
            });
        }
 
private SPGroup spGatingGroup(SPWeb spDLRWeb, string groupName)
        {
            SPGroup spGroup = null;
            try
            {
                spGroup = spWeb.SiteGroups[groupName];
                if (spGroup == nullthrow new Exception("Sharepoint Group name '" + groupName + "' not found");
            }
            catch (Exception ex)
            {
                //ErrorHandling(ex);
            }
            return spGroup;
        }

private bool IsUserInSPGroup(SPUser user, string strname)
        {
            foreach (SPGroup group in user.Groups)
            {
                if (group.Name == strname)
                    return true;
            }
            return false;
        }

Comments