Use of Global list endpoints

How to use global list endpoints in API

Updating a global list

PUT /v1/GlobalLists/{uid}/values

When updating a global list there are a large amount of changes that can be made on each respective list. In this case we are updating the values of a global list.

In the following example we will set everything up so that you can get an idea of how to use the API.

Retrieving a global list along with its values.

C#
GlobalList existingGlobalList = _apiClient.GlobalLists.GetGlobalList("Suppliers");

The GetGlobalList method sends an HTTP request to the /v1/GlobalLists/ByAlias/{alias} endpoint. As alias we use "Suppliers" in order to retrieve all the global lists under the alias.

Alternatively you can use the /v1/GlobalLists/{uid} endpoint instead if you know the uid of the global list you are looking for.

After having fetched the global list we can now follow up with also retrieving its values.

C#

Dictionary<string, GlobalListValue<SuppliersGlobalListModel>> globalListValues = _apiClient.GlobalLists.GetGlobalListValues<SuppliersGlobalListModel>(existingGlobalList.Uid).GlobalListValues.ToDictionary(x => x.Value.SupplierId);

Now we need some values to update the global lists with. In this example we use the model SuppliersGlobalListModel to set up the data that we want to update our PIM with.

C#
List<SuppliersGlobalListModel> newData = new List<SuppliersGlobalListModel>
{
    new SuppliersGlobalListModel { SupplierId = "100", Name = "Supplier D (Updated)", Phone = "555-0101", Email = "[email protected]" },
    new SuppliersGlobalListModel { SupplierId = "83", Name = "Supplier B (Updated)", Phone = "555-0303", Email = "[email protected]" },
};

From here we proceed with updating the global lists, but only if the SupplierId from newData matches an entry in globalListValues.

C#
var globalListValuesToUpdate = new List<GlobalListValue<SuppliersGlobalListModel>>();

foreach (var newSupplierData in newData)
{
    if (globalListValues.TryGetValue(newSupplierData.SupplierId, out var existingGlobalListValue))
    {
        existingGlobalListValue.Value.Name = newSupplierData.Name;
        existingGlobalListValue.Value.Phone = newSupplierData.Phone;
        existingGlobalListValue.Value.Email = newSupplierData.Email;

        globalListValuesToUpdate.Add(existingGlobalListValue);
    }
}

With them now updated we can proceed to update the values of the global list in the PIM with the changes.

C#
    _apiClient.GlobalLists.UpdateGlobalListValues<SuppliersGlobalListModel>            (existingGlobalList.Uid, globalListValuesToUpdate.ToList());

Last updated