Developer forum

Forum » Dynamicweb 10 » Save custom fields on UserCreate module

Save custom fields on UserCreate module

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,
What is the right syntax for saving a custom field on UserCreate?
I tried CustomField.{SystemName} and  SystemName, but it does not seem to work:
<input type="hidden" class="form-control" name="CustomField.AccessUser_AppActivationId" id="CustomField.AccessUser_AppActivationId">
<input type="hidden" class="form-control" name="CustomField.AppActivationId" id="CustomField.AppActivationId" value="1">
<input type="hidden" class="form-control" name="AppActivationId" id="AppActivationId" value="2">
<input type="hidden" class="form-control" name="AccessUser_AppActivationId" id="AccessUser_AppActivationId" value="3">

I am using DW10.22.8.

Thank you,

Adrian


Replies

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Adrian,

I poked around in the code and found this:

foreach (CustomFieldValue customFieldValue in this)
{
  string requestName = string.Format(CultureInfo.InvariantCulture, "CustomField.{0}_{1}", customFieldValue.CustomField.TableName,
           customFieldValue.CustomField.SystemName);

This means you need to include the table name also (AccessUser for users and AccessUserAddress for addresses). So your field name (the ID doesn't matter) should be like this:

<input type="text" name="CustomField.AccessUser_AccessUser_AppActivationId" value="1" />

where CustomField. is a hardcoded prefix. the first AccessUser_ comes from the table name and separator and the rest is the system name of the field.

That said, it doesn't seem to work for the Create template. When I added this code to the Edit template I can make it work, but when added to the Create template it does nothing.

I think that's a bug in the code. In UserEditFrontend.SetUserDetailsFromRequest I see this:

//Request custom fields
user.CustomFieldValues.RequestCustomFieldValues();

but I don't see that being called anywhere for Create scenarios. I believe it's a bug so you may want to report this to care.

You can work around it and do it yourself with a simple notification subscriber:

using Dynamicweb.Extensibility.Notifications;
using Dynamicweb.Security.UserManagement.Notifications;

namespace Dynamicweb.Host.Suite;

/// <summary>
/// Assigns custom fields from the request to the user. Only runs for new users.
/// </summary>
[Subscribe(Security.UserManagement.Notifications.Notifications.UserSaved)]
public class UserSavedSubscriber : NotificationSubscriber
{
  /// <summary>
  /// Update the user's fields.
  /// </summary>
  /// <param name="notification">The notification.</param>
  /// <param name="args">The arguments.</param>
  public override void OnNotify(string notification, NotificationArgs args)
  {
    var userArgs = (UserSavedNotificationArgs)args;
    if (!userArgs.IsNew)
    {
      return;
    }
    userArgs.Subject.CustomFieldValues.RequestCustomFieldValues();
  }
}

Even if this gets implemented in the core version in the future, your custom code shouldn't break anything.

Hope this helps,

Imar

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Imar,

Thank you very much for the prompt response.
That was my first attempt (copied from an DW9 template) but it did not work and I thought the pattern changed for some reason.

I can make it work for my case but I will also report it to Care.

Thank you,

Adrian

 

You must be logged in to post in the forum