Tuesday, February 3, 2015

SharePoint People Picker/editor Adding multiple users

SharePoint People Picker/editor Adding multiple users

 <label for="peAddUers">
                       <span class="mandatory">*</span>Choose Users</label>
                     <SharePoint:PeopleEditor ID="peAddUers" runat="server" Width="329px" AllowEmpty="true" MultiSelect="true" SelectionSet="User" />
                     <asp:CustomValidator ID="cvAddUers" runat="server"
                                    ValidationGroup="Save" CssClass="requiredMessage" ClientValidationFunction="UserValidation">
                                    Please enter atlest one user
                                </asp:CustomValidator>




Validation:

   function UserValidation(sender, args) {
        var isValid = false;
        var pickval = document.getElementById('<%=peAddUers.ClientID%>' + "_hiddenSpanData").value;

        pickval = pickval.replace(/&nbsp;/gi, '"');
        //alert(pickval);
        if ((pickval != null) && (pickval != '') && (pickval != ' ') && (pickval != '&#160;') && (pickval != '"')) {
            document.getElementById('<%=peAddUers.ClientID%>' + "_checkNames").click();
            isValid = true;
        }
        args.IsValid = isValid;
    }


Adding Users at back-end:
private void AddUsers()
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               SPWeb web = SPContext.Current.Web;
               Hashtable htEntityData = new Hashtable();
               int index = 0;
               List<string> lst = new List<string>();
               List<string> lst1 = new List<string>();
               for (index = 0; index <= this.peAddUers.ResolvedEntities.Count - 1; ++index)
               {
                   try
                   {
                       objEntity = (PickerEntity)this.peAddUers.ResolvedEntities[index];
                       if (objEntity.EntityData.ContainsKey("SPUserID"))
                       {
                           lst.Add(objEntity.EntityData["SPUserID"].ToString());
                           lst1.Add(objEntity.Key.ToString());
                           srvcRepCol.Add(new SPFieldUserValue(web, Convert.ToInt32(objEntity.EntityData["SPUserID"].ToString()), objEntity.Key));
                       }
                       else if (objEntity.EntityData.ContainsKey("AccountName"))
                       {
                           lst.Add(objEntity.EntityData["AccountName"].ToString());
                           lst1.Add(objEntity.Key.ToString());
                           try
                           {
                               SPUser spUsr = web.EnsureUser(objEntity.Key.ToString());
                               if (spUsr != null)
                               {
                                   srvcRepCol.Add(new SPFieldUserValue(web, Convert.ToInt32(spUsr.ID.ToString()), objEntity.Key));
                               }
                           }
                           catch (Exception)
                           {
                               // throw;
                           }
                           // srvcRepCol.Add(new SPFieldUserValue(web,objEntity.EntityData["AccountName"].ToString()), objEntity.Key));
                       }
                   }
                   catch (Exception ex)
                   {
                   }
               }

               bool isTrue = false;
               foreach (SPFieldUserValue spfuv in srvcRepCol)
               {
                   try
                   {
                       SPUser spUsr = web.EnsureUser(spfuv.User.LoginName);
                       if (spUsr != null)
                       {
                           if (ddlGroups.SelectedItem.Text != "Select") // SharePoint Group
                           {
                               SPGroup spGrp = web.Groups[ddlGroups.SelectedItem.Text];
                               if (spGrp != null)
                               {
                                   spGrp.AddUser(spUsr);
                                   web.Update();
                                   isTrue = true;
                               }
                           }
                           else
                           {
                               lblError.Visible = true;
                               lblError.Text = "Please enter valid user";
                           }
                       }
                   }
                   catch (Exception ex)
                   {
                      // handle Exception if user not ensures
                   }
                   if (isTrue)
                   {
                       lblSuccess.Visible = true;
                       lblSuccess.Text = "User(s) added uccessfully to" + " " + ddlGroups.SelectedItem.Text + " " + "Group";
                   }
                   else
                   {
                       lblSuccess.Visible = false;
                   }
               }

           });
        }


No comments:

Post a Comment