Search code examples
c#sitecoresitecore7.2

Sitecore: How to get the user profile item of currently logged in user


In sitecore:

If a user is logged in with user name "Addemo", How Can I get the User profile item which is under /sitecore/content/Intranet/User Profiles/A/Ad/addemo.

I tried with User.Current.Profile.ProfileItemId; But the ID I get with this is not the item Id of addemo (/User Profiles/A/Ad/addemo).


Solution

  • It looks like you are using Sitecore Intranet. I'm not 100% familiar with the details of that (mostly my coworkers that work with that part), but this should be what you are looking for:

    using Sitecore.Intranet.Profiles;
    using Sitecore.Intranet.Profiles.Providers;
    using Sitecore.Security.Accounts; 
    
    // ------------------------------
    
    var userName = User.Current.Name;
    var account = Account.FromName(userName, AccountType.User);
    
    var profileProvider = new UserProfileProvider(new Settings());
    var profile = profileProvider.GetProfile(account.LocalName.ToLower());
    
    var profileItem = profile.ProfileItem;
    

    I haven't tested this as I don't have a solution at hand, so let me know if something is a bit off.