Search code examples
c#dynamics-crm

Object reference of OrganizationService is not creating in Micosoft Dynamic 365


I am trying to retrieve incident records from Dynamic 365 but while trying to create reference of OrganizationService , I am getting null reference.

Don't know if anything is new in Dynamic 365 and I am doing it wrong?

Note : UserName and Password is removed for a reason. But it is Passed in Code!!

CrmConnection crmConnectionString = CrmConnection.Parse("Url=https://stbtrial.api.crm8.dynamics.com/XRMServices/2011/Organization.svc;Username=;Password=;");


OrganizationService service = new OrganizationService(crmConnectionString);
QueryExpression query = new QueryExpression("incident")
                    {
                        ColumnSet = new ColumnSet("title", "ticketnumber", "subjectid", "customerid", "caseorigincode", "pcl_pushtocaseflag"),

                        Criteria =
                        {
                            Conditions =
                {
                    new ConditionExpression
                    {
                        AttributeName="pcl_pushtocaseflag",
                        Operator=ConditionOperator.Equal,
                        Values= { true }
                    }
                }
                        },
                        Orders =
                        {
                           new OrderExpression
                           {
                               AttributeName="createdon",
                               OrderType=OrderType.Descending
                           }
                        }
                    };
EntityCollection crmCaseRecords = service.RetrieveMultiple(query);

Solution

  • You can use following code.

    Use these namespaces

    using Microsoft.Xrm.Sdk;
    
    using Microsoft.Xrm.Sdk.Client;
    
    
        #region GetOrganizationService
    public static IOrganizationService GetOrganizationService()
    {
      try
      {
        IOrganizationService organizationService = null;
    
        Uri uri = new Uri("OrganizationUri");
        var credentials = new ClientCredentials();
        credentials.UserName.UserName = "UserName";
        credentials.UserName.Password = "Password";
    
        // Cast the proxy client to the IOrganizationService interface.
        using (OrganizationServiceProxy organizationServiceProxy = new OrganizationServiceProxy(uri, null, credentials, null))
        { organizationService = (IOrganizationService)organizationServiceProxy; }
    
        return organizationService;
      }
      catch (System.Exception exception)
      {
        throw exception;
      }
    }
    #endregion
    

    Please note:

    OrganizationUri = https://yourOrgName.api.crm8.dynamics.com/XRMServices/2011/Organization.svc

    UserName = [email protected]