Access CurrentUser Container Parts

Topics: Core, General
Mar 11, 2015 at 9:46 PM
Hello all,

In my cshtml files, I can access the username of the currently logged in user using:
@this.WorkContext.CurrentUser.UserName
In my Orchard app, the User content item has a Containable part. I also have a Group content item, which has a Container part.

Every user is a member of a group, say Group A or Group B.

How can I access this group value from cshtml files? I want to output which group the logged in user belongs to in the frontend.

Many thanks.
Mar 13, 2015 at 5:13 AM
First, you need to add to the User type a Common part. See the CommomPartRecord table that owns a Container_Id column

Then you can use something like that
var containerId = 0;
var userPart = WorkContext.CurrentUser;
if (userPart.Has<CommonPart>() && userPart.As<CommonPart>().Container != null) {
    containerId = userPart.As<CommonPart>().Container.Id;
}
Then you can query your container and use its specific properties (e.g group name)

Best
Mar 13, 2015 at 3:45 PM
Edited Mar 13, 2015 at 3:45 PM
Thanks @jtkech for point me in the right direction. Currently, I'm doing this:
@{
    dynamic user = WorkContext.CurrentUser.ContentItem;
}

@user.CommonPart.Container.Id
And it's correctly printing out the Id.

Now, how do I access the Title Part from that Id? That will give me the group name.
Mar 13, 2015 at 10:10 PM
This works
@using Orchard.ContentManagement;
@using Orchard.Core.Title.Models;
@using Orchard.Core.Common.Models;
...

    ....
    var containerTitle = "";
    dynamic container = null;
    var userPart = WorkContext.CurrentUser;
    if (userPart.Has<CommonPart>() && userPart.As<CommonPart>().Container != null) {
        container = userPart.As<CommonPart>().Container;
        containerTitle = ((ContentItem)container).As<TitlePart>().Title;
    }
    ....
Best
Mar 14, 2015 at 8:43 PM
It sure does work @jktech, thank you.

Now, finally, if I wanted to access the Content TYpe of the Container part?

For example, if Users were contained in either Groups or Teams content types, how do I access these?
Mar 14, 2015 at 10:34 PM
    ...
    container = userPart.As<CommonPart>().Container;
    containerTitle = ((ContentItem)container).As<TitlePart>().Title;
    containerContentType = container.ContentType;
    ...
Best
Mar 20, 2015 at 6:00 PM
Fantastic, thanks @jktech.
Mar 20, 2015 at 6:38 PM
Edited Mar 20, 2015 at 6:38 PM
@Makedonian, @jktech's solution helped you out, but it's not clear to me from the different parts how this was done. Could you post a complete code example, or do I just piece together the three code examples he provided?

Thanks.
Mar 24, 2015 at 2:51 PM
Sure @remesq

First step was to copy the User.csthml file located in Orchard.Web -> Core -> Shapes over to Orchard.Web -> Themes -> YOUR THEME -> Views

This is a crucial first step often omitted from other discussions.

Second, you use the following directives in the cshtml file where you are running the user access code:
@using Orchard.ContentManagement;
@using Orchard.Core.Title.Models;
@using Orchard.Core.Common.Models;
Finally, this is the code that allows you to access user parts:
@{
    dynamic user = WorkContext.CurrentUser.ContentItem;
}
@{
    var containerTitle = "";
    var containerContentType = "";
    dynamic container = null;
    var userPart = WorkContext.CurrentUser;

    if (userPart.Has<CommonPart>() && userPart.As<CommonPart>().Container != null)
    {
        container = userPart.As<CommonPart>().Container;
        containerTitle = ((ContentItem)container).As<TitlePart>().Title;
        containerContentType = container.ContentType;
    }
}
so this is what gets printed...
@{
                        // hopefully everone has user first name, but otherwise fallback on username
                        var printUser = "";
                        if (user.UserDetails.FirstName.Value != null)
                        {
                            printUser = user.UserDetails.FirstName.Value;
                        }
                        else
                        {
                            printUser = WorkContext.CurrentUser.UserName;
                        }
                        @printUser // prints custom FirstName field captured at user registration, or lacking this prints username
@containerTitle // prints the title part of the container item for user, for example "Kickass Group"
@containerContentType // prints the type of the container, for example "Group", "Team", "Squad"
Lastly, it's worth noting that Im also using the contrib profile module to allow users to edit their profiles.

Thanks, hope this helps. If it doesn't, I'm around the forum.