Skip to content

Instantly share code, notes, and snippets.

@ivakhnov
Last active February 21, 2020 15:52
Show Gist options
  • Select an option

  • Save ivakhnov/6eeaa354118e81de2348013d84b55fca to your computer and use it in GitHub Desktop.

Select an option

Save ivakhnov/6eeaa354118e81de2348013d84b55fca to your computer and use it in GitHub Desktop.
[Get Impersonated MClient] Whenever you need to have an impersonated instance for another user of the MClient in the Action Script. #sitecorecontenthub, #contenthub, #actionscript, #scripting, #impersonating
using Stylelabs.M.Sdk;
var id = Context.TriggeringUserId.Value
var client = await GetImpersonatedClientAsync(userId).ConfigureAwait(false);
async Task<IMClient> GetImpersonatedClientAsync(long userId)
{
// Unfortunately for now, there is only one overload of the method which receives the username, not the Id
// of the user you want to impersonate. Hence, we need to load the username
var loadConfig = new EntityLoadConfiguration
{
CultureLoadOption = CultureLoadOption.None,
RelationLoadOption = RelationLoadOption.None,
PropertyLoadOption = new PropertyLoadOption("Username")
};
var user = await MClient.Entities.GetAsync(userId, loadConfig).ConfigureAwait(false);
var userName = user.GetPropertyValue<string>("Username");
var impersonatedClient = await MClient.ImpersonateAsync(userName).ConfigureAwait(false);
return impersonatedClient;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment