Created
January 27, 2023 20:19
-
-
Save luebster/d27031c7e0f71e8db642d0edf4f6fb82 to your computer and use it in GitHub Desktop.
MarketingCloud: Get the first page of records from a given Data Extension
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.Text.Json.Serialization; | |
using System.Threading.Tasks; | |
using System.Linq; | |
using RestSharp; | |
public class MarketingCloudApi | |
{ | |
private readonly string _clientID = "[your client id]"; | |
private readonly string _clientSecret = "[your client secret]"; | |
private readonly string _grantType = "client_credentials"; | |
private readonly string _baseUri = "https://[your company id].rest.marketingcloudapis.com"; | |
private readonly string _baseAuth = "https://[your company id].auth.marketingcloudapis.com"; | |
private readonly string _endPointDataExtensionRecords = "data/v1/customobjectdata/key/{0}/rowset"; | |
public string AccessToken { get; set; } | |
/// externalkey from your data extension | |
public async Task<RestResponse> GetDataExtensionRecordsAsync(string externalKey) | |
{ | |
AccessToken ??= await GetOAuthAccessToken(); | |
var _client = new RestClient(_baseUri); | |
var endpoint = string.Format(_endPointDataExtensionRecords, externalKey); | |
var request = new RestRequest(endpoint, Method.Get) | |
.AddHeader("Authorization", "Bearer " + AccessToken); | |
RestResponse response = await _client.GetAsync(request); | |
// you can process the records like this | |
var records = System.Text.Json.JsonSerializer.Deserialize<Rootobject>(response.Content); | |
var entries = records.items.Select(x => x.values); | |
return response; | |
// you can also | |
} | |
private async Task<string> GetOAuthAccessToken() | |
{ | |
var _client = new RestClient(_baseAuth); | |
var request = new RestRequest("v2/token", Method.Post); | |
var data = new AccessTokenRequest | |
{ | |
ClientId = _clientID, | |
ClientSecret = _clientSecret, | |
GrantType = _grantType | |
}; | |
request.AddJsonBody(data); | |
// Get the response. | |
var response = await _client.ExecuteAsync<AccessTokenResponse>(request); | |
AccessTokenResponse responseData = response.Data; | |
return responseData.AccessToken; | |
} | |
} | |
public partial class AccessTokenRequest | |
{ | |
[JsonPropertyName("grant_type")] | |
public string GrantType { get; set; } | |
[JsonPropertyName("client_id")] | |
public string ClientId { get; set; } | |
[JsonPropertyName("client_secret")] | |
public string ClientSecret { get; set; } | |
} | |
public partial class AccessTokenResponse | |
{ | |
[JsonPropertyName("access_token")] | |
public string AccessToken { get; set; } | |
[JsonPropertyName("token_type")] | |
public string TokenType { get; set; } | |
[JsonPropertyName("expires_in")] | |
public long ExpiresIn { get; set; } | |
[JsonPropertyName("scope")] | |
public string Scope { get; set; } | |
[JsonPropertyName("soap_instance_url")] | |
public string SoapInstanceUrl { get; set; } | |
[JsonPropertyName("rest_instance_url")] | |
public string RestInstanceUrl { get; set; } | |
} | |
public class Rootobject | |
{ | |
public Links links { get; set; } | |
public string requestToken { get; set; } | |
public DateTime tokenExpireDateUtc { get; set; } | |
public string customObjectId { get; set; } | |
public string customObjectKey { get; set; } | |
public int pageSize { get; set; } | |
public int page { get; set; } | |
public int count { get; set; } | |
public int top { get; set; } | |
public Item[] items { get; set; } | |
} | |
public class Links | |
{ | |
public string self { get; set; } | |
} | |
public class Item | |
{ | |
public Keys keys { get; set; } | |
public Values values { get; set; } | |
} | |
public class Keys | |
{ | |
} | |
// the properties in the Values class will vary | |
// they should match the fields in your data extension | |
// every one will return as a string | |
// you will need to convert the string value to the appropriate data type when applicable | |
public class Values | |
{ | |
public string subscriberkey { get; set; } | |
public string emailaddress { get; set; } | |
public string firstname { get; set; } | |
public string lastname { get; set; } | |
public string mobilephone { get; set; } | |
public string address { get; set; } | |
public string city { get; set; } | |
public string state { get; set; } | |
public string zipcode { get; set; } | |
public string newsoffers { get; set; } | |
public string rules { get; set; } | |
public string preferredchili { get; set; } | |
public string entrydate { get; set; } | |
public string smsoptin { get; set; } | |
public string photofilename { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment