How to get tcmid of currently logged user in Tridion?

前端 未结 2 574
终归单人心
终归单人心 2021-01-29 12:55
    private void Subscribe()
    {
        EventSystem.Subscribe(GetInfo, EventPhases.Initiated);
    }

    public void GetInfo(User user, Lo         


        
相关标签:
2条回答
  • 2021-01-29 13:31

    If you want to get the TCM URI of the current user, you can do so in a number of ways.

    I would recommend one of these:

    1. Using the Core Service, call GetCurrentUser and read the Id property.
    2. Using TOM.NET, read the User.Id property of the current Session.

    It looks like you want #2 in this case as your code is in the event system.

    0 讨论(0)
  • 2021-01-29 13:42

    The event you were initially subscribing to is the processed phase of any identifiable object with any of its actions, that will trigger basically on every transaction happening in the SDL Tridion CMS, so it won't give you any indication of when a user logs in (it's basically everything which happens all the time).

    Probably one of the first things which is happening after a user logs in, is that its user info and application data is read. So what you should try is something along the lines of:

    private void Subscribe()
    {
        EventSystem.Subscribe<User, LoadEventArgs>(GetInfo, EventPhases.Initiated);
    }
    
    public void GetInfo(User user, LoadEventArgs args, EventPhases phase)
    {
        TcmUri id = user.Id;
        string name = user.Title;
    }
    

    But do keep in mind that this will also be triggered by other actions, things like viewing history, checking publish transactions and possibly a lot more. I don't know how you can distinguish this action to be part of a user login, since there isn't an event triggered specifically for that.

    You might want to check out if you can find anything specific for a login in the LoadEventArgs for instance in its ContextVariables, EventStack, FormerLoadState or LoadFlags.

    Edit:

    Please note that the Event System is running inside the SDL Tridion core, so you won't ever see a console window popup from anywhere. If you want to log information, you can include the following using statement:

    using Tridion.Logging;
    

    After adding a reference to the Tridion.Logging.dll which you can find in your ..\Tridion\bin\client directory. Then you can use the following logging statement in your code:

    Logger.Write("message", "name", LoggingCategory.General, TraceEventType.Information); 
    

    Which you will find back in your Tridion Event log (provided you have set the logging level to show information messages too).

    But probably the best option here is to just debug your event system, so you can directly inspect your object when the event is triggered. Here you can find a nice blog article about how to setup debugging of your event system.

    0 讨论(0)
提交回复
热议问题