Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Unanswered

Changes on a file field made by a plugin does not persist

(0) ShareShare
ReportReport
Posted on by 5

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
            };
        }
    }

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 78 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 72

Overall leaderboard

Product updates

Dynamics 365 release plans