编程语言
首页 > 编程语言> > c#-未在Micosoft Dynamic 365中创建OrganizationService的对象引用

c#-未在Micosoft Dynamic 365中创建OrganizationService的对象引用

作者:互联网

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

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

注意:删除用户名和密码是有原因的.但是它是通过代码传递的!

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

解决方法:

您可以使用以下代码.

使用这些名称空间

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

请注意:

组织Uri = https://yourOrgName.api.crm8.dynamics.com/XRMServices/2011/Organization.svc

UserName = UserName@OrgName.onmicrosoft.com

标签:dynamics-crm,c
来源: https://codeday.me/bug/20191112/2024411.html