0
votes

I've installed a fresh new version of Kentico v12 and i'm using the basic goat template.

I would like to be able to synchronize the creation of users and updating of personal informations of those users in the frontend application with a SAP webservice.

I've added a new custom field in the user object "SAPID" and created a connector to manage the synchronization with SAP webservices.

Here is my poc code:

public class CMSIntegrationConnector : BaseIntegrationConnector
{
    /// <summary>
    /// Initializes the connector name.
    /// </summary>
    public override void Init()
    {
        // Initializes the connector name (must match the code name of the connector object in the system)
        // GetType().Name uses the name of the class as the ConnectorName
        ConnectorName = GetType().Name;
         SubscribeToObjects(TaskProcessTypeEnum.AsyncSimple, PredefinedObjectType.USER);
    }
    public override IntegrationProcessResultEnum ProcessInternalTaskAsync(GeneralizedInfo infoObj, TranslationHelper translations, TaskTypeEnum taskType, TaskDataTypeEnum dataType, string siteName, out string errorMessage)
    {
        try
        {
            if (siteName == "DancingGoat")
            {
                if (infoObj.TypeInfo.ObjectType == PredefinedObjectType.USER.ToString())
                {
                    if (taskType == TaskTypeEnum.CreateObject)
                    {
                        EventLogProvider.LogInformation("Connector", "CreateUser", "User created on SAP !!!!!");
                        UserInfo user = infoObj.MainObject as UserInfo;
                        // Call SAP webservice
                        user.SetValue("SAPID", Guid.NewGuid());
                        UserInfoProvider.SetUserInfo(user);
                    }
                    else if (taskType == TaskTypeEnum.UpdateObject)
                    {
                        EventLogProvider.LogInformation("Connector", "CreateUser", "User updated on SAP !!!!!");
                        // Call SAP webservice
                    }
                }
            }
        }
        catch (Exception ex)
        {
            EventLogProvider.LogException("Connector", "CreateUser", ex);
            errorMessage = ex.Message;
            return IntegrationProcessResultEnum.Error;
        }
        errorMessage = null;
        return IntegrationProcessResultEnum.OK;
    }
}

Here is a dump of the values of parameters I get when I debug on a createobject event: enter image description here

I have 2 issues.

  • Why the parameter sitename is null ?
  • Why it's executed 3 times successively on each CreateObject event ?

I've checked this post: Kentico 12 DancingGoat MVC SiteName is empty or null

Adding "localhost" in the domain alias of the site didn't work.

Thank you by advance !

1
I believe the sitename is not available because its "async" synchronization and thus context of the site is lost. If you try to use "sync" you should see sitename as well as other context properties. You say its executed 3 times, but did you check what type of object that is? Is it identical object? It could be that related objects are created along with your main one. - Enn
Thank you for your quick answer ! I think I found out why, it comes from the instruction "UserInfoProvider.SetUserInfo(user);".... Do you know if there is a way to explicitly say "do not emit an event for the next instruction" ? When a new customer creates an account I would like to update the user info with it's SAP identifier :x - Aliz
Is it you, who is using this API directly, or is it call from KenticoAPI? Either way, CMSActionContext class can serve as a wrapper which you can use and set LogTasks property (or similar name) to false in order to suppress task creation. For Kentico API, this could be adjusted with customized provider, but I would be still interested to see where call is coming from. - Michal Samuhel
I'm using IntegrationBus, my connector is executed by Kentico on each event on "cms.user" objects, thank you for the clue about "CMSActionContext", i'll investigate on it ! - Aliz

1 Answers

2
votes

With the comment of Enn I understood that my problem came from this instruction "UserInfoProvider.SetUserInfo(user);" I subscribed to apply a logic on any new User objects and update it again in the logic, that's why I it was executed more than once.

To solve it, I applied the proposition of Michal

using (CMSActionContext context = new CMSActionContext())
{
    context.LogWebFarmTasks = false;
    context.LogEvents = false;
    context.LogExport = false;
    context.LogIntegration = false;
    context.LogSynchronization = false;

    UserInfo user = infoObj.MainObject as UserInfo;
    user.SetValue("SAPID", Guid.NewGuid());
    UserInfoProvider.SetUserInfo(user);
}

Thank you !