Last active
January 13, 2021 15:08
-
-
Save tbuha/11a83c92ab5ed37449e75c32379845a4 to your computer and use it in GitHub Desktop.
How to upload Genesys Purecloud CSV ContactList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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