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


Sunday, January 15, 2017

Microsoft RDLC creation by C# programatically. Reporting servicescreate & View Simple RDLC Report in ASP.NET (C#)

Microsoft RDLC creation by C# programatically.Reporting services cretion.


create & View Simple RDLC Report in ASP.NET (C#)

 C# Code.

<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" /><br />
        <rsweb:ReportViewer ID="ReportViewer1" runat="server">
           
        </rsweb:ReportViewer>
 

Aspx code.

protected void Button2_Click(object sender, EventArgs e)
        {
            DataTable dtt = new DataTable();
            dtt.Columns.Add("Serial",typeof(string));
            dtt.Columns.Add("Name", typeof(string));
            dtt.Columns.Add("Dept", typeof(string));
            dtt.Columns.Add("Salary", typeof(string));
            dtt.Rows.Add("1","ShayamJi",".Net","20000");
            dtt.Rows.Add("2", "Ashish Ji", "Sharepoint", "30000");
            dtt.Rows.Add("3", "Trupthi ji", "Java", "21000");


            ReportDataSource dsrpt = new ReportDataSource("market", dtt);

            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(dsrpt);
            ReportViewer1.LocalReport.ReportPath=(Server.MapPath("Report1.rdlc"));
            ReportViewer1.LocalReport.Refresh();
            //this.ReportViewer1.ReportRefresh();
            //ReportViewer1.LocalReport.ReportEmbeddedResource
        }

 Steps:

1. Create a simple asp.net empty project.

2. In default.aspx insert ReportViewer form Toolbox=>Reporing.

3. Add new item DataSet in project.

4. Add table in DataSet table name and code name should be same.  

5. Add Report without wizard in project in  Solution Explorer.

6. In Report Data window add new and dataset.

7. In dataset Wizard give name and select the DataSetName1.xsd .

8. Add table in Rdlc and drag drop the column.