> ## Documentation Index
> Fetch the complete documentation index at: https://docs.superoffice.com/llms.txt
> Use this file to discover all available pages before exploring further.

# How to set a user-defined list item on a Udef field (services)

> How to set a user-defined list item on a Udef field using services

This example demonstrates how to set a user-defined list item value on a user-defined field on a given **contact** using Net Server services.

<Note>
  The code examples on this page use the nuget SOAP proxies, [SuperOffice.NetServer.Services][1].
</Note>

We will be using the user-defined field `Udlist one` and populate a list box with the list items for it. When an item is picked from the list box, the selected value is displayed. Clicking the **Save** button will set the selected value to the user-defined field `Udlist one` for the contact.

The following screenshot shows how the application displays the list of values for the given user-defined field.

![Application displays the list of values for the given user-defined field -screenshot][img1]

## Populate the list box

```csharp CS theme={null}
using SuperOffice;
using SuperOffice.CRM.Services;

using (SoSession newSession = SoSession.Authenticate("p", "p"))
{
  if (!(String.IsNullOrEmpty(txtContactId.Text.Trim())))
  {
    // Create a Contact Agent
    ContactAgent agent = new ContactAgent();

    // Get a Contact Entity through the Contact Agent
    ContactEntity contactEntity = agent.GetContactEntity(int.Parse(txtContactId.Text.Trim()));
    if (contactEntity != null)
    {
      this.lblContactName.Text = contactEntity.Name;

      // Create a UserDefinedFieldInfoAgent
      UserDefinedFieldInfoAgent udefFieldInfoAgent = new UserDefinedFieldInfoAgent();

      // Get the UserDefinedFieldInfo of 'Udlist one' through the UserDefinedFieldInfoAgent
      UserDefinedFieldInfo udefFieldInfo = udefFieldInfoAgent.GetUserDefinedFieldFromProgId("SuperOffice:12", 7);

      // Create MDOAgent
      MDOAgent mdoAgent = new MDOAgent();

      // Get the MDOListItems array for the given udef field - Udlist one
      MDOListItem[] userDefinedListItems = mdoAgent.GetList("udlist", true, udefFieldInfo.UDListDefinitionId.ToString(), false);

      // Set the list items
      this.lstFieldList.DataSource = userDefinedListItems;
      this.lstFieldList.DisplayMember = "Name";
      this.lstFieldList.ValueMember = "Id";
    }
  }
  else
  {
    MessageBox.Show("Please enter the contact ID.");
  }
}
```

The above code segment shows how the population of the list box is done using the `GetList` method of `SuperOffice.CRM.Services.IMDOAgent`. This method accepts a custom list ID and returns the item array of the same:

```csharp CS theme={null}
using SuperOffice;
using SuperOffice.CRM.Services;

using (SoSession newSession = SoSession.Authenticate("p", "p"))
{
  if (!(String.IsNullOrEmpty(txtContactId.Text.Trim())))
  {
    // Create a Contact Agent
    ContactAgent agent = new ContactAgent();

    // Get a Contact Entity through the Contact Agent
    ContactEntity contactEntity = agent.GetContactEntity(int.Parse(txtContactId.Text.Trim()));
    if (contactEntity != null)
    {
      this.lblContactName.Text = contactEntity.Name;

      // Create a UserDefinedFieldInfoAgent
      UserDefinedFieldInfoAgent udefFieldInfoAgent = new UserDefinedFieldInfoAgent();

      // Get the UserDefinedFieldInfo of 'Udlist one' through the UserDefinedFieldInfoAgent
      UserDefinedFieldInfo udefFieldInfo = udefFieldInfoAgent.GetUserDefinedFieldFromProgId("SuperOffice:12", 7);

      // Create MDOAgent
      MDOAgent mdoAgent = new MDOAgent();

      // Get the MDOListItems array for the given udef field - Udlist one
      MDOListItem[] userDefinedListItems = mdoAgent.GetList("udlist", true, udefFieldInfo.UDListDefinitionId.ToString(), false);

      // Set the list items
      this.lstFieldList.DataSource = userDefinedListItems;
      this.lstFieldList.DisplayMember = "Name";
      this.lstFieldList.ValueMember = "Id";
    }
  }
  else
  {
    MessageBox.Show("Please enter the contact ID.");
  }
}
```

Next, we have set the `MDOListItem` array as the data source for the list box:

```csharp CS theme={null}
      this.lstFieldList.DataSource = userDefinedListItems;
      this.lstFieldList.DisplayMember = "Name";
      this.lstFieldList.ValueMember = "Id";
```

## Set the user-defined field value

This section explains how the value selected on the list box is set as the user-defined field value.

```csharp CS theme={null}
// Create a Contact Agent
IContactAgent agent = new ContactAgent();

// Get a Contact Entity through the Contact Agent
ContactEntity contactEntity = agent.GetContactEntity(int.Parse(txtContactId.Text.Trim()));
if (contactEntity != null)
{
  // Create a UserDefinedFieldInfoAgent
  UserDefinedFieldInfoAgent udefFieldInfoAgent = new UserDefinedFieldInfoAgent();

  // Get the UserDefinedFieldInfo of 'Udlist one' through the UserDefinedFieldInfoAgent
  UserDefinedFieldInfo udefFieldInfo = udefFieldInfoAgent.GetUserDefinedFieldFromFieldLabel("Udlist one", 7);

  // Get the ProgId of the udefField
  string progId = udefFieldInfo.ProgId;

  // Get the UserDefinedFields collection for the current contact
  StringDictionary dictionary = contactEntity.UserDefinedFields;

  // Set the selected value on the listbox to the udefField value
  dictionary[progId] = this.lstFieldList.SelectedValue.ToString();

  // Save the contact details
  agent.SaveContactEntity(contactEntity);
  MessageBox.Show("Contact details saved successfully.");
  this.clearContents();
}
```

We have retrieved the `UserDefinedFields` collection for the contact of interest. This is a dictionary that holds the user-defined field data as a key-value pair where the key string is the `ProgId` of the UdefField. Thus the selected value is filled against the `ProgId` of the UdefField `Udlist one` as shown below.

```csharp CS theme={null}
  // Get the ProgId of the udefField
  string progId = udefFieldInfo.ProgId;

  // Get the UserDefinedFields collection for the current contact
  StringDictionary dictionary = contactEntity.UserDefinedFields;
```

Finally, the `SaveContactEntity` method is called to update the contact entity.

<a href="../../../../../downloads/api/setudeflistitem.zip" download>Click to download source code (zip)</a>

[1]: https://www.nuget.org/packages/SuperOffice.NetServer.Services

[img1]: /media/loc/en/api/web-services/image003-4.jpg


## Related topics

- [How to create a user-defined field using the web services API](/en/api/web-services/howto/custom-objects/rest-create-udef-field.md)
- [How to update a user-defined field using the web services API](/en/api/web-services/howto/custom-objects/rest-update-udef-field.md)
- [How to get all user-defined fields using the web services API](/en/api/web-services/howto/custom-objects/rest-get-all-udef-fields.md)
- [How to delete a user-defined field using the web services API](/en/api/web-services/howto/custom-objects/rest-delete-udef-field.md)
- [How to display all user-defined fields](/en/api/web-services/howto/custom-objects/get-udef-list-and-values.md)
- [User-defined fields](/en/custom-objects/learn/udef.md)
