OrganizationService的对象引用不是在Micosoft Dynamic 365中创建的

时间:2016-11-11 14:34:55

标签: c# dynamics-crm

我正在尝试从Dynamic 365检索事件记录,但在尝试创建OrganizationService的引用时,我得到了null引用。

不知道Dynamic 365中是否有任何新功能,我做错了吗?

注意:出于某种原因删除了UserName和Password。但它在代码中传递!!

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);

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码。

使用这些名称空间

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

请注意:

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

UserName = UserName@OrgName.onmicrosoft.com