2012-10-30 89 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和所有实体......享受。

相关问题