Last active
February 21, 2020 15:52
-
-
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
This file contains hidden or 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 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