共用方式為


使用 XRM 工具建立資料

 

發行︰ 2016年11月

適用於: Dynamics CRM 2015

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

此方法用來建立和附加至任何實體記錄的附註物件。 當您可以填入第一個傳遞的所有變數時,您只需要提供主旨與附註文字欄位。 事實上,這通常用來將系統產生的附註附加至實體中,或附加檔案,該檔案儲存在實體的 Dynamics 365 中。 此外,如果您為使用者提供自訂的 UI 來建立註記,這就是您如何附加該註記到 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 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 工具 API 的快速開始
使用 XRM 工具在 CRM 中執行動作

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權