How to set a user-defined list item on a Udef field (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.
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.
Populate the list box
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:
MDOListItem[] userDefinedListItems = mdoAgent.GetList("udlist", true, udefFieldInfo.UDListDefinitionId.ToString(), false);
Next, we have set the MDOListItem
array as the data source for the list box:
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.
// 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.
// 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.