I am completely new to CRM and now have this task to accomplish:
There is an entity with a field of name file_field and File datatype, when a new record of this entity is created, a plugin should run and upload a file to its file field.
I am following all the steps provided by the documentation of a file attribute, using the SOAP api. There is no error in runtime, the execution finished, all the api requests returned what they were expected to, including the CommitFileBlocksUploadRequest, that returns a CommitFileBlocksUploadResponse containing the Guid of the just uploaded file. I then set this Guid to the entity attribute and call the Update from the service. I also tried to change other atributes of the entity, like the name, just to test if the problem only ocurred with the file field, and it happens that every other field that i change persists correctly, but changes in the file field are not made, when i try to access it anywhere else it just says that the field is still empty.
There follows some of the code that i used to upload the file.
public void Execute(IServiceProvider serviceProvider) { _tracingService = (ITracingService) serviceProvider.GetService(typeof(ITracingService)); _tracingService?.Trace("Start..."); var executionContext = (IPluginExecutionContext) serviceProvider.GetService(typeof(IPluginExecutionContext)); var factory = (IOrganizationServiceFactory) serviceProvider.GetService(typeof(IOrganizationServiceFactory)); _service = factory?.CreateOrganizationService(executionContext?.UserId); var entity = (Entity) executionContext?.InputParameters["Target"]; _tracingService?.Trace($"Entity: {entity}"); if (entity != null) { var request = BuildInitRequest(entity); var response = (InitializeFileBlocksUploadResponse) _service?.Execute(request); var blockIds = (List) BuildBlockIds(response); var fileResponse = CommitRequest(blockIds, response); entity[FileAttrName] = fileResponse.FileId; entity["entity_name"] = "modified entity"; _tracingService?.Trace($"File Id: {fileResponse.FileId}"); _tracingService?.Trace($"File Size: {fileResponse.FileSizeInBytes}"); _tracingService?.Trace($"Entity Name Field: {entity["entity_name"]}"); _tracingService?.Trace($"Entity File Field: {entity[FileAttrName]}"); _service?.Update(entity); } _tracingService?.Trace("End."); } private CommitFileBlocksUploadResponse CommitRequest(List blockIds, InitializeFileBlocksUploadResponse response) { var request = new CommitFileBlocksUploadRequest() { BlockList = blockIds.ToArray(), FileContinuationToken = response.FileContinuationToken, FileName = FileName, MimeType = @"application/octet-stream" }; return (CommitFileBlocksUploadResponse) _service.Execute(request); } private IEnumerable BuildBlockIds(InitializeFileBlocksUploadResponse response) { var result = new List(); const int limit = 4 * 1024 * 1024; // 4mb var steps = Math.Ceiling(Resources.sample.Length / Convert.ToDecimal(limit)); for (var i = 0; i < steps; i ) { var blockId = NewBlockId(); result.Add(blockId); var blockData = Resources.sample.Skip(i * limit).Take(limit).ToArray(); var blockRequest = new UploadBlockRequest { FileContinuationToken = response.FileContinuationToken, BlockData = blockData, BlockId = blockId }; _service.Execute(blockRequest); } return result; } private static string NewBlockId() { return Convert.ToBase64String(Encoding.UTF8.GetBytes(Guid.NewGuid().ToString())); } private static InitializeFileBlocksUploadRequest BuildInitRequest(Entity entity) { return new InitializeFileBlocksUploadRequest { Target = new EntityReference(entity.LogicalName, entity.Id), FileAttributeName = FileAttrName, FileName = FileName }; } }