I have a handler class (some sdk requests are handled here) for a custom entity and this handler is referenced in many plugins/classes. This entity must be reached over admin context instead of calling user. Instead of passing the service "that is created over admin guid" to the handler class, we are trying to impersonate the service inside the handler class. For example;
----- Inside the Plugin -----
IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
// Instead of doing something like this;
var adminOrganizationService = factory.CreateOrganizationService(Guid.Parse("~ADMINGUID~"));
MyEntityHandler myEntityHandler = new MyEntityHandler(adminOrganizationService);
// Use myEntityHandler
// What i want to do is, calling my entity handler with the service of calling user;
MyEntityHandler myEntityHandler = new MyEntityHandler(factory.CreateOrganizationService(context.UserId));
// Use myEntityHandler
and inside my entity handler, change the CallerID of the IOrganizationService instance by casting it to the OrganizationServiceProxy first.
----- Inside the Handler -----
private IOrganizationService service;
public MyEntityHandler(IOrganizationService organizationService)
{
// This is what i have tried.
service = organizationService;
(service as OrganizationServiceProxy).CallerId = Guid.Parse("~ADMINGUID~");
}
I get 'Exception: System.NullReferenceException: Object reference not set to an instance of an object.' in the casting part. Is there any way to do something like this. I hope i explained myself well enough, thanks...