I am trying to save the annotation entity in CRM 2011 using the following code :
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();
The save changes method fails with following error in the eventviewer of the crm server :
The Web Service plug-in failed in OrganizationId: ###########; SdkMessageProcessingStepId: #####; EntityName: annotation; Stage: 30; MessageName: Create; AssemblyName: Microsoft.Crm.Extensibility.InternalOperationPlugin, Microsoft.Crm.ObjectModel, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35; ClassName: Microsoft.Crm.Extensibility.InternalOperationPlugin; Exception: Unhandled Exception: System.InvalidCastException: Specified cast is not valid. at Microsoft.Crm.BusinessEntities.EntityNameReferenceAttributeConverter.ConvertToBusinessEntity(Object value, AttributeMetadata attributeMetadata, Dictionary`2 abbrvToChildAttributeMetadata, ICrmConversionContext context, BusinessEntity target) at Microsoft.Crm.BusinessEntities.EntityToBusinessEntityConverter.ConvertUsingExistingBusinessEntity(EntityMetadata entityMetadata, ICrmConversionContext conversionContext, Entity entity, BusinessEntity businessEntity) at Microsoft.Crm.BusinessEntities.BusinessEntity.Converter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at Microsoft.Crm.BusinessEntities.ConversionHelpers.Convert(ICrmConversionContext conversionContext, Object source, Type destinationType) at Microsoft.Crm.Extensibility.DictionaryMapper.Map(ParameterCollection inputs, ICrmConversionContext context) at Microsoft.Crm.Extensibility.InternalOperationPlugin.Execute(IServiceProvider serviceProvider) at Microsoft.Crm.Extensibility.V5PluginProxyStep.ExecuteInternal(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)