Skip to content

Instantly share code, notes, and snippets.

@tbuha
Last active January 13, 2021 15:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tbuha/11a83c92ab5ed37449e75c32379845a4 to your computer and use it in GitHub Desktop.
Save tbuha/11a83c92ab5ed37449e75c32379845a4 to your computer and use it in GitHub Desktop.
How to upload Genesys Purecloud CSV ContactList
using System;
using System.IO;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace PCUploadContactList
{
public class ContactListService
{
/// <summary>
///
/// </summary>
/// <param name="fileToUpload">CSV file</param>
/// <param name="contactListId">Purecloud ContactListId</param>
/// <param name="accessToken">Purecloud AccessToken</param>
/// <returns></returns>
public static async Task<ContactListUploadResponse> UploadAsync(FileDetailsProvider fileToUpload, string contactListId, string accessToken)
{
var api = "https://apps.mypurecloud.ie/uploads/v2/contactlist";
try
{
var byteContent = fileToUpload.ReadBytes();
using (var client = new HttpClient())
{
var form = new MultipartFormDataContent();
client.DefaultRequestHeaders.Add("authorization", $"bearer {accessToken}");
form.Add(new StringContent(contactListId), "id");
form.Add(new StringContent("contactlist"), "fileType");
//very important to use same column as in Purecloud ContactList.
form.Add(new StringContent("FULL PHONE"), "contact-id-name");
form.Add(new ByteArrayContent(byteContent, 0, byteContent.Length), "file", Path.GetFileName(fileToUpload.Name));
var response = await client.PostAsync(api, form);
response.EnsureSuccessStatusCode();
var responseResult = await response.Content.ReadAsStringAsync();
return JsonSerializer.Deserialize<ContactListUploadResponse>(responseResult);
}
}
catch (System.Exception ex)
{
return new ContactListUploadResponse
{
Exception = ex
};
}
}
}
public class ContactListUploadResponse
{
public string CorelationId { get; set; }
public Exception Exception { get; set; }
}
public class FileDetailsProvider
{
private readonly string _filePath;
public FileDetailsProvider(string filePath)
{
_filePath = filePath;
}
public byte[] ReadBytes() => File.ReadAllBytes(_filePath);
public string Name => Path.GetFileName(_filePath);
}
}
@tbuha
Copy link
Author

tbuha commented Jan 11, 2021

How to upload csv js example.
How to create contactlists API documentation.

IMPORTANT contact-id-name should be one of the columnNames. After csv upload Purecloud will create contact record with unique Id based on contact-id-name.

POST ContactList body request example


{
    "name": "<contact list name>",
    "division": {
        "id": "<divisions Id>,
        "name": "<divisions name>",
        "homeDivision": true
    },
    "phoneColumns": [
        {
            "columnName": "Phone",
            "type": "Cell"
        }
    ],
    "columnNames": [
        "Phone",
        "FULL PHONE",
        "Name"
    ],
    "previewModeColumnName": "Name",
    "previewModeAcceptedValues": [
        "client1",
        "client2",
        "client3"
    ],
    "attemptLimits": null,
    "automaticTimeZoneMapping": false,
    "zipCodeColumnName": null
}

@tbuha
Copy link
Author

tbuha commented Jan 11, 2021

How to add contacts to ContactList API documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment