共用方式為


使用 XRM 工具刪除資料

 

發行︰ 2016年11月

適用於: Dynamics CRM 2015

CrmServiceClient 類別中有兩種方法可用來刪除 Microsoft Dynamics 365 中的資料:DeleteEntityDeleteEntityAssociation

DeleteEntity

DeleteEntity 用來從 CRM 移除單一資料列。 若要使用此方法,您必須了解您要影響的實體結構描述名稱,以及您要移除的列的 GUID。

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

    // Delete the entity record
    crmSvc.DeleteEntity("account", <accountId>);
}
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;
}

DeleteEntityAssociation

DeleteEntityAssociation 會移除實體中記錄之間的多對多關聯。 在此範例中,我們將移除潛在客戶和客戶實體中某項記錄之間的關聯。

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

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

    Guid accountId = new Guid("<Account_GUID>");
    Guid leadId = new Guid("<Lead_GUID>");
    string accountLeadRelationshipName= "accountleads_association"; 
    crmSvc.DeleteEntityAssociation("account" , accountId, "lead" ,  leadId, accountLeadRelationshipName)
}
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
使用 XRM 工具在 CRM 中執行動作

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