Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of using the Episerver Profile store API
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;
}
}
}
@AThraen

This comment has been minimized.

Show comment
Hide comment
@AThraen

AThraen Oct 12, 2018

Thanks, David. Very helpful!

AThraen commented Oct 12, 2018

Thanks, David. Very helpful!

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