Profile name instead of username

Topics: Customizing Orchard
Apr 28, 2011 at 6:39 PM

I'm using the Profile module and I've been trying to figure out how to do this by going through the code, but I'm not having any luck and could use some suggestions if anyone has one.

Currently, the User.cshtml View is showing the username in my header using: @WorkContext.CurrentUser.UserName.

I have created a Profile field called FirstName to hold the user's first name. I would like to display the FirstName field from the Profile if it exists, else display the UserName. I can't seem to figure out how to get to the FirstName Profile field though.

Has anyone used the Profile module to retrieve a property and can help? Thanks.

Apr 28, 2011 at 8:13 PM

You can access it thru the part name and then the field name. So I assume it's ProfilePart (which I think it is): @WorkContext.CurrentUser.ProfilePart.FirstName

Coordinator
Apr 28, 2011 at 8:13 PM

First you need to override the current Shape (not View) into your Theme. To do so, copy the Core\Shapes\Views\User.cshtml file into the folder Themes\{YourTheme}\.
Now you can change the behavior inside your theme, without altering the original source code which might be used by other tenants running on the same application, or when you update the module with new releases.

Then, edit the content of the file, and insert this declaration at the beginning

@{
    dynamic user = WorkContext.CurrentUser.ContentItem;
}

 The variable user contains a dynamic representation of the User Content Item. With that you can access parts and fields dynamically. If user is not null, you can use it:

(string)user.ProfilePart.FirstName.Value

 


May 4, 2011 at 1:19 AM
Edited May 4, 2011 at 1:25 AM

I forgot to turn on notify for this topic so I didn't realize you had replied. Thanks a bunch...that works perfectly! Although I had to put the dynamic user code within my @if (Request.IsAuthenticated) section, otherwise it took an error, which is fine because only authenticated users can setup their profile.

I also couldn't seem to get it to work with that file under my theme (in the proper folder structure). It wouldn't override the original shape file. The only way I can get it to work is to replace the original shape. Is there anything else I need to do to override a Core shape?

Thanks again!

May 4, 2011 at 1:32 AM

Got it...I had to put it in the Views folder under my theme, not Core/Shapes/Views

May 19, 2012 at 11:15 PM

Hello,

Ok, I'm using the Bootstrap theme. Am attempting to show a Display Name instead of the User Name. I added FirstName & LastName to the ProfilePart, I want to combine them into a DisplayName and show that where the username is displayed. I am also hoping to add a link in the Dropdown that says Profile or such... Any ideas... A little beyond me... My project site is http://project3.tjnhost.com...

Regards,

TJ Havens

May 20, 2012 at 1:05 AM
Hey TJ...totally possible. My daughter is leaving in a couple hours for Washington DC and New York for a week, so give me a few and I'll send you the code.

The basics are creating a dynamic user = WorkContext.CurrentUser.ContentPart then accessing the ProfilePart using the dynamic User. It's really quite simple. Just need to make sure you check for a null user first to make sure someone is logged in.

All of this code will go in User.cshtml in the theme's View folder.

I'll send you a code snippet in a while.

Thanks for using the theme!



On May 19, 2012, at 3:15 PM, xotj123 <notifications@codeplex.com> wrote:

From: xotj123

Hello,

Ok, I'm using the Bootstrap theme. Am attempting to show a Display Name instead of the User Name. I added FirstName & LastName to the ProfilePart, I want to combine them into a DisplayName and show that where the username is displayed. I am also hoping to add a link in the Dropdown that says Profile or such... Any ideas... A little beyond me... My project site is http://project3.tjnhost.com...

Regards,

TJ Havens

May 20, 2012 at 3:31 AM

awesome, thanks...

May 21, 2012 at 6:37 PM
Hi TJ, Sorry for the delay...I realized after I sent you the last email that the code was on my server at work, not on my laptop. Here is what you're looking for:
@if (WorkContext.CurrentUser != null) { dynamic user = WorkContext.CurrentUser.ContentItem; List roles = user.UserRolesPart.Roles; } else {
  • @Html.ActionLink(T("Sign In").ToString(), "LogOn", new { Controller = "Account", Area = "Orchard.Users", ReturnUrl = (Request.QueryString["ReturnUrl"] ?? Request.RawUrl) })
}
My particular code here is checking to make sure that the FirstName value isn't null. If it is, it displays the Username instead. Let me know if you have any questions. On May 19, 2012, at 03:15 PM, xotj123 wrote: From: xotj123 Hello, Ok, I'm using the Bootstrap theme. Am attempting to show a Display Name instead of the User Name. I added FirstName & LastName to the ProfilePart, I want to combine them into a DisplayName and show that where the username is displayed. I am also hoping to add a link in the Dropdown that says Profile or such... Any ideas... A little beyond me... My project site is http://project3.tjnhost.com... Regards, TJ Havens Read the full discussion online. To add a post to this discussion, reply to this email (orchard@discussions.codeplex.com) To start a new discussion for this project, email orchard@discussions.codeplex.com You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com. Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com
Hi TJ,

Sorry for the delay...I realized after I sent you the last email that the code was on my server at work, not on my laptop. Here is what you're looking for:

@if (WorkContext.CurrentUser != null)
{
dynamic user = WorkContext.CurrentUser.ContentItem;
List<string> roles = user.UserRolesPart.Roles;

  • @Html.ActionLink(T("Edit Profile").ToString(), "Edit", new { Area = "Contrib.Profile", Controller = "Home" })
  • @if (AuthorizedFor(Orchard.Security.StandardPermissions.AccessAdminPanel))
    {
  • @Html.ActionLink(T("Dashboard").ToString(), "Index", new { Area = "Dashboard", Controller = "Admin" })
  • }
    }
    else
    {
  • @Html.ActionLink(T("Sign In").ToString(), "LogOn", new { Controller = "Account", Area = "Orchard.Users", ReturnUrl = (Request.QueryString["ReturnUrl"] ?? Request.RawUrl) })
    }


    My particular code here is checking to make sure that the FirstName value isn't null. If it is, it displays the Username instead. Let me know if you have any questions.

    On May 19, 2012, at 03:15 PM, xotj123 <notifications@codeplex.com> wrote:

    From: xotj123

    Hello,

    Ok, I'm using the Bootstrap theme. Am attempting to show a Display Name instead of the User Name. I added FirstName & LastName to the ProfilePart, I want to combine them into a DisplayName and show that where the username is displayed. I am also hoping to add a link in the Dropdown that says Profile or such... Any ideas... A little beyond me... My project site is http://project3.tjnhost.com...

    Regards,

    TJ Havens

    --Boundary_(ID_/Jr8TurjIMFvbb9Ln8eQnA)--
    May 21, 2012 at 7:07 PM

    Does that use the Drop Down from the bootstrap theme?

    May 21, 2012 at 7:20 PM

    ok, I got it to work, thanks alot man...

    Oct 22, 2012 at 11:03 PM

    I came to this thread via this blog post, and thought I would post his solution, since it was complete and helped me very quickly:

    var displayName = String.Empty;
    if(WorkContext.CurrentUser != null)
    {
      dynamic user = WorkContext.CurrentUser.ContentItem;
      if(user.ProfilePart != null && 
        user.ProfilePart.Has(typeof(object), "DisplayName") &&
        user.ProfilePart.DisplayName.Value is string)
      {
        displayName = user.ProfilePart.DisplayName.Value.Trim();
      }
      
      if(String.IsNullOrWhiteSpace(displayName))
        { displayName = WorkContext.CurrentUser.UserName; }
    }

    Coordinator
    Oct 22, 2012 at 11:06 PM

    For future releases I intend to make it a shape, so that any module can define how to display the username, and also use the Content Item metadata. This will be easier.

    Oct 23, 2012 at 12:10 AM

    That will be great Sebastien...I've really come to depend on the Profile module for holding a lot of custom information related to users. It's extremely flexible.