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
Developer forum
E-mail notifications
Access custom fields for a user object
Replies
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; }
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
Sorry, I've corrected my previous post.
-- Pavel
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
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
}
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;
}
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