Responsive Ads Here

Tuesday, July 17, 2012

Programmatically set "Person or Group" column to select user from a specified group


SPFieldUser Properties

SelectionGroup

[ClientCallableAttribute]
public int SelectionGroup { get; set; }
Gets or sets a value that represents the SPGroup to associate with a user.

Property Value

Type: System.Int32 ID of the SPGroup that is being associated with a user.

SelectionMode

[ClientCallableAttribute]
public SPFieldUserSelectionMode SelectionMode { get; set; }
Gets or sets a value that specifies whether only individuals or both individuals and groups can be selected as field values in forms for creating or editing list items.

Property Value

Type: Microsoft.SharePoint.SPFieldUserSelectionMode An SPFieldUserSelectionMode value that specifies whether individuals or both individuals and groups can be selected in forms.

Example

                            //Check for tasks list
                            if (spList.Title == Constants.LstTasks)
                            {
                                //Get the Assigned to Column
                                fld = spList.Fields.Cast<SPField>().FirstOrDefault(x => x.Title == "Assigned To");
                                if (fld != null)
                                {
                                    SPFieldUser flduser = fld as SPFieldUser;
                                    if (flduser != null)
                                    {
                                        //Get the all users group of the current workspace
                                        string strGroupName = strProjectId + "-" + Constants.GrpAllUsers;
                                        SPGroup grpAllUsers = spWeb.SiteGroups.Cast<SPGroup>().FirstOrDefault(siteGroup => siteGroup.Name.Equals(strGroupName, StringComparison.OrdinalIgnoreCase));
                                        if(grpAllUsers!=null)
                                        {
                                            flduser.SelectionGroup = grpAllUsers.ID;
                                            flduser.SelectionMode = SPFieldUserSelectionMode.PeopleOnly;
                                            flduser.Update();
                                            spList.Update();
                                        }
                                    }
                                }
                            }

No comments:

Post a Comment