Reputation: 4517
I'm writing a stand-alone service (not a plug-in, in the strictest sense) to periodically update Dynamics CRM 2011 using the SDK.
My code includes the following:
// Get entity metadata so we can process attributes correctly
IPluginExecutionContext context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
... (etc)
This requires an instance of the ServiceProvider class [which implements IServiceProvider] but I have no idea how to get it.
So, how do I get it?
Thanks
[Edit]
I'm currently looking at using ServerConnection as an alternative.
http://msdn.microsoft.com/en-us/library/gg309393.aspx
[/Edit]
Upvotes: 1
Views: 1577
Reputation: 4517
I got it sorted.
From http://markuserlandsson.wordpress.com/2011/01/26/my-first-crm-2011-project-part-1/
(reproduced here for safety)
ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
IServiceConfiguration<IOrganizationService> config =
ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(
new Uri(Properties.Settings.Default.CrmUrl));
config.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
OrganizationServiceProxy proxy = new OrganizationServiceProxy(config, credentials);
OrganizationServiceContext context = new OrganizationServiceContext(proxy);
Then use this proxy
or context
to do what you need.
Upvotes: 0