Loading...
Area: Optimizely Service API
Applies to versions: 3 and higher

Catalog service

Recommended reading 

The Optimizely Service API supports the bulk import of catalog data into Optimizely Commerce. This section describes how to work with bulk operations for catalog items and elements.

Catalog bulk import and export

In the following we describe the methods to use when importing and exporting catalog files.

Generating the catalog XML file

For the catalog import and export to work as described here, you need to generate an XML file named catalog.xml and place it at the root of a zip file to be uploaded. See Catalog service XML how to generate the XML file used when importing and exporting catalog data.

Bulk import with file

When using this method you receive a backwards-compatible catalog file that will import the content into Commerce.

post/episerverapi/commerce/import/catalog

C# code sample

using (var client = new HttpClient())
  {
    client.BaseAddress = new Uri("https://mysite.com/");
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    var content = new MultipartFormDataContent();
    var filestream = new FileStream(path, FileMode.Open);
    content.Add(new StreamContent(filestream), "file", "Catalog.zip");
    var response = client.PostAsync("/episerverapi/commerce/import/catalog", content).Result;
    if (response.StatusCode == HttpStatusCode.OK)
      {
        var returnString = response.Content.ReadAsStringAsync().Result;
        returnString = returnString.Replace("\"", "");
        Guid taskId = Guid.Empty;
        Guid.TryParse(returnString, out taskId);
      }
  }

Response

"\"9e4bd26f-b263-488c-a5d3-3e4c9f87ac4f\""

Bulk import with file upload identifier

When using this method you receive a backwards-compatible catalog file that will import the content into Commerce.

The file to be used is based of the upload identifier of a file previously updated using /episerverapi/commerce/import/upload/chunk and /episerverapi/commerce/import/upload/commit, see Chunk upload of large files.

post/episerverapi/commerce/import/catalog/{uploadId}

C# code sample

using (var client = new HttpClient())
  {
    client.BaseAddress = new Uri("https://mysite.com/");
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    var response = client.PostAsync(String.Format("episerverapi/commerce/import/catalog/{0}", uploadId), new FormUrlEncodedContent(new List<KeyValuePair<String, String>>())).Result;
    if (response.StatusCode == HttpStatusCode.OK)
      {
        var returnString = response.Content.ReadAsStringAsync().Result;
        returnString = returnString.Replace("\"", "");
        Guid taskId = Guid.Empty;
        Guid.TryParse(returnString, out taskId);
      }
  }

Response

"\"9e4bd26f-b263-488c-a5d3-3e4c9f87ac4f\""

Bulk export

This method returns a zip file of the exported content of the catalog specified in the name parameter.

get/episerverapi/commerce/export/catalog/{catalogName}

C# code sample

using (var client = new HttpClient())
  {
    client.BaseAddress = new Uri("https://mysite.com/");
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
    var response = client.GetAsync("/episerverapi/commerce/export/catalog/{name}").Result;
    if (response.StatusCode == HttpStatusCode.OK)
      {
        var zip = response.Content.ReadAsStreamAsync().Result;
        zip.CopyTo(File.Create("output.zip"));
      }
  }

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Apr 14, 2021

Recommended reading