获取Sitecore自定义配置文件属性

时间:2015-03-10 06:49:38

标签: c# .net sitecore custom-properties

所以我现在已经在Sitecore工作了一段时间,并且通过电子邮件广告管理员发送电子邮件我已经设置了一个模块,该模块应该根据字段(用户填充的兴趣)加载某些项目在自定义配置文件属性中。

Sitecore.Context.User.Profile获取属性效果很好(所以当我以用户身份登录时,它可以正常工作)但是当我尝试将其设置为var currentUser = Sitecore.Security.Accounts.User.FromName(Request["ec_recipient"], true);时(或替换请求[ " ec_recipient"]具有现有用户名),我的所有自定义属性都没有值。我已经尝试currentUser.Profile.Reload()currentUser.Profile.Initialize("username", true),但似乎都没有效果。

更新

我忘了提到我为userprofile添加了一个重载,如下所示:

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);
        }
    }
}

此外,我已经尝试了以下内容(如评论中所述,结果仍为空字符串)

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);
            }
        }

自定义用户配置文件在以下核心数据库中定义:/ sitecore / templates / System / Security / Custom User 任何关于这个主题的想法/帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

我试过了,它对我有用:

 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);
     } 
} 

答案 1 :(得分:0)

如果您在调度简报管道中执行代码,它将无法正常工作,因为&#34;安全性已被禁用&#34;。我遇到了同样的问题并对其进行了解释here

您需要再次启用安全性才能进行检查。它可能是这样的:

 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);
                }
            }

答案 2 :(得分:0)

如此显而易见,即使用户管理器中的用户域名为“Emailcampaignmanager”,我也需要使用“extranet”域。

string testName = @"Emailcampaign\example_at_gmail_dot_com";

应该是

string testName = @"extranet\example_at_gmail_dot_com";
相关问题