locked
Getting the Organization details from a plugin RRS feed

  • Question

  • Hello,

    From within a plug in is it possible to obtain the URL of the CRM (2011) Organization.

    This link gives the code but when trying to use this within my plugin at compile time we get this error "Error 1 The best overloaded method match for 'Microsoft.Xrm.Sdk.IOrganizationService.Execute(Microsoft.Xrm.Sdk.OrganizationRequest)' has some invalid arguments .

    protected void ExecutePostCaseDelete(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
    
                // TODO: Implement your custom Plug-in business logic.
    
                RetrieveOrganizationRequest orgRequest =
                    new RetrieveOrganizationRequest()
                    {
                        UniqueName = "MyOrg",
                        AccessType = EndpointAccessType.Default,
                        Release = OrganizationRelease.Current
                    };
    
                RetrieveOrganizationResponse org = (RetrieveOrganizationResponse)localContext.OrganizationService.Execute(orgRequest); 
    
    
                
            }


    Maz

    Thursday, October 9, 2014 10:31 AM

Answers

  • Hello,

    You are getting this error because you send it to wrong service. This request should be sent to Discovery service and you send it to OrganizationService.

    Just curious - why do you need this? I believe it is possible to store this setting somewhere - pass it to CRM through plugin's configuration section or store it in CRM in some settings entity.


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    • Marked as answer by MazP Thursday, October 9, 2014 10:50 AM
    Thursday, October 9, 2014 10:46 AM
    Moderator
  • Try to use following code snippet in your plugin code:

                QueryExpression organizationquery = new QueryExpression("organization")
                {
                    ColumnSet = new ColumnSet("name")
                };
    
                Entity organization = service.RetrieveMultiple(organizationquery).Entities.FirstOrDefault();
    
                string organizationname = organization.GetAttributeValue<string>("name");
    


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    • Marked as answer by MazP Thursday, October 9, 2014 1:01 PM
    Thursday, October 9, 2014 12:15 PM
    Moderator

All replies

  • Hello,

    You are getting this error because you send it to wrong service. This request should be sent to Discovery service and you send it to OrganizationService.

    Just curious - why do you need this? I believe it is possible to store this setting somewhere - pass it to CRM through plugin's configuration section or store it in CRM in some settings entity.


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    • Marked as answer by MazP Thursday, October 9, 2014 10:50 AM
    Thursday, October 9, 2014 10:46 AM
    Moderator
  • Thanks Andrii. Yes you are right of course!

    Yes, we could use the configuration section of the plugin but then how would we know which environment we were operating within e.g. DEV, UAT or PROD e.g. integrating with SharePoint and pointing to different SharePoint locations dependent upon the environment.


    Maz

    Thursday, October 9, 2014 10:53 AM
  • In this case you can use the same config for plugin for all deployments like following:

    <Deployments>
    	<Deployment id="ProdOrgName">
    		<Setting id="setting" value="Value"/>
    	</Deployment>
    	<Deployment id="TestOrgName">
    		<Setting id="setting" value="Value1"/>
    	</Deployment>
    </Deployments>

    During execution of a plugin you can retrieve organization entity to know for sure in what organization plugin was triggered.

    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    Thursday, October 9, 2014 11:05 AM
    Moderator
  • Thanks: I think this is what I really meant in my initial post. Can you supply therefore an example of getting the Organization Entity from within the plug in please.

    Maz

    Thursday, October 9, 2014 11:23 AM
  • Try to use following code snippet in your plugin code:

                QueryExpression organizationquery = new QueryExpression("organization")
                {
                    ColumnSet = new ColumnSet("name")
                };
    
                Entity organization = service.RetrieveMultiple(organizationquery).Entities.FirstOrDefault();
    
                string organizationname = organization.GetAttributeValue<string>("name");
    


    Dynamics CRM MVP/ Technical Evangelist at SlickData LLC
    My blog

    • Marked as answer by MazP Thursday, October 9, 2014 1:01 PM
    Thursday, October 9, 2014 12:15 PM
    Moderator
  • That's great Andrii, thanks very much. But if e.g. both DEV and UAT had the same name that will not help differentiate. That's why I was thinking of getting the URLs which are unique per environment?

    Maz


    • Edited by MazP Thursday, October 9, 2014 1:14 PM
    Thursday, October 9, 2014 1:01 PM
  • Does anybody have any idea whether from within a plugin one can establish the URL of the Dynamics server or any other way of uniquely identifying the environment (DEV/UAT/PROD) in which the plugin is running other than using the Name attribute of the Organization Entity?


    Maz


    • Edited by MazP Tuesday, October 14, 2014 5:38 PM
    Tuesday, October 14, 2014 5:34 PM
  • Yes, you can get the organization info including the Uri/Url via:

                Uri MajestyUri = new Uri("http://majesty:5555/Or/XRMServices/2011/Organization.svc");
    
                ClientCredentials clientcred = new ClientCredentials();
                DiscoveryServiceProxy dsp = new DiscoveryServiceProxy(MajestyUri, null, clientcred, null);
                dsp.Authenticate();
                RetrieveOrganizationsRequest rosreq = new RetrieveOrganizationsRequest();
                RetrieveOrganizationsResponse r = (RetrieveOrganizationsResponse)dsp.Execute(rosreq);
                OrganizationDetail orginfo = null;
                foreach (OrganizationDetail o in r.Details)
                {
                    Console.WriteLine("OrgName: {0} ", o.FriendlyName);
                }

    ...but as the actual question specified - 'from within a plugin' it's the OrganizationService they need to get it from. Good news is - yes; the CurrentServiceEndpoint stores this:


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful. Michael Mayo

    Wednesday, October 15, 2014 8:49 PM
  • Hello Michael,

    Thank you very much for your detailed response which I appreciate. However it does not I think provide exactly what I am looking for in that to create the service proxy one has to supply the URLof the organization service. The URL of that service will reflect the server (therefore environment DEV/UAT/PROD) in which one is operating. But that is what I am trying to find out if you see what I mean.

    I think that this recent post entitled Where to find Server Address And Organization Name in plugin? does provide me with the solution I was looking for though.

    Thanks again for your help.


    Maz

    Thursday, October 16, 2014 7:52 AM