Developer forum

Forum » Development » Access custom fields for a user object

Access custom fields for a user object

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

Is there a way to access custom fields for a user using the API? I looked at Dynamicweb.Frontend.PageView.Current().User but I see no properties or methods to access the fields I defined in the backend. Before I start querying the database through code, I was wondering if there was an API member available somewhere....

Imar
 



Replies

 
Pavel Volgarev
Reply
This post has been marked as an answer
Hi Imar,

Try this:
using Dynamicweb.Modules.Common;
using Dynamicweb.Modules.Common.CustomFields;
using Dynamicweb.Modules.UserManagement;

// ......

// Getting the current Extranet user
User u = User.Current(PagePermissionLevels.Frontend);

foreach (CustomFieldValue val in u.CustomFieldValues)
{
    CustomField field = val.CustomField;
    object fieldValue = val.Value;
}
-- Pavel
Votes for this answer: 0
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Pavel,

Thanks for your quick reply.

Is this DW 8? I can't convert Current().User (the Extranet class) to a User from UserManagement. Also, a CustomFieldValue doesn't have a Field property (but it does have a CustomField property with, for example, Name and SystemName properties....

Imar

 
Pavel Volgarev
Reply
Hi Imar,

Sorry, I've corrected my previous post.

-- Pavel
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
 
Perfect. That worked great. I made two changes to your code. C# doesn't allow indexed properties, so I had to use get_Current instead. In addition, the User can be null when not logged in. Here's what I ended up with:

User user = User.get_Current(PagePermissionLevels.Frontend);
if (user != null)
{
  foreach (CustomFieldValue val in user.CustomFieldValues)
  {
    CustomField field = val.CustomField;
   object fieldValue = val.Value;
  }
}

Thanks again,

Imar
 
Pavel Volgarev
Reply
You're welcome ;-)

-- Pavel 
 
Thaw Htun Lynn
Reply

Hi There,

I am having a hard time trying to figure out how to access UserManagement:User.CustomFields loop in general use. for example calling it in master. can somebody help me with that issue? but it can definitly be call in page like onestepcheckout page.

    @foreach(LoopItem l in GetLoop("UserManagement:User.CustomFields")){
        <p>@l.TemplateTags()</p>
        @l.GetString("CustomField.Value")//got my customfield value
    }

 

I have tried API as well.

           

@using Dynamicweb.Modules.Common.CustomFields
@using Dynamicweb.Modules.UserManagement

int i=Convert.ToInt32(GetGlobalValue("Global:Extranet.UserID")); 
            Dynamicweb.Modules.UserManagement.User u = Dynamicweb.Modules.UserManagement.User.GetUserByID(i);
            var uname=u.Name;//i got name here
             foreach (LoopItem val in u.CustomFields)
              {                
                String fieldValue= val.GetString("CustomField.Value");//got nothing here               
              }

 
Nicolai Høeg Pedersen
Reply

Hi Thaw

You do not have access to custom fields for users as global tags.

But you can make an instance of the user as you do - but you can not loop over a users custom fields using templating language

@using Dynamicweb.Modules.Common.CustomFields
@using Dynamicweb.Modules.UserManagement

int i=Convert.ToInt32(GetGlobalValue("Global:Extranet.UserID")); 
            Dynamicweb.Modules.UserManagement.User u = Dynamicweb.Modules.UserManagement.User.GetUserByID(i);
            var uname=u.Name;//i got name here
             foreach (LoopItem val in u.CustomFields)
              {                
                String fieldValue= val.GetString("CustomField.Value");//got nothing here               
              }

So you have to do something like this:

@using Dynamicweb.Modules.Common.CustomFields
@using Dynamicweb.Modules.UserManagement

int i=Convert.ToInt32(GetGlobalValue("Global:Extranet.UserID")); 
            Dynamicweb.Modules.UserManagement.User u = Dynamicweb.Modules.UserManagement.User.GetUserByID(i);
            var uname=u.Name;//i got name here
            foreach (CustomFieldValue val in u.CustomFieldValues)
  {
    CustomField field = val.CustomField;
   object fieldValue = val.Value;
  }

 
Thaw Htun Lynn
Reply

Thank you Nicolai. I got what i want now. I didn't think val.Value as a string. I thought it's just an object.

 

You must be logged in to post in the forum