0

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.

Ronald
  • 33
  • 1
  • 7

3 Answers3

1

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);
     } 
} 
Vlad Iobagiu
  • 4,118
  • 3
  • 13
  • 22
  • Thanks for the quick response. I tried this, but it didn't work for me. I've updated the question, because i realized it wasn't complete. – Ronald Mar 10 '15 at 08:21
0

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);
                }
            }
Vicent Galiana
  • 328
  • 4
  • 15
  • Great note! That will be the idea eventually, but meanwhile, to test my code, i want to get a user based on a username, but this doesn't work either. Strangely my custom profile is not filled. var currentUserCustomProfile = currentUser.Profile as UserProfile; has no values, but when I check it with the same user but from the Sitecore.Context.User.Profile, it does work.. Any thoughts on how that's possible. – Ronald Mar 10 '15 at 12:04
  • Moving the var currentuser =... into the using of the new securityenabler maybe? – Vicent Galiana Mar 10 '15 at 12:29
  • Didn't work either. Somehow my Custom properties are not (re)loaded. :( – Ronald Mar 10 '15 at 12:46
  • Could you provide a bit more of context, please? When are you loading the user profile? during the dispatch pipeline? send email? on the site? Which version of ECM? – Vicent Galiana Mar 10 '15 at 13:42
  • At this moment i'm not using the ECM just yet. It doesn't work when i'm loading the component in my site as well. Not in page-editor mode, not in a "normal" view – Ronald Mar 10 '15 at 14:55
0

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";
Ronald
  • 33
  • 1
  • 7