将Sitecore社交个人资料更新到外部系统

时间:2015-05-22 01:43:17

标签: asp.net sitecore sitecore7.5 sitecore-social-connected

我们使用Sitecore社交网络版本3.0与Sitecore 7.5。在我们登录用户并查看存储在MongoDb中的配置文件信息的意义上,一切正常。但是,还需要将配置文件信息存储到单独的系统中。

我已成功通过点击以下社交模块活动来完成此操作:

<event name="social:connector:user:loggedin">
</event>
<event name="social:connector:user:socialprofileattached">
</event>

在这些事件中,我使用以下代码来读取MongoDb并尝试更新外部系统中的配置文件:

INetworkManager networkManager = ExecutingContext.Current.IoC.Get<INetworkManager>(new IParameter[0]);
ISocialProfileManager socialProfileManager = ExecutingContext.Current.IoC.Get<ISocialProfileManager>(new IParameter[0]);
string name = networkManager.GetNetwork(new IDIdentifier(ID.Parse(MembershipParameters.LinkedIn_NetworkId))).Name;
SocialProfile socialProfile = socialProfileManager.GetSocialProfile(Tracker.Current.Contact.ContactId.GetIdentifier(), name);

if (!socialProfile.IsEmpty && socialProfile.Fields.Any())
{
    MembershipUtil.UpdateLinkedInPofile(socialProfile);
}

问题是这只适用于第二次+登录尝试。在第一次尝试时,socialProfile.Fields计数始终为0.似乎代码过早被调用。

我真正需要的是事件或管道更新后mongo已更新,以便我可以检索它并更新外部系统。

关于如何实现这一点的任何其他建议显然也会受到欢迎。感谢。

2 个答案:

答案 0 :(得分:0)

根据我使用Sitecore使用MongoDB的经验,在会话结束并且某些数据存储在会话中之前,无法获得有关用户访问的完整详细信息。在会话结束时,数据被刷新到MongoDB中。

获取社交数据的更可靠方法可能是使用AggregationProcessor。您可以在此处理器中放置一些代码,以便将社交数据推送到外部系统。

namespace MyNamespace.Aggregation
{
    public class MyProcessor: AggregationProcessor
    {
      protected override void OnProcess(AggregationPipelineArgs args)
      {
          //your code
      }
   }
}

然后在配置文件中:

 <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
 <sitecore>
    <pipelines>
        <group groupName="analytics.aggregation">
            <pipelines>
                <interactions>
                    <processor type="Mynamespace.aggregation.MyProcessor, MyDll" />
                </interactions>
            </pipelines>
        </group>
    </pipelines>
</sitecore>

答案 1 :(得分:0)

原来我是在思考整个过程。在反编译器的帮助下,我意识到调用的登录方法接受Callback URL子布局参数。这是用户在登录后被重定向到的地方,因此我覆盖了登录按钮并向Callback网址添加了返回URL。然后在回调网址中,我调用了我的数据库更新方法。

登录代码已从Sitecore.Social.Client.Connector.Controls.LoginButtonBase.Login

反编译
protected void NewLogin(string networkName)
{
    var pageUrl = Request["pageUrl"];
    ILoginHelper loginHelper = ExecutingContext.Current.IoC.Get<ILoginHelper>(new IParameter[0]);
    Sublayout parent = this.Parent as Sublayout;
    NameValueCollection nameValueCollection = (parent != null ? WebUtil.ParseUrlParameters(parent.Parameters) : new NameValueCollection());
    Dictionary<string, object> dictionary = nameValueCollection.AllKeys.ToDictionary<string, string, object>((string key) => key, (string key) => nameValueCollection[key]);
    string callbackUrl = string.Empty;
    if (dictionary.ContainsKey("Callback URL") && !string.IsNullOrEmpty((string)dictionary["Callback URL"]))
    {
        try
        {
            try
            {
                callbackUrl = ExecutingContext.Current.IoC.Get<ILinkManager>(new IParameter[0]).GenerateLink(dictionary["Callback URL"].ToString(), string.Empty);
                if (string.IsNullOrEmpty(callbackUrl))
                {
                    ExecutingContext.Current.IoC.Get<ILogManager>(new IParameter[0]).LogMessage(string.Format("Could not parse the '{0}' link value. Context item ID: {1}", dictionary["Callback URL"], (Sitecore.Context.Item != null ? Sitecore.Context.Item.ID.ToString() : "null")), LogLevel.Error, this);
                }
                else if (!string.IsNullOrEmpty(callbackUrl) && !string.IsNullOrEmpty(pageUrl))
                {
                    callbackUrl += string.Format("?pageurl={0}", pageUrl);
                }
            }
            catch (Exception exception1)
            {
                Exception exception = exception1;
                ExecutingContext.Current.IoC.Get<ILogManager>(new IParameter[0]).LogMessage(string.Format("Could not parse the '{0}' link value. Context item ID: {1}", dictionary["Callback URL"], (Sitecore.Context.Item != null ? Sitecore.Context.Item.ID.ToString() : "null")), LogLevel.Error, this, exception);
            }
        }
        finally
        {
            dictionary.Remove("Callback URL");
        }
    }
    if (string.IsNullOrEmpty(callbackUrl))
    {
        callbackUrl = base.Request.Url.ToString();
    }
    loginHelper.Login(networkName, false, dictionary, callbackUrl);
}

这里有一个重要的注意事项。以下行中的第二个参数指示是否应异步更新配置文件。如果将此设置为true,则在您点击回调网址时,您可能仍无法使用该配置文件。将其设置为false可确保MongoDB记录中的UserProfile对象已更新,并且在回调页面上可供我使用。这自然会带来成本。

loginHelper.Login(networkName, false, dictionary, callbackUrl);

在回调网址登录页面中调用此方法:

INetworkManager networkManager = ExecutingContext.Current.IoC.Get<INetworkManager>(new IParameter[0]);
ISocialProfileManager socialProfileManager = ExecutingContext.Current.IoC.Get<ISocialProfileManager>(new IParameter[0]);
string name = networkManager.GetNetwork(new IDIdentifier(Sitecore.Data.ID.Parse(MembershipParameters.LinkedIn_NetworkId))).Name;
if (Tracker.Current != null)
{
    SocialProfile socialProfile =
        socialProfileManager.GetSocialProfile(Tracker.Current.Contact.ContactId.GetIdentifier(),
            name);

    if (!socialProfile.IsEmpty && socialProfile.Fields.Any())
    {
        MembershipUtil.UpdateLinkedInPofile(socialProfile);
    }
}