Monday, March 9, 2015

How to Get Email Groups from Outlook (Different Active Directory Groups)

Design the Form as Following(Default.aspx)

<table>
<tr>
<td>
<asp:ListBox ID="lstGroups"runat="server"Height="520px"width="200px"AutoPostBack="true"EnableViewState="true"
onselectedindexchanged="lstGroups_SelectedIndexChanged"></asp:ListBox>
</td>
<td valign="top">
<asp:ListBox ID="lstGroupmembers"runat="server"Height="300px"Width="200px"AutoPostBack="true"></asp:ListBox>
</td>
</tr>

</table>




Default.aspx.cs

Add the following Namespaces
usingSystem.Collections.Specialized;
This namespace is used for  StringCollection

Add the following reference.
·       System.DirectoryServices.
·       System.DirectoryServices.AccountManagement.

Then u can add the namespace
using System.DirectoryServices;
using System.Configuration;

-----------------------------------------------------------------------------------------------
Write the web.config File as following.
  <appSettings>
    <add key="DomainName" value="ABC0"/>

<addkey="DefaultActiveDirectoryServer"value="LDAP://ABC0"/>
  </appSettings>


Write the following Code in Page Load
protected void Page_Load(object sender, EventArgs e)
    {
        foreach (string @group in GetGroups())
        {
            lstGroups.Items.Add(@group);
        }
    }


Write the following Methods in the Page.
public StringCollection GetGroupMembers(string strDomain, string strGroup)
   
{
        StringCollection groupMemebers = new StringCollection();

        try
        {
            DirectoryEntry ent = new DirectoryEntry("LDAP://DC=" + strDomain + ",DC=com");
            DirectorySearcher srch = new DirectorySearcher("(" + strGroup + ")");
            SearchResultCollection coll = srch.FindAll();
            foreach (SearchResult rs in coll)
            {
                ResultPropertyCollection resultPropColl = rs.Properties;
                foreach (Object memberColl in resultPropColl["member"])
                {
                    DirectoryEntry gpMemberEntry = new DirectoryEntry("LDAP://" + memberColl);
                    System.DirectoryServices.PropertyCollection userProps = gpMemberEntry.Properties;
                    object obVal = userProps["sAMAccountName"].Value;
                    if (null != obVal)
                    {
                        groupMemebers.Add(obVal.ToString());
                    }
                }
            }
        }

        catch (Exception ex)
        {
            Trace.Write(ex.Message);
        }

        return groupMemebers;

    }


public static List<string> GetGroups()
    {
        DirectoryEntry objADAM = default(DirectoryEntry);
        // Binding object.
        DirectoryEntry objGroupEntry = default(DirectoryEntry);
        // Group Results.
        DirectorySearcher objSearchADAM = default(DirectorySearcher);
        // Search object.
        SearchResultCollection objSearchResults = default(SearchResultCollection);
        // Results collection.
        string strPath = null;
        // Binding path.
        List<string> result = new List<string>();
        string Path = Convert.ToString(ConfigurationManager.AppSettings["DefaultActiveDirectoryServer"]);
        // Construct the binding string.
        strPath = Path;
        //Change to your ADserver

        // Get the AD LDS object.
        try
        {
            objADAM = new DirectoryEntry(strPath);
            objADAM.RefreshCache();
        }
        catch (Exception e)
        {
            throw e;
        }

        // Get search object, specify filter and scope,
        // perform search.
        try
        {
            objSearchADAM = new DirectorySearcher(objADAM);
            objSearchADAM.Filter = "(&(objectClass=group))";
            objSearchADAM.SearchScope = SearchScope.Subtree;
            objSearchResults = objSearchADAM.FindAll();

        }
        catch (Exception e)
        {
            throw e;
        }

        // Enumerate groups
        try
        {
            if (objSearchResults.Count != 0)
            {
                foreach (SearchResult objResult in objSearchResults)
                {
                    objGroupEntry = objResult.GetDirectoryEntry();

                    result.Add(objGroupEntry.Name);
                }
            }
            else
            {
                throw new Exception("No groups found");
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

        return result;
    }

========================================


protected void lstGroups_SelectedIndexChanged(object sender, EventArgs e)
    {
        string domainName = Convert.ToString(ConfigurationManager.AppSettings["DomainName"]);
        string group = lstGroups.SelectedValue;
        StringCollection groupMembers = this.GetGroupMembers(domainName, group);
        //Response.Write("The members of"+group +"are");
        lstGroupmembers.Items.Clear();
        foreach (string strMember in groupMembers)
        {
            //Response.Write("<br><b>" + strMember + "</b>");
            lstGroupmembers.Items.Add(strMember);
        }
    }



















Cascading DropDown using MVC






And click on Ok Button


And select the project template and click on ok


And go to controller Right click on it and select ADD Controller







Give the name of the controller


Demo Controller.cs





public class DemoController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LoadCountries()
        {
            List<SelectListItem> li = new List<SelectListItem>();
            li.Add(new SelectListItem { Text = "Select", Value = "0" });
            li.Add(new SelectListItem { Text = "India", Value = "1" });
            li.Add(new SelectListItem { Text = "USA", Value = "2" });
            li.Add(new SelectListItem { Text = "UK", Value = "3" });
            ViewData["Countries"] = li;
            return View();
        }
        public JsonResult GetCityDetails(string id)
        {
            List<SelectListItem> cities = new List<SelectListItem>();
            switch (id)
            {
                case "1":
                    cities.Add(new SelectListItem { Text = "Select", Value = "0" });
                    cities.Add(new SelectListItem { Text = "Hyderabad", Value = "1" });
                    cities.Add(new SelectListItem { Text = "Bangalore", Value = "2" });
                    cities.Add(new SelectListItem { Text = "Mumbai", Value = "3" });
                    cities.Add(new SelectListItem { Text = "Tirupathi", Value = "4" });
                    break;
                case "2":
                    break;
                case "3":
                    break;
            }
            return Json(new SelectList(cities, "Value", "Text"));
        }

    }


Right Click on LoadCoutries Method, and Select View






LoadCountries.cshtml





@{
    ViewBag.Title = "Address Details";
}
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#Country").change(function () {
            $("#City").empty();
            $.ajax({
                type: 'POST',
                url: '@Url.Action("GetCityDetails")',
                dataType: 'json',
                data: { id: $("#Country").val() },
                success: function (city) {
                    $.each(city, function (i, city) {
                        $("#City").append('<option value="' + city.Value + '">' + city.Text + '</option>');
                    }); 
                },
                error: function (ex) {
                    alert('Error Message.' + ex);
                }
            });
            return false;
        })
    });
</script>
@using (Html.BeginForm())
{
    <div style="color: Purple;">
        @Html.Label("Select County")
    </div>
    <div>
        @if (ViewData.ContainsKey("Countries"))
        {
            @Html.DropDownList("Country", ViewData["Countries"] as List<SelectListItem>, new { style = "width:250px", @class = "dropdown1" })
        }
    </div>
    
    <div>
        @Html.Label("Select City")
    </div>
    <div class="editor-field">
        @Html.DropDownList("City", new SelectList(string.Empty, "Value", "Text"), "Select City", new { style = "width:250px", @class = "dropdown1" })
    </div>

}


Output:




Sunday, March 8, 2015

Consume RESTful service using jQuery

Go to the following Url to Create Wcf Service using REST.



Default.aspx:

<body>
    <form id="form1" runat="server">
        <div>
            <table border="1" id="Employees">
                <tr>
                    <td><b>UserId</b></td>
                    <td><b>FirstName</b></td>
                    <td><b>LastName</b></td>
                    <td><b>Salary</b></td>
                </tr>
            </table>
        </div>
    </form>
</body>



javascript:

<head runat="server">
    <title></title>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function ()
        {
            $.ajax({
                type: "GET",
                url: "http://localhost:62153/Service1.svc/GetEmployeeList/",
                dataType: "xml",
                    success: function (xml) {
                        $(xml).find('Employee').each(function () {
                            var id = $(this).find('UserId').text();
                            var firstName = $(this).find('FirstName').text();
                            var lastName = $(this).find('LastName').text();
                            var salary = $(this).find('Salary').text();
                            $('<tr><td>' + id + '</td><td>' + firstName + '</td><td>' + lastName + '</td><td>' + salary+'</td>').appendTo('#Employees');
                            });
                    },
                    error: function (xhr) {
                        alert(xhr.responseText);
                    }
            });
        });
    </script>
</head>


Output:


Saturday, March 7, 2015

RESTFUL service using WCF


Delete the default two files: that is IService, Service1.


Add-> New Item.





Add New Item and select Class




Employee.cs

[DataContract]
    public class Employee
    {
        [DataMember]
        public int UserId { get; set; }
        [DataMember]
        public string FirstName { get; set; }
        [DataMember]
        public string LastName { get; set; }
        [DataMember]
        public int Salary { get; set; }
    }
    public partial class EmployeeData
    {
        private static readonly EmployeeData _instance = new EmployeeData();
        private EmployeeData() { }
        public static EmployeeData Instance
        {
            get
            {
                return _instance;
            }
        }

        public List<Employee> EmployeeList
        {
            get
            {
                return employee;
            }
        }
            private List<Employee> employee = new List<Employee>() 
            { 
                 new Employee() { UserId = 1, FirstName = "Product 1", LastName = "Category ", Salary=10}, 
                new Employee() { UserId = 1, FirstName = "Product 1", LastName = "Category ", Salary=10}, 
                new Employee() { UserId = 1, FirstName = "Product 1", LastName = "Category ", Salary=10}
            };

        }




Iservice.cs




Service1.cs



Global.asax.


web.config.





Output:




Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...