Example of using the Episerver Profile store API
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 Newtonsoft.Json.Linq; | |
using RestSharp; | |
namespace InsightClient | |
{ | |
class Program | |
{ | |
private static string deviceId = "<deviceid>"; // retrive from _madid cookie value | |
private static string apiRootUrl = "<apiRootUrl>"; // get from Insight / Profile store developer portal | |
private static string subscriptionKey = "<subscriptionKey>"; // get from Insight / Profile store developer portal | |
private static string resourceGetProfile = "api/v1.0/Profiles"; | |
private static string resourceUpdateProfile = "api/v1.0/Profiles/{id}"; | |
static void Main(string[] args) | |
{ | |
var insightProfile = GetProfile(); | |
var updateResult = UpdateProfile(insightProfile, "07966 123456", "Episerver", "https://www.david-tec.com"); | |
} | |
private static JToken GetProfile() | |
{ | |
// Set up the request | |
var client = new RestClient(apiRootUrl); | |
var request = new RestRequest(resourceGetProfile, Method.GET); | |
request.AddHeader("Ocp-Apim-Subscription-Key", subscriptionKey); | |
// Filter the profiles based on the current device id | |
request.AddParameter("$filter", "DeviceIds eq " + deviceId); | |
// Execute the request to get the profile | |
var getProfileResponse = client.Execute(request); | |
var getProfileContent = getProfileResponse.Content; | |
// Get the results as a JArray object | |
var profileResponseObject = JObject.Parse(getProfileContent); | |
var profileArray = (JArray)profileResponseObject["items"]; | |
// Expecting an array of profiles with one item in it | |
var profileObject = profileArray.First; | |
return profileObject; | |
} | |
private static IRestResponse UpdateProfile(JToken profileObject, string mobile, string company, string website) | |
{ | |
// Set up the update profile request | |
var client = new RestClient(apiRootUrl); | |
var profileUpdateRequest = new RestRequest(resourceUpdateProfile, Method.PUT); | |
profileUpdateRequest.AddHeader("Ocp-Apim-Subscription-Key", subscriptionKey); | |
profileUpdateRequest.AddUrlSegment("id", profileObject["ProfileId"]); | |
// Update the profile | |
var infoObject = profileObject["Info"]; | |
infoObject["Mobile"] = mobile; | |
infoObject["Company"] = company; | |
infoObject["Website"] = website; | |
// Populate the body to update the profile | |
var updateBody = profileObject.ToString(); | |
profileUpdateRequest.AddParameter("application/json", updateBody, ParameterType.RequestBody); | |
// PUT the update request to the API | |
var updateResponse = client.Execute(profileUpdateRequest); | |
return updateResponse; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, David. Very helpful!