2012-10-30 90 views
-1

我發現這個片段提供了一個名爲annotation的新實體的創建。 我無法找到聲明爲using指令的類XrmServicesContext。 有誰知道這到底是什麼?我在哪裏可以找到XrmServicesContext類?

private static void AddNoteToContact(IOrganizationService service, Guid id) 
{ 
    Entity annotation = new Entity(); 
    annotation.LogicalName = "annotation"; 
    using (var crm = new XrmServicesContext(service)) 
    { 
     var contact = crm.ContactSet.Where(c => c.ContactId == id).First(); 

     Debug.Write(contact.FirstName); 

     annotation["createdby"] = new EntityReference("systemuser", new Guid("2a213502-db00-e111-b263-001ec928e97f")); 
     annotation["objectid"] = contact.ToEntityReference(); 
     annotation["subject"] = "Creato con il plu-in"; 
     annotation["notetext"] = "Questa note è stata creata con l'esempio del plug-in"; 
     annotation["ObjectTypeCode"] = contact.LogicalName; 
     try 
     { 
      Guid annotationId = service.Create(annotation); 

      crm.AddObject(annotation); 
      crm.SaveChanges(); 
     } 
     catch (Exception e) 
     { 
      throw new Exception(e.Message); 
     } 

     // var note = new Annotation{ 

     //Subject ="Creato con il plu-in", 

     //NoteText ="Questa note è stata creata con l'esempio del plug-in", 

     //ObjectId = contact.ToEntityReference(), 

     //ObjectTypeCode = contact.LogicalName 

    }; 
} 

回答

2

首先,您必須生成早期綁定的實體類。 Check this article。然後,在你的代碼中插入using語句。

在您的示例中,您正在使用早期和晚期綁定的組合。我建議你選擇其中之一。在的情況下,早期綁定,生成早期綁定類後,你可以修改你的代碼,如:

Annotation annotation = new Annotation(); 
     using (var crm = new XrmServiceContext(service)) 
     { 
     annotation.ObjectId = contact.ToEntityReference(); 
     annotation.Subject = "Creato con il plu-in"; 
     annotation.NoteText = "Questa note e stata creata con l'esempio del plug-in"; 
     annotation.ObjectTypeCode = Contact.LogicalName; 

     crm.AddObject(annotation); 
     crm.SaveChanges(); 
     } 

您這裏有一個錯誤,annotation.CreatedBy場是隻讀的,你不能從設置值到這個碼。

如果你要使用後期綁定,XrmServiceContext是沒有必要的。您可以使用QueryExpression從CRM獲取聯繫人。找到examples here。而對於註釋創建使用:

Guid annotationId = service.Create(annotation); 
0

在SDK /斌/ CrmSvcUtil.exe,該工具可用來從命令提示符早期綁定實體類 ,帶參數運行CrmSvcUtil.exe即

如果你的SDK元位置是 「d:\ DATA \ sdk2013 \ SDK \ BIN \ CrmSvcUtil.exe」 那麼你的命令會喜歡這個,
CMD:

D:\Data\sdk 2013\SDK\Bin>CrmSvcUtil.exe /out:Xrm\Xrm.cs /url:[OrganizationServiceUrl] /username:[yourusername] /password:[yourpass] /namespace:Xrm /serviceContextName:XrmServiceContext 

[OrganizationServiceUrl]:爲您的組織服務URL,U可以從設置/定製/開發人員rosources /組織服務如發現

https://msdtraders.api.crm.dynamics.com/XRMServices/2011/Organization.svc
[yourusername]:您的用戶名
[yourpass]:您的密碼

這將生成名爲Xrm.cs的文件中的實體類,並在bin/Xrm/Xrm.cs文件中。如果文件夾不存在,在bin中創建文件夾Xrm,或者在cmd [out:Xrm \ Xrm.cs]中編輯參數。
在您的項目中添加Xrm.cs

在您的代碼中使用XrmServicesContext添加使用語句。
using Xrm;

現在你可以使用/訪問XrmServicesContext和所有實體......享受。

相關問題