使用 XRM 工具来创建数据

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

CrmServiceClient 类提供了七种方法来创建新数据和联系。 使用 XRM 工具 API 的创建动作需要数据负载。 数据负载执行 Dictionary<string, CrmDataTypeWrapper> 对象的窗体。CrmDataTypeWrapper 用来通知接口您正引用的数据点需要何种处理。 创建数据的一些方法列举在本话题里。

CreateNewRecord

该方法用来创建 Microsoft Dynamics 365 中的任何类型的实体数据。 使用时,您需要知道您想要在其中创建记录的实体的方案名,而且需要构建一个传递给它的数据负载。 本例创建一个帐户记录。

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>",“<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you’re connected to.
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Create an account record
    Dictionary<string, CrmDataTypeWrapper> inData = new Dictionary<string, CrmDataTypeWrapper>();
    inData.Add("name", new CrmDataTypeWrapper("Sample Account Name", CrmFieldType.String));
    inData.Add("address1_city", new CrmDataTypeWrapper("Redmond", CrmFieldType.String));
    inData.Add("telephone1", new CrmDataTypeWrapper("555-0160", CrmFieldType.String));
    accountId = ctrl.CrmConnectionMgr.CrmSvc.CreateNewRecord("account", inData);

    // Verify if the account is created.
    if (accountId != Guid.Empty)
    {
        Console.WriteLine(“Account created.”);
    }
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

在这个例子中,我们创建了一个名为 indata 的数据负载对象。 接着,我们用通用的语法填充它: crmFieldName , new CrmDataTypeWrapper(data,CrmFieldType)。 在设置好 indata 对象来获取要创建的值后,我们调用 CreateNewRecord 方法,该方法提供了帐户的实体逻辑名和数据负载 (indata)。

备注

您还可以使用 XRM 工具,通过使用ExecuteCrmOrganizationRequest 方法执行CreateRequest 消息来创建一个实体记录。详细信息:将消息(请求和响应类)与 ExecuteCrmOrganizationRequest 方法结合使用

CreateAnnotation

此方法用于创建和附加一个 note 对象到任何实体记录。。 尽管在第一阶段您可以填充 note 的所有变量,您只需提供主题和 note 文本字段。 实际上,这通常用于将系统生成的 note 添加到实体,或者将存储在 Dynamics 365 中的文件添加到一个实体。 此外,如果您提供自己的 UI 给您的用户创建 note,这将是您在 Dynamics 365 中添加 note 到所有者实体中的方式。 此示例是继前例来创建新生成的帐户的 note。

CrmServiceClient crmSvc = new CrmServiceClient(new System.Net.NetworkCredential("<UserName>", "<Password>", “<Domain>”),"<Server>", "<Port>", "<OrgName>");

// Verify that you are connected.
if (crmSvc != null && crmSvc.IsReady)
{
    //Display the CRM version number and org name that you are connected to.
    Console.WriteLine("Connected to CRM! (Version: {0}; Org: {1}", 
    crmSvc.ConnectedOrgVersion, crmSvc.ConnectedOrgUniqueName);

    // Create and attach a note.
    inData.Clear(); 
    inData.Add("subject", new CrmDataTypeWrapper("This is a NOTE from the API" , CrmFieldType.String)); 
    inData.Add("notetext", new CrmDataTypeWrapper("This is text that will go in the body of the note" , CrmFieldType.String));
    Guid noteID = crmSvc.CreateAnnotation("account", accountId, inData);
}
else
{
    // Display the last error.
    Console.WriteLine("An error occurred: {0}", crmSvc.LastCrmError);

    // Display the last exception message if any.
    Console.WriteLine(crmSvc.LastCrmException.Message);
    Console.WriteLine(crmSvc.LastCrmException.Source);
    Console.WriteLine(crmSvc.LastCrmException.StackTrace);

    return;
}

另请参阅

示例:XRM Tooling API 快速入门
使用 XRM 工具执行 Dynamics 365 中的操作

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权