Tokens in cshtml files

Topics: Core, General
Mar 6, 2015 at 7:34 PM
Edited Mar 9, 2015 at 5:58 PM
Hello all,

I'd like to use user tokens in some of my cshtml files, but I'm having a difficult time getting it working.
 #{User.Name}
works in body parts, but not in cshtml layout files. Do I need a using directive for it to work?

Thanks for your time!
Mar 6, 2015 at 8:13 PM
I think a normal .*cshtml file gets not through the Tokenizer - so you can't do that. I think a better approach is to write an custom widget (if your needs are a little bit more complex) or you use
@this.WorkContext.CurrentUser.UserName
in C# Code in the cshtml directly.
Mar 9, 2015 at 12:30 AM
Makedonian wrote:
Hello all,

I'd like to use user tokens in some of my cshtml files, but I'm having a difficult time getting it working.

#{User.Name} works in body parts, but not in cshtml layout files. Do I need a using directive for it to work?

Thanks for your time!
@Makedonian
How are using the #{} in a body part? I have tried both #{Content.Fields.MyForm.FirstName} and #{Request.Content.Fields.MyForm.FirstName} from a page that I am redirecting to from within Custom Forms. The page is a ~/Success page.
Mar 9, 2015 at 1:34 AM
Edited Mar 9, 2015 at 1:34 AM
remesq wrote:
Makedonian wrote:
Hello all,

I'd like to use user tokens in some of my cshtml files, but I'm having a difficult time getting it working.

#{User.Name} works in body parts, but not in cshtml layout files. Do I need a using directive for it to work?

Thanks for your time!
@Makedonian
How are using the #{} in a body part? I have tried both #{Content.Fields.MyForm.FirstName} and #{Request.Content.Fields.MyForm.FirstName} from a page that I am redirecting to from within Custom Forms. The page is a ~/Success page.
@remesq
You must download enable the Tokens Html Filter module first, then you should be able to use that syntax.
Mar 9, 2015 at 1:41 AM
benschi11 wrote:
I think a normal .*cshtml file gets not through the Tokenizer - so you can't do that. I think a better approach is to write an custom widget (if your needs are a little bit more complex) or you use
@this.WorkContext.CurrentUser.UserName
in C# Code in the cshtml directly.
@benschi11
Thanks, works great!

I also have the Profile module enabled, which "Adds a set of functionalities to display and edit users' profile". I have created a First Name and Last Name field that is attached to the User content type. I have also made the User content type "Containable", so that users can be members of another Content Type, called Groups.

How can I reach and output these custom fields in cshtml files?
Mar 9, 2015 at 7:07 AM
Hi! If you added them as fields they should be accessible through
@this.WorkContext.CurrentUser.User.FirstName
But I am not 100% and I cannot test it right now, but you can give it a shot.
Mar 9, 2015 at 3:23 PM
Edited Mar 9, 2015 at 5:57 PM
@benschi11

I had tried that, it doesn't work.

To clarify: I've created a "User Full Name" part, with 2 fields in it: "First Name" and "Last Name", and have attached the part to the User content type. I can capture this information in the front end through a form, by using:
@Html.TextBox("User Full Name.FirstName.Value") 
@Html.TextBox("User Full Name.LastName.Value")
I just haven't figured out the syntax yet to reach it in csthml files.

Also, I have attached a Containable part to the User content type, and I've attached a Container part to the custom Group content type. Therefore, Users can belong to Groups. This works well, I can edit which group users are "members of", but I can't reach this info from csthml files either.

Thanks for your great help!
Mar 9, 2015 at 4:34 PM
Ok. When you created a own part for it you should be able to access it through
@this.WorkContext.CurrentUser.PartName.FieldName
With the correct PartName and FieldName.
Mar 9, 2015 at 5:42 PM
Edited Mar 9, 2015 at 5:57 PM
@benschi11

So my parts are called "User Full Name" (note spaces) and my fields "FirstName" and "LastName".

I have tried, to no avail:
@this.WorkContext.CurrentUser.UserFullName.FirstName

@this.WorkContext.CurrentUser.User Full Name.FirstName

@this.WorkContext.CurrentUser.User Full Name.FirstName

@this.WorkContext.CurrentUser.User_Full_Name.FirstName
As a test, I've added a "GUID" field straight to the User content type (no parts), and I still can't retrieve it through:

@this.WorkContext.CurrentUser.GUID
Any ideas?
Mar 9, 2015 at 5:52 PM
Hi!
I have to dig into your problem with the part, because I thought there is a 'technical name' for every part, but I am not so sure anymore. I need to debug it.

For the case with the fields:
Technically there are no fields directly attached to a contentType. If you add a field Orchard creates an Part with the same name as the ContentType and if you want to access this field you have to use ContentItem.PartName.FieldName.

In your case try:
@this.WorkContext.CurrentUser.User.Guid
I will try to invest your problem when I am at home in the evening.
Mar 9, 2015 at 5:56 PM
Edited Mar 9, 2015 at 5:59 PM
Thanks @benschi11, please let me know what you find.

I think it's something related to this issue:

http://stackoverflow.com/questions/18786310/orchards-context-data-so-close-yet-so-far

I have also tried:
@this.WorkContext.CurrentUser.As<UserPart>().Record.Guid
No luck.
Mar 9, 2015 at 6:01 PM
Ahm, according to your Part. On the part creation page it says: "Use no spaces or special characters. For example, "MyCustomPart" ". So you shouldn't have a part with the name of yours.
Anyway I will take a look in the evening maybe I can find something out.
Mar 9, 2015 at 6:15 PM
@benschi11

I just tried creating a new part, "UserDetails", no spaces, and added a FirstName fields to it. Still same error.

In case it helps, exception detail from VS is :

'Orchard.Security.IUser' does not contain a definition for 'UserDetails' and no extension method 'UserDetails' accepting a first argument of type 'Orchard.Security.IUser' could be found (are you missing a using directive or an assembly reference?)
Mar 10, 2015 at 9:53 AM
Edited Mar 10, 2015 at 10:02 AM
How about:
var currentUser = WorkContext.CurrentUser;
var firstName = currentUser != null ? currentUser.ContentItem.UserFullName.FirstName.Value : string.Empty;
Notice the ContentItem access, the parts are attached to the ContentItem of the User, not to the CurrentUser directly. Also, this fails if the part and/or field is not attached, so you should probably check for that.
Mar 10, 2015 at 6:50 PM
I will try your proposed solution in a few hours @sanderg, thanks.

As a side note, I also have to access the value of the Containable part. How is this done?

Each user is contained within a group, the group content type has a container part, the user content type has a containable part. I need the value of the current user's group.
Mar 13, 2015 at 3:49 PM
@benschi11 and @sanderg

Thanks both for your help.

I did two things.

First, I changed the name of my part to UserDetails, without any space. This part has fields like GUID and FirstName.

Secondly, I copied over the User.cshtml part from the core to my view. I read on a comment somewhere about this, and it's crucial.

Now, this code works for me:
@{
    dynamic user = WorkContext.CurrentUser.ContentItem;
}

@user.UserDetails.GUID.Value
@user.UserDetails.FirstName.Value
Thanks again!