Thursday, January 19, 2017

Get all user email form a SharePoint Group,Get Email From Active Directory or AD by login name


• Get all user email form a SharePoint Group, 
public static string GetAllUserEmailFromGroup(string strsite, string GrpName)
        {
            string strArray = "";
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(strsite))
                {
                    using (SPWeb oweb = site.OpenWeb())
                    {
                        SPGroup group = oweb.SiteGroups[GrpName];

                        foreach (SPUser user in group.Users)
                        {
                            if (!string.IsNullOrEmpty(user.Email))
                                strArray+=user.Email+";";
                        }
                    }
                }
            });
            return strArray;
        }

  

• Get Email From Active Directory or AD by login name
public static List<string> GetUserEmailFromADBy(string strsite, string uName)
        {
            List<string> strEmailArray = new List<string>();
            DirectorySearcher dirSearcher = new DirectorySearcher();
            DirectoryEntry entry = new DirectoryEntry(dirSearcher.SearchRoot.Path);
            dirSearcher.Filter = "(&(objectClass=user)(anr=" + uName + "))";
            //dirSearcher.Filter = "(&(objectClass=user)(objectcategory=person)(mail=" + uName + "*))";

            SearchResult srEmail = dirSearcher.FindOne();

            string propName = "mail";
            ResultPropertyValueCollection valColl = srEmail.Properties[propName];
            try
            {
                string email= valColl[0].ToString();
            }
            catch
            {
              
            }
            return strEmailArray;

        }


• Common method to Execute the CAML in Sharepoint Server object model.
public static SPListItemCollection GetListItemCollectionBy(string strsite, string listname, string querystr) //// added
        {
            SPListItemCollection itemColl = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(strsite))
                {
                    using (SPWeb oweb = site.OpenWeb())
                    {
                        SPList oList = oweb.Lists.TryGetList(listname);
                        SPQuery query = new SPQuery();
                        query.Query = querystr;
                        itemColl = oList.GetItems(query);

                    }
                }
            });
            return itemColl;
        }

• Common method to Execute the SQL procedure to select the Row/table.
public static DataSet GetResultFromSqlDatabase(string cmdText, List<SqlParameter> paramColl)
        {
            string DBConnectionString = string.Empty;
            if (ConfigurationManager.AppSettings.AllKeys != null && ConfigurationManager.AppSettings.AllKeys.Contains("DbConnectionString"))
                DBConnectionString = ConfigurationManager.AppSettings["DbConnectionString"];
            SqlDataAdapter sqlAdapter = null;
            SqlConnection sqlConnection = null;
            SqlCommand sqlCommand = null;
            DataSet dsResult = new DataSet();
            if (!String.IsNullOrEmpty(DBConnectionString))
            {
                sqlConnection = new SqlConnection(DBConnectionString);
                sqlCommand = new SqlCommand(cmdText, sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;     // Uncomment it in case Stored Procedure.
                sqlCommand.CommandTimeout = 240;
                foreach (SqlParameter p in paramColl)
                    sqlCommand.Parameters.Add(p);
                sqlAdapter = new SqlDataAdapter(sqlCommand);
                sqlAdapter.Fill(dsResult);
            }
            sqlAdapter.Dispose();
            sqlConnection.Dispose();
            sqlCommand.Dispose();
            DBConnectionString = string.Empty;
            return dsResult;
        }


No comments:

Post a Comment