Getting a User Or AllUsers from Active Directory using C#

January 2, 2013 1 comment

.NET 3.5 introduced System.DirectoryServices.AccountManagement which manages directory objects independent of System.DirectoryServices namespace. I will be focusing on getting user and AllUsers from AD using AccountManagement dll.

To make is short just attaching code snippet. Details can be acquired from the link which helped me a lot.

After adding the reference in project and namespace in class. We need to have the following functions.

This one gets the domain with the system is connected.

        ///// <summary>
       ///// Gets the base principal context
      ///// </summary>
     ///// <returns>Returns the PrincipalContext object which contains Domain</returns>
       public PrincipalContext GetPrincipalContext()
       {

           PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain);
           return oPrincipalContext;
       }

To get a specific user.

        /// <summary>
        /// Gets a certain user on Active Directory
        /// </summary>
        /// <param name="sUserName">The username to get</param>
        /// <returns>Returns the UserPrincipal Object</returns>
        public UserPrincipal GetUser(string sUserName)
        {
            PrincipalContext oPrincipalContext = GetPrincipalContext();
            UserPrincipal oUserPrincipal =
               UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
            return oUserPrincipal;
        }

To get all users.

        /// <summary>
       /// Getting all users of AD and adding them into List.
       /// </summary>
       /// <returns></returns>
       public List<string> GetAllUsers()
       {
           try
           {
               List<string> users = new List<string>();

               PrincipalContext oPrincipal = GetPrincipalContext();
               GroupPrincipal gPrincipal = GroupPrincipal.FindByIdentity(oPrincipal, IdentityType.SamAccountName, "Domain Users");

               if (gPrincipal != null)
               {
                   foreach (Principal p in gPrincipal.GetMembers(false))
                   {
                       users.Add(p.DisplayName);
                   }
               }

               return users;
           }
           catch (Exception ex)
           {

               throw ex;
           }
       }

Hope this helps 🙂 .

Advertisements

Cisco VPN Client for Windows 8 / Windows Server 2012.

December 28, 2012 10 comments

Till now Cisco has not launched VPN client for Windows 8 or Windows Server 2012, but it is always said ‘Where there is a will , there is a way’. In few of your minutes you will be able to run you Cisco VPN client successfully.

  1. Install Cisco VPN client , depending on your system specks (x86 or x64).
  2. Press (windows + R) or go to run command.
  3. Type ‘regedit’.
  4. Navigate to the following registry key: “HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\CVirtA”
  5. Double click DisplayName.
  • For x86, change the value data from something like “@oem8.inf,%CVirtA_Desc%;Cisco Systems VPN Adapter” to “Cisco Systems VPN Adapter
  • For x64, change the value data from something like “@oem8.inf,%CVirtA_Desc%;Cisco Systems VPN Adapter for 64-bit Windows” to “Cisco Systems VPN Adapter for 64-bit Windows

After it , just open your Cisco VPN client and you will be able to connect successfully.

Stsadm and PowerShell Commands to Add , Deploy/Install , Retract/Uninstall and Delete/Remove SharePoint Solution.

December 19, 2012 2 comments

I think this is good time to post stsadm and PowerShell commands for adding , deploying/Installing , retracting/uninstalling and removing SharePoint solutions (wsp).

Commands are quiet similar just with different names. PowerShell works on SharePoint 2010 where as stsadm works on both SharePoint 2007 and SharePoint 2010.

Stsadm Commands

Stsadm commands are executed from hive/bin.

Add Solution :

stsadm.exe -o addsolution -filename “complete name of SharePoint solution with extension”

For example :

stsadm.exe -o addsolution -filename “SharePoint.CustomProject.wsp”

Deploy Solution :

stsadm.exe -o deploysolution -name “complete name of SharePoint solution with extension” -immediate  -allowGacDeployment

For example :

stsadm.exe” -o deploysolution -name “SharePoint.CustomProject.wsp” -immediate  -allowGacDeployment

Retract Solution :

stsadm.exe” -o retractsolution -name “complete name of SharePoint solution with extension” –immediate

For example :

stsadm.exe” -o retractsolution -name “SharePoint.CustomProject.wsp” –immediate

Delete Solution :

stsadm.exe” -o deletesolution -name “complete name of SharePoint solution with extension”

For example :

stsadm.exe” -o deletesolution -name “SharePoint.CustomProject.wsp”

 

PowerShell Commands

Run SharePoint 2010 Management Shell.

Add

Add-SPSolution “complete path where wsp file exist/solution name”

For example :

Add-SPSolution c:\Users\Administrator\Desktop\SharePoint.CustomProject.wsp

Install

Install-SPSolution –Identity “solution name”  –AllWebApplications –GACDeployment

For example :

Install-SPSolution –identity SharePoint.CustomProject.wsp –AllWebApplications –GACDeployment

Uninstall

Uninstall-SPSolution –Identity “solution name” –AllWebApplications

For Example :

                Uninstall–SPSolution –Identity SharePoint.CustomProject.wsp – AllWebApplications

Remove

Remove-SPSolution –Identity “solution name”

For Example :

                Remove-SPSolution –Identity SharePoint.CustomProject.wsp

Custom Error Message for Event Receiver

June 20, 2012 1 comment

 

SharePoint default error are not user friendly, for that if you need to show your own error message for even receiver exception then what would you do ?

This blog post is simply and specially written to deal in that situation.

You just need assign your custom error message to properties.ErrorMessage

Smile 
                        properties.ErrorMessage = This is my custom error message.;
                        properties.Cancel = true;

Moreover you can perform formatting with string for better presentation of your custom error message.

Setting Drop Down Selected Index programmatically.

June 9, 2012 4 comments

What is you need to select the item from the drop down list programmatically where you don’t know on which index it would be. For that simplest way is

ddlData.SelectedIndex=ddlData.Items.IndexOf(ddlData.Items.FindByText("value"));
Happy CodingSmile
 

Cannot create Content Database with Domain Account , Installing MOSS.

It took almost a day to install MOSS on my machine. Previously, I had SharePoint 2010 server on my machine , then I switched to MOSS as it was requirement.
In nut shell , Every time it continued failing on step 2 ‘Cannot create content database’.
In logs there was nothing special, I was using domain account and it already had all the required rights for MOSS as same user was running SharePoint 2010 server.

After googling so many blogs , I red a statement ‘ if you have Microsoft Office Installed then un-Install it’.
I thought to give it a try as except this every thing ‘rights and permissions ‘ were as expected.

I un-installed the Microsoft office and ran ‘SharePoint Products and Technologies Configuration Wizard’. You know what ?

It worked as a charm without any error Smile.

So It is good to install Microsoft Office after SharePoint Smile.