locked
Get current user properties from Active directory RRS feed

  • Question

  • HI

    How can i get the properties(department) from Active directory of Current user   programatically  in sharepoint?

     

    Thanks and Regards,

    Vinay Kumar

    Monday, April 12, 2010 12:58 AM

Answers

  • Try this, hope this helps you

       SPSite _site = SPContext.Current.Site;
       ServerContext serverContext = ServerContext.GetContext(_site);
       UserProfileManager myUserProfile = new UserProfileManager(serverContext);
       UserProfile currentUserUserProfile = myUserProfile .GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

       string departmentName = (string)currentUserUserProfile["department"].Value;
       _site.RootWeb.Dispose();
       _site.Dispose();


    Ashish Kanoongo, MCP, MCSD, MCTS
    Monday, April 12, 2010 12:59 AM

All replies

  • Try this, hope this helps you

       SPSite _site = SPContext.Current.Site;
       ServerContext serverContext = ServerContext.GetContext(_site);
       UserProfileManager myUserProfile = new UserProfileManager(serverContext);
       UserProfile currentUserUserProfile = myUserProfile .GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

       string departmentName = (string)currentUserUserProfile["department"].Value;
       _site.RootWeb.Dispose();
       _site.Dispose();


    Ashish Kanoongo, MCP, MCSD, MCTS
    Monday, April 12, 2010 12:59 AM
  • Hi

     

    This gets the information from Sharepoint profile or Active Directory?

     

    i am having a property "employeeID" in Active directory properties. But when i tried following code

     

      SPSite _site = SPContext.Current.Site;

                    ServerContext serverContext = ServerContext.GetContext(_site);

                    Microsoft.Office.Server.UserProfiles.UserProfileManager myUserProfile = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serverContext);

                    Microsoft.Office.Server.UserProfiles.UserProfile currentUserUserProfile = myUserProfile.GetUserProfile(System.Web.HttpContext.Current.User.Identity.Name);

     

     

                    strDepartment = (string)currentUserUserProfile["department"].Value;

                    string EmployeeID = (string)currentUserUserProfile["employeeID"].Value;

     

    The department name is
    The error is Microsoft.Office.Server.UserProfiles.UserNotFoundException: User Not Found: Could not load profile data from the database. at Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReader myReader, Boolean bFirstRead, Boolean bWssId) at Microsoft.Office.Server.UserProfiles.UserProfile.Load(SqlDataReader myReader) at Microsoft.Office.Server.UserProfiles.UserProfile.RetrieveUser(String strAcct, Guid gAcct, Byte[] bSid, Nullable`1 recordId, Boolean doNotResolveToMasterAccount) at Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct, Boolean doNotResolveToMasterAccount, Boolean forceUserIsSelf) at Microsoft.Office.Server.UserProfiles.UserProfile..ctor(UserProfileManager objManager, String strAcct) at Microsoft.Office.Server.UserProfiles.UserProfileManager.GetUserProfile(String strAccountName)

     

     

    Please let me know if i miss anything.

     

    Monday, April 12, 2010 4:09 AM
  • Ashish deifnitely answered your first question, the second one is related to the fact the current user DOES NOT HAVE A PROFILE.

    Chris


    Chris Givens CEO, Architecting Connected Systems Blog Twitter
    Monday, April 12, 2010 4:16 AM
  • current user DOES NOT HAVE A PROFILE --- is this related to sharepoint profile or Active Directory ?
    Monday, April 12, 2010 4:20 AM
  • Have you configured Profile Import from Active Directory to SharePoint. The code what Ashish has written, will work but you have to have AD user profiles imported to SharePoint. -Ved
    Monday, April 12, 2010 4:47 AM
  • Hi

     

    i didn't Imported Profiles from Active Directory to share point. Is there any way i can get directly from Active Directory without importing to share point profiles.

     

    Thanks and regards

    Vinay kumar

    Monday, April 12, 2010 5:39 AM
  • Hi

     

    i didn't Imported Profiles from Active Directory to share point. Is there any way i can get directly from Active Directory without importing to share point profiles.

     

    Thanks and regards

    Vinay kumar

    Monday, April 12, 2010 5:39 AM