Ronald
Ronald

Reputation: 33

Get Sitecore Custom Profile Properties

So I've been working Sitecore for a while now, and for sending an email through the e-mailcampaignmanager I've set up a module which should load certain items based on fields (user-filled interests) in the Custom Profile Properties.

Getting the properties from the Sitecore.Context.User.Profile works great (so when i'm logged in as the user, it works) but when i try to get it like var currentUser = Sitecore.Security.Accounts.User.FromName(Request["ec_recipient"], true); (or replacing Request["ec_recipient"] with an existing user name), all my custom properties have no value. I've tried currentUser.Profile.Reload() and currentUser.Profile.Initialize("username", true) but neither seem to work.

UPDATE

I forgot to mention that i've added an overload for the userprofile as below:

public class UserProfile : Sitecore.Security.UserProfile
{
   /// <summary>
    /// Gets or sets the interests.
    /// </summary>
    /// <value>
    /// The interests.
    /// </value>
    public IList<ID> Interests
    {
        get
        {
            return string.IsNullOrWhiteSpace(this.interests)
                ? new List<ID>()
                : this.interests
                    .Split(',')
                    .Where(ID.IsID)
                    .Select(g => new ID(g))
                    .ToList();
        }

        set
        {
            this.interests= string.Join(",", value.Select(g => g.ToString()));
        }
    }

    /// <summary>
    /// Gets or sets backingfield for the interests.
    /// </summary>
    /// <value>
    /// The sectors.
    /// </value>
    private string interests
    {
        get
        {
            return this.GetCustomProperty("Interests");
        }

        set
        {
            this.SetCustomProperty("Interests", value);
        }
    }
}

Also, i've tried the following (as mentioned in the comments, with still an empty string as result)

var interests = new List<ID>();

        using (new SecurityEnabler())
        {
            var currentUser = Sitecore.Security.Accounts.User.FromName(username, true);

            using (new Sitecore.Security.Accounts.UserSwitcher(currentUser))
            {
                var userprofile = Sitecore.Context.User.Profile as UserProfile;

                interests.AddRange(userprofile.Interests);
            }
        }

The custom user profile is defined in the Core Database in: /sitecore/templates/System/Security/Custom User Any thoughts/help on this topic would be greatly appreciated.

Upvotes: 0

Views: 3592

Answers (3)

Ronald
Ronald

Reputation: 33

So apparantly, even though the user's domain in the usermanager was "Emailcampaignmanager", i needed to use the "extranet\"-domain.

string testName = @"Emailcampaign\example_at_gmail_dot_com";

should have been

string testName = @"extranet\example_at_gmail_dot_com";

Upvotes: 0

Vicent Galiana
Vicent Galiana

Reputation: 328

If you're executing your code during the dispatch newsletter pipeline, it won't work because the "security is disabled". I experienced the same issue and explained it here

You need to enable the security again in order to be able to do your checks. It could be something like this:

 string testName = @"Emailcampaign\example_at_gmail_dot_com";
            //Request["ec_recipient"]
            var currentUser = Sitecore.Security.Accounts.User.FromName(testName, true);

            var interests = new List<ID>();
            using (new SecurityEnabler())
            {
                using (new Sitecore.Security.Accounts.UserSwitcher(currentUser))
                {
                    var w = currentUser.Profile.GetCustomProperty("Sectors");

                    var currentUserCustomProfile = currentUser.Profile as UserProfile;
                    currentUserCustomProfile.Initialize(testName, true);
                    currentUserCustomProfile.Reload();

                    interests.AddRange(currentUserCustomProfile.Interests);
                }
            }

Upvotes: 0

Vlad Iobagiu
Vlad Iobagiu

Reputation: 4118

I tried like and it works for me:

 string domainUser = @"domain\user"; 
 if (Sitecore.Security.Accounts.User.Exists(domainUser)) 
  { 
     Sitecore.Security.Accounts.User user = 
     Sitecore.Security.Accounts.User.FromName(domainUser,false); 

       using (new Sitecore.Security.Accounts.UserSwitcher(user)) 
     { 
       var property=user.Profile.GetCustomProperty("propertyname);
     } 
} 

Upvotes: 1

Related Questions