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 MigrateEntityToLocal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey) { if (!entity.Attributes.ContainsKey(documentBodyKey)) { return(true); } string filename = (string)entity.Attributes[filenameKey]; Notify(string.Format("Migrating '{0}' with filename '{1}' External -> CRM using {2}.", entityName, entity.Id.ToString() + "_" + filename, storageProvider.GetType().Name)); if ((string)entity.Attributes[documentBodyKey] == GenericConstants.EmptyBodyContent) { byte[] data = storageProvider.Read(entity.Id, filename); entity.Attributes[documentBodyKey] = Convert.ToBase64String(data); localProxy.Update(entity); var query = new QueryExpression(GenericConstants.AttachementExtensionEntityName); query.ColumnSet = new ColumnSet(false); query.Criteria = new FilterExpression(LogicalOperator.And); query.Criteria.AddCondition(GenericConstants.AttachementExtensionEntityNameKey, ConditionOperator.Equal, entity.LogicalName); query.Criteria.AddCondition(GenericConstants.AttachementExtensionEntityIdKey, ConditionOperator.Equal, entity.Id.ToString()); DataCollection <Entity> attachementExtensionEntities = localProxy.RetrieveMultiple(query).Entities; if (attachementExtensionEntities != null) { foreach (var attachementExtensionEntity in attachementExtensionEntities) { localProxy.Delete(attachementExtensionEntity.LogicalName, attachementExtensionEntity.Id); } } } storageProvider.Delete(entity.Id, filename); Notify(string.Format("Migration of '{0}' with id '{1}' DONE.", entityName, entity.Id.ToString() + "_" + filename)); return(true); }
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 MigrateEntityToLocal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey) { if (!entity.Attributes.ContainsKey(documentBodyKey)) { return(true); } string filename = (string)entity.Attributes[filenameKey]; Notify(string.Format("Migrating '{0}' with filename '{1}' External -> CRM using {2}.", entityName, entity.Id.ToString() + "_" + filename, storageProvider.GetType().Name)); if ((string)entity.Attributes[documentBodyKey] == BinaryStorageOptions.GenericConstants.EmptyBodyContent) { byte[] data = storageProvider.Read(entity.Id, filename); entity.Attributes[documentBodyKey] = Convert.ToBase64String(data); localProxy.Update(entity); } storageProvider.Delete(entity.Id, filename); Notify(string.Format("Migration of '{0}' with id '{1}' DONE.", entityName, entity.Id.ToString() + "_" + filename)); return(true); }
private bool MigrateEntity(Guid id, string entityName, string documentBodyKey, string filenameKey, bool moveExternal) { OrganizationServiceProxy localProxy = null; bool success = false; try { if (IfdSelected) { localProxy = new OrganizationServiceProxy(serviceManagement, authCredentials.SecurityTokenResponse); } else { localProxy = new OrganizationServiceProxy(serviceManagement, authCredentials.ClientCredentials); } BinaryStorageOptions.Configuration.IConfigurationProvider configProvider = BinaryStorageOptions.Configuration.Factory.GetConfigurationProvider(proxy, entityName, GetUnsecurePluginConfiguration(localProxy, entityName), GetSecurePluginConfiguration()); BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider = BinaryStorageOptions.Providers.Factory.GetStorageProvider(configProvider); Entity entity = localProxy.Retrieve(entityName, id, new ColumnSet(true)); if (moveExternal) { success = MigrateEntityToExternal(localProxy, storageProvider, entity, entityName, documentBodyKey, filenameKey); } else { success = MigrateEntityToLocal(localProxy, storageProvider, entity, entityName, documentBodyKey, filenameKey); } } catch (Exception ex) { NotifyError(ex.ToString()); } finally { if (localProxy != null) { localProxy.Dispose(); localProxy = null; } } return(success); }
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); }
private bool MigrateEntityLocal(OrganizationServiceProxy localProxy, BinaryStorageOptions.Providers.IBinaryStorageProvider storageProvider, Entity entity, string entityName, string documentBodyKey, string filenameKey) { string fileName = (string)entity.Attributes[filenameKey]; ConcurrentDictionary <Guid, string> externalEntries; while (!allExternalFileNames.TryGetValue(entityName, out externalEntries)) { System.Threading.Thread.Sleep(new Random().Next(100, 500)); } if (!fileName.StartsWith(BinaryStorageOptions.Constants.ExternalFilePrefix)) { //Already migrated, skip return(true); } Notify(string.Format("Migrating '{0}' with filename '{1}' External -> CRM using {2}.", entityName, externalEntries[entity.Id], storageProvider.GetType().Name)); fileName = fileName.Substring(BinaryStorageOptions.Constants.ExternalFilePrefix.Length); byte[] data = storageProvider.Read(entity.Id, fileName); entity.Attributes[filenameKey] = fileName; entity.Attributes[documentBodyKey] = Convert.ToBase64String(data); localProxy.Update(entity); storageProvider.Delete(externalEntries[entity.Id]); //Use the non lookup delete method. Notify(string.Format("Migration of '{0}' with id '{1}' DONE.", entityName, entity.Id.ToString() + "_" + fileName)); return(true); }