|
using Newtonsoft.Json; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.IO; |
|
using System.Linq; |
|
using System.Web; |
|
using Telerik.DigitalExperienceCloud.Client; |
|
using Telerik.Sitefinity.Utilities.Zip; |
|
|
|
namespace Sitefinity.DEC.Snippets |
|
{ |
|
public class DataManagementClients |
|
{ |
|
public static void WorkWithDataManagementClients(string accessKey, string dataCenterApiKey, string dataSourceName, string trackingID) |
|
{ |
|
|
|
// Fill in your credentials and data center api key |
|
var accessToken = new AccessToken(accessKey); |
|
|
|
// Set the contact for which you wish to delete/export data |
|
var contactForWhichToDeleteOrExportData = new List<SubjectId>() |
|
{ |
|
new SubjectId(dataSourceName,trackingID) |
|
}; |
|
|
|
// Create the export data client |
|
IContactExportRequestClient contactExportClient = new ContactExportRequestClient(accessToken, dataCenterApiKey); |
|
|
|
// This is how you create a new report for exporting data |
|
ContactExportRequest newlyCreatedExportRequest = contactExportClient.CreateExportRequest(contactForWhichToDeleteOrExportData).Result; |
|
|
|
// You can get all of the export requests in the data center |
|
var allExportRequests = contactExportClient.GetAllExportRequests(new LoadOptions()); |
|
|
|
// Or you can get a specific request by knowing its id |
|
ContactExportRequest existingExportRequestObtainedById = contactExportClient.GetExportRequestById(newlyCreatedExportRequest.Id).Result; |
|
|
|
// If the status of the export request is DataPrepared, you can proceed and download the exported data |
|
if (existingExportRequestObtainedById.Status == ContactExportRequestStatus.DataPrepared) |
|
{ |
|
// Note that the report is zipped |
|
Stream exportedDataZipStream = contactExportClient.DownloadExportedData(existingExportRequestObtainedById.Id).Result; |
|
string zipFileDownloadLocation = Path.Combine(Directory.GetCurrentDirectory(), "report.zip"); |
|
|
|
using (var zipFileStream = File.Create(zipFileDownloadLocation)) |
|
{ |
|
exportedDataZipStream.CopyTo(zipFileStream); |
|
} |
|
|
|
// The exported data is in json format |
|
string extractedReportFileLocation = Path.Combine(Directory.GetCurrentDirectory(), "exportedData.json"); |
|
var extractedReportFileStream = new MemoryStream(); |
|
using (ZipFile archivedReport = ZipFile.Read(zipFileDownloadLocation)) |
|
{ |
|
archivedReport.Entries.FirstOrDefault().Extract(extractedReportFileStream); |
|
extractedReportFileStream.Position = 0; |
|
using (var reportZipFileStream = File.Create(extractedReportFileLocation)) |
|
{ |
|
extractedReportFileStream.CopyTo(reportZipFileStream); |
|
} |
|
extractedReportFileStream.Position = 0; |
|
using (var exportedDataStreamReader = new StreamReader(extractedReportFileStream)) |
|
{ |
|
using (var exportedDataJsonReader = new JsonTextReader(exportedDataStreamReader)) |
|
{ |
|
ExportFileData exportedData = new JsonSerializer().Deserialize<ExportFileData>(exportedDataJsonReader); |
|
} |
|
} |
|
} |
|
} |
|
|
|
// This is how you create a delete data client |
|
IContactDeleteRequestClient contactDeleteClient = new ContactDeleteRequestClient(accessToken, dataCenterApiKey); |
|
|
|
// And this is how you can create a new data delete request |
|
ContactDeleteRequest newlyCreatedDeleteRequest = contactDeleteClient.CreateDeleteRequest(contactForWhichToDeleteOrExportData).Result; |
|
|
|
// You can get all of the delete requests in the data center |
|
var allDeleteRequests = contactDeleteClient.GetAll(new LoadOptions()); |
|
|
|
// Or a specific one if you know its id |
|
ContactDeleteRequest existingDeleteRequestObtainedById = contactDeleteClient.GetById(newlyCreatedDeleteRequest.Id).Result; |
|
|
|
// You can consider the data to have been deleted when the status of the request is Done |
|
if (existingDeleteRequestObtainedById.Status == ContactDeleteRequestStatus.Done) |
|
{ |
|
Console.WriteLine("The data has been successfully deleted"); |
|
} |
|
} |
|
} |
|
public class ExportFileData |
|
{ |
|
public string Source { get; set; } |
|
public DateTime DataExportDateInUtc { get; set; } |
|
public DemographicItem[] DemographicData { get; set; } |
|
public BehavioralItem[] BehavioralData { get; set; } |
|
} |
|
|
|
public class BehavioralItem |
|
{ |
|
public string Action { get; set; } |
|
public string Object { get; set; } |
|
public DateTime Timestamp { get; set; } |
|
} |
|
|
|
public class DemographicItem |
|
{ |
|
public string Key { get; set; } |
|
public string Value { get; set; } |
|
} |
|
} |