在CRM中保存注释实体时出错

时间:2013-04-22 09:29:18

标签: dynamics-crm-2011

我正在尝试使用以下代码在CRM 2011中保存注释实体:

var annotationContext = new CrmOrganizationServiceContext(crmOrganizationService);
var annotation = annotationContext.CreateEntity("annotation");
annotationContext.AddObject(annotation);
// Setting the account id.
entity.SetAttributeValue<EntityReference>("objectid", "account", "Guid of the account");    
entity.iCrmSetPropertyValue(AnnotationDefinition.ColumnsName.DocumentBody,Convert.ToBase64String(new UnicodeEncoding().GetBytes("Sample Annotation Text")));
entity.iCrmSetPropertyValue("isdocument",true);
entity.iCrmSetPropertyValue(AnnotationDefinition.ColumnsName.Mime, "text/plain");
entity.iCrmSetPickListValue(AnnotationDefinition.ColumnsName.ObjectType, 1);
entity.iCrmSetPropertyValue(AnnotationDefinition.ColumnsName.Subject, "CompanyLogo");
annotationContext.SaveChanges();

保存更改方法失败,并在crm服务器的事件查看器中显示以下错误:

  

OrganizationId中的Web Service插件失败:###########;   SdkMessageProcessingStepId:#####; EntityName:注释;阶段:30;   MessageName:创建;的AssemblyName:   Microsoft.Crm.Extensibility.InternalOperationPlugin,   Microsoft.Crm.ObjectModel,Version = 5.0.0.0,Culture = neutral,   公钥= 31bf3856ad364e35;班级名称:   Microsoft.Crm.Extensibility.InternalOperationPlugin;例外:   未处理的异常:System.InvalidCastException:指定的强制转换是   无效。在   Microsoft.Crm.BusinessEntities.EntityNameReferenceAttributeConverter.ConvertToBusinessEntity(对象   value,AttributeMetadata attributeMetadata,Dictionary`2   abbrvToChildAttributeMetadata,ICrmConversionContext上下文,   BusinessEntity目标)   Microsoft.Crm.BusinessEntities.EntityToBusinessEntityConverter.ConvertUsingExistingBusinessEntity(EntityMetadata   entityMetadata,ICrmConversionContext conversionContext,Entity   实体,BusinessEntity businessEntity)at   Microsoft.Crm.BusinessEntities.BusinessEntity.Converter.ConvertFrom(ITypeDescriptorContext   上下文,CultureInfo文化,对象价值)   Microsoft.Crm.BusinessEntities.ConversionHelpers.Convert(ICrmConversionContext   conversionContext,Object source,Type destinationType)at   Microsoft.Crm.Extensibility.DictionaryMapper.Map(ParameterCollection中   输入,ICrmConversionContext context)at   Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(的IServiceProvider   serviceProvider)at   Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext   上下文)   Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext   上下文中)

1 个答案:

答案 0 :(得分:0)

您正在将annotation变量添加到上下文中,但更新实体对象。