Share via


The type or namespace name 'PrincipalContext' could not be found

Question

Monday, May 5, 2014 7:22 PM

I am attempting to retrieve a user's full name from AD with the code below in .aspx using C#. I am using Sharepoint Designer 2010. I keep getting the error 'An error occurred during the compilation of the requested file, or one of its dependencies. The type or namespace name 'PrincipalContext' could not be found (are you missing a using directive or an assembly reference?). What am I doing wrong?

@using System.IO;
@using System.DirectoryServices.AccountManagement;
@using System.DirectoryServices;

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    string fac1;
    string userid;
    string fname;
    string lname;
    string fullname;
 
    protected void btnShowInfo_Click(object sender, EventArgs e)
    {
 
        try
        {
            userid = GetHTTPContext();          
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message + "<BR>" + ex.StackTrace);
        }
        finally
        {

        }

        using (PrincipalContext context = new PrincipalContext(ContextType.Domain));
  
            {
                WindowsPrincipal principal = (WindowsPrincipal)User;
                UserPrincipal user = UserPrincipal.FindByIdentity(context, principal.Identity.Name);
               
                fname = user.GivenName;
                lname = user.Surname;
                fullname = user.Name;
                Response.Write(fname + "  " + lname)

            }     

    }

    private string GetHTTPContext()
    {
        return(HttpContext.Current.User.Identity.Name);
    }

All replies (5)

Monday, May 5, 2014 11:59 PM

See if this works:

Add a reference to System.DirectoryServices.AccountManagement

Add a using statement:

  using System.DirectoryServices.AccountManagement;

or use the full reference:

  System.DirectoryServices.AccountManagement.PrincipalContext

Mike Smith TechTrainingNotes.blogspot.com


Tuesday, May 6, 2014 3:03 PM

I was able to fix the error by importing the namespaces and including the assemblies. That got me past the 'PrincipalContext' error.

Now I am getting an error on the line 'UserPrincipal user =...':

An error occurred during the
compilation of the requested file, or one of its dependencies. The name
'context1' does not exist in the current context

Here is the code:

            using( PrincipalContext context1 = new PrincipalContext(ContextType.Domain)); 

            {
                WindowsPrincipal principal = (WindowsPrincipal)User;
                UserPrincipal user = UserPrincipal.FindByIdentity(context1, principal.Identity.Name);

            }


Wednesday, May 7, 2014 1:37 AM

Where is variable "context1" defined?

Mike Smith TechTrainingNotes.blogspot.com


Wednesday, May 7, 2014 1:52 AM

Hi ,

Can you try by removing the ';' at the end of using statement like below,

using( PrincipalContext context1 = new PrincipalContext(ContextType.Domain))

            {
                 WindowsPrincipal principal = (WindowsPrincipal)User;
                 UserPrincipal user = UserPrincipal.FindByIdentity(context1, principal.Identity.Name);

            }

Regards,

subhash reddy


Wednesday, May 7, 2014 4:25 PM

I removed the ";" at the end of the line. Now I get an 'unexpected error has occurred".

I do not define context1 anywhere else. I thought the command itself defined it.