Description:
I would like to display the current logged on user in the header bar of the new web UI. How to accomplish this task?
Answer:
To accomplish your task, consider one of the following solutions depending on your business requirements and XAF version:
For 16.1.6+ and New Web UI
Use the CurrentUserDisplayImageAttribute and ModelApplicationWeb.CurrentUserDisplayMode features to achieve the result shown below:
Refer to the XCRM.Web demo app or corresponding product documentation for more details.
For older versions or Classic Web UI
1. Create and register a custom template content for the main form (use the Default Template Content or Default Vertical Template Content items in the DevExpress Template Gallery) as per the How to: Customize an ASP.NET Template article and add a new table cell with the following code to the markup of the created *.ASCX file, between cells containing the logo and header menu:
ASPx...
<td>
<img src="Images/Logo.png" />
</td>
<td class="width100"></td>
<td><%= DevExpress.ExpressApp.SecuritySystem.CurrentUserName %></td>
<td>
<div id="xafHeaderMenu" class="xafHeaderMenu" style="float: right;">
...
</td>
...
2. You can customize the standard MyDetailsController class in the following manner (optionally, customize the corresponding navigation item):
Codeusing DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.ExpressApp.Security;
using DevExpress.ExpressApp.SystemModule;
namespace MainDemo.Module {
public class WindowController1 : WindowController {
public WindowController1() {
TargetWindowType = WindowType.Main;
}
protected override void OnActivated() {
base.OnActivated();
string ActionItemId = "MyDetails";
MyDetailsController myDetailsController = Frame.GetController<MyDetailsController>();
if (myDetailsController != null) {
myDetailsController.Actions[ActionItemId].Caption = SecuritySystem.CurrentUserName;
}
//Optionally, customize the corresponding navigation item.
ShowNavigationItemController showNavigationItemController = Frame.GetController<ShowNavigationItemController>();
if (showNavigationItemController != null) {
showNavigationItemController.ShowNavigationItemAction.Items.Find(
ActionItemId, ChoiceActionItemFindType.Recursive, ChoiceActionItemFindTarget.Leaf
).Caption = SecuritySystem.CurrentUserName;
}
}
}
}
See Also:
Refer to the following help topics for more details on the demonstrated approaches:
How to: Customize an ASP.NET Template.eXpressApp Framework>Concepts>Extend Functionality>Customize Controllers and ActionsWebApplication > SwitchToNewStyle,
ASP.NET Web Application's Appearance
Thanks for the help
You're welcome. Should you need any further assistance, please let me know.
Starting with version 16.1.6, you can use the IModelApplicationWeb.CurrentUserDisplayMode property to show the current user display name (the value of the default property specified by the XafDefaultProperty attribute). Run the Model Editor for the ASP.NET-specific project. Focus the root node and set the CurrentUserDisplayMode property to CaptionAndImage or Caption. This property is not yet documented. The corresponding help topic will be published after the version 16.2 release.
Hi Konstantin,
I have follow the blog on http://dennisgaravsky.blogspot.co.id/2016/12/displaying-currently-logged-user-name.html
but it only show user name, no image, whats wrong ? not even after i reduce photo size (see the picture attached)
this is my security user class
using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
namespace ELS2018.Module.BusinessObjects
{
[DefaultProperty("UserName"), XafDisplayName("Limited User")]
[CurrentUserDisplayImage("Photo")]
//[ImageName("BO_Contact")]
//[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
//[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
//[Persistent("DatabaseTableName")]
// Specify more UI options using a declarative approach (https://documentation.devexpress.com/#eXpressAppFramework/CustomDocument112701).
public class SecurityUser : PermissionPolicyUser
{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument113146.aspx).
public SecurityUser(Session session)
: base(session)
{
}
private Branch company;
[Association("Branch-Users")]
public Branch Company
{
get { return company; }
set { SetPropertyValue("Company", ref company, value); }
}
private String userserver;
[XafDisplayName("Server")]
public String UserServer
{
get { return userserver; }
set { SetPropertyValue("UserServer", ref userserver, value); }
}
private String userdatabase;
[XafDisplayName("Database")]
public String UserDatabase
{
get { return userdatabase; }
set { SetPropertyValue("UserDatabase", ref userdatabase, value); }
}
public virtual MediaDataObject Photo { get; set; }
[Association("Users-Customers")]
public XPCollection<Customer> Customers
{
get { return GetCollection<Customer>("Customers"); }
}
public override void AfterConstruction()
{
base.AfterConstruction();
// Place your initialization code here (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument112834.aspx).
}
//private string _PersistentProperty;
//[XafDisplayName("My display name"), ToolTip("My hint message")]
//[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
//[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
//public string PersistentProperty {
// get { return _PersistentProperty; }
// set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
//}
//[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
//public void ActionMethod() {
// // Trigger a custom business logic for the current record in the UI (https://documentation.devexpress.com/eXpressAppFramework/CustomDocument112619.aspx).
// this.PersistentProperty = "Paid";
//}
}
}
Hello,
Please run the Model Editor for the ASP.NET module or application project and ensure that the IModelApplicationWeb.CurrentUserDisplayMode property is set to CaptionAndImage.
Additionally, if you store the model differences in the database, ensure that this setting is not overridden accidentally at the database level. If this does not help, would you please create a separate ticket and attach a sample project that shows this issue?
PS: the virtual modifier is not required for the Photo property in an XPO persistent class. An example from the blog you are referring to is for Entity Framework.
I did choose CaptionAndImage, but its not working. I'm sure its not overridden at database level.
Would you please create a separate ticket for me ?
Thanks
@Ade:
I've created a separate ticket on your behalf (T513951: Solution provided in T321618 doesn't work in my project). It has been placed in our processing queue and will be answered shortly.