Developer forum

Forum » Templates » Referencing custom fields in Razor template

Referencing custom fields in Razor template

Stephen Anthony Jackson
Stephen Anthony Jackson
Reply

Hello.

I have created a custom field "GroupDescription", systemname "AccessUser_GroupDescription", and I want to retrieve its value when rendering a page using the name of the group from the group class

I have been studying the page at https://doc.dynamicweb.com/template-tags/users/extranet/details/usermanagementuser-customfields and I just dont understand the documentation.

Can someone give me a concrete example of how to retrieve the value of a custom field, for a given user name?

Cheers

Steve


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Steve

Use this approach: https://doc.dynamicweb.com/template-tags/users/extranet/details/usermanagementuser-usercustomfieldsystemname

Like this for your custom field: @GetString("UserManagement:User.GroupDescription")

You do not specify if it is a user or a group or in which template - but if you are in a template where the user is rendered, the above tag should do.

BR Nicolai

 

 
Stephen Anthony Jackson
Stephen Anthony Jackson
Reply

Hei Nicolai.  I get the error   The name 'GetString' does not exist in the current context

If I modify this to Model.Item.GetString, then no error message is displayed, but neither is the content from the AccessUser_Group_Description field (Thats the system name in the Control Panel->Users->Custom Fields config)

(Trying to reference it by  @Model.Item.GetString("UserManagement:User.AccessUser_Group_Description")  )

 
Stephen Anthony Jackson
Stephen Anthony Jackson
Reply

For clarity, I am using this loop

 

   foreach (var group in Dynamicweb.Security.UserManagement.Group.GetGroups())
 
//And I want to get out customfields -> here <- somewhere
  {
 
 
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
This post has been marked as an answer

If you're getting the groups with the API, you can use the CustomFieldValues collection like so:

foreach (var group in Dynamicweb.Security.UserManagement.Group.GetGroups())
{
  var value = group.CustomFieldValues.FirstOrDefault(x => x.CustomField.SystemName == "YourSystemName")?.Value;
}

Hope this helps,

Imar

 

 

Votes for this answer: 1
 
Stephen Anthony Jackson
Stephen Anthony Jackson
Reply

Aha. I have to access it from a customfields collection. Thanks Imar, will try that.

 
Stephen Anthony Jackson
Stephen Anthony Jackson
Reply

Thanks Imar, that fixed it

 

You must be logged in to post in the forum