private bool MigrateEntityToExternal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey) { if (!entity.Attributes.ContainsKey(documentBodyKey) || (string)entity.Attributes[documentBodyKey] == BinaryStorageOptions.GenericConstants.EmptyBodyContent) { return(true); } string fileName = (string)entity.Attributes[filenameKey]; Notify(string.Format("Migrating '{0}' with filename '{1}' CRM -> External using {2}.", entityName, entity.Id.ToString() + "_" + fileName, storageProvider.GetType().Name)); if (storageProvider.Create(entity.Id, fileName, Convert.FromBase64String((string)entity.Attributes[documentBodyKey]))) { Notify(string.Format("Created '{0}' with filename '{1}'. Removing from CRM.", entityName, entity.Id.ToString() + "_" + fileName)); //Create an additional record to basically store the filesize Entity attachmentExtension = new Entity(GenericConstants.AttachementExtensionEntityName); attachmentExtension.Attributes.Add(GenericConstants.AttachementExtensionEntityNameKey, entity.LogicalName); attachmentExtension.Attributes.Add(GenericConstants.AttachementExtensionEntityIdKey, entity.Id.ToString()); attachmentExtension.Attributes.Add(GenericConstants.AttachementExtensionFileSizeKey, entity.Attributes[CrmConstants.FileSizeKey]); localProxy.Create(attachmentExtension); //FileSize (3) attributes are handled by CRM and is useless to set here. Must have a body else we get funny requests in retrieve messages. entity.Attributes[GenericConstants.Constants[entity.LogicalName][GenericConstants.DocumentBodyAttributeKey]] = GenericConstants.EmptyBodyContent; localProxy.Update(entity); Notify(string.Format("Migration of '{0}' with filename '{1}' DONE.", entityName, entity.Id.ToString() + "_" + fileName)); return(true); } return(false); }
private bool MigrateEntityExternal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey) { if (!entity.Attributes.ContainsKey(documentBodyKey) || string.IsNullOrWhiteSpace((string)entity.Attributes[documentBodyKey])) { return(true); } string fileName = (string)entity.Attributes[filenameKey]; if (fileName.StartsWith(BinaryStorageOptions.Constants.ExternalFilePrefix)) { //Already migrated, skip return(true); } Notify(string.Format("Migrating '{0}' with filename '{1}' CRM -> External using {2}.", entityName, entity.Id.ToString() + "_" + fileName, storageProvider.GetType().Name)); if (storageProvider.Create(entity.Id, fileName, Convert.FromBase64String((string)entity.Attributes[documentBodyKey]))) { Notify(string.Format("Created '{0}' with filename '{1}'. Removing from CRM.", entityName, entity.Id.ToString() + "_" + fileName)); entity.Attributes[filenameKey] = BinaryStorageOptions.Constants.ExternalFilePrefix + fileName; entity.Attributes[documentBodyKey] = BinaryStorageOptions.Constants.EmptyBodyContent; try { localProxy.Update(entity); } catch { storageProvider.Delete(entity.Id); throw; } Notify(string.Format("Migration of '{0}' with filename '{1}' DONE.", entityName, entity.Id.ToString() + "_" + fileName)); return(true); } return(false); }
private bool MigrateEntityToExternal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey, bool moveAnnotations) { if (!entity.Attributes.ContainsKey(documentBodyKey) || (string)entity.Attributes[documentBodyKey] == BinaryStorageOptions.GenericConstants.EmptyBodyContent) { return(true); } string fileName = (string)entity.Attributes[filenameKey]; Notify(string.Format("Migrating '{0}' with filename '{1}' CRM -> External using {2}.", entityName, entity.Id.ToString() + "_" + fileName, storageProvider.GetType().Name)); if (storageProvider.Create(entity.Id, fileName, Convert.FromBase64String((string)entity.Attributes[documentBodyKey]))) { Notify(string.Format("Created '{0}' with filename '{1}'", entityName, entity.Id.ToString() + "_" + fileName)); if (entityName == BinaryStorageOptions.CrmConstants.AttachmentEntityName || moveAnnotations) { Notify(string.Format("Removing '{0}' with filename '{1}' from CRM.", entityName, entity.Id.ToString() + "_" + fileName)); entity.Attributes[BinaryStorageOptions.GenericConstants.Constants[entity.LogicalName][BinaryStorageOptions.GenericConstants.DocumentBodyAttributeKey]] = BinaryStorageOptions.GenericConstants.EmptyBodyContent; localProxy.Update(entity); } Notify(string.Format("Migration of '{0}' with filename '{1}' DONE.", entityName, entity.Id.ToString() + "_" + fileName)); return(true); } return(false); }