How to get tcmid of currently logged user in Tridion?

故事扮演 提交于 2019-12-02 13:23:01

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!