0

I have a custom attribute with the type 'file' and I need to delete the file via a plugin. I checked the documentation located here: https://learn.microsoft.com/en-us/powerapps/developer/data-platform/file-attributes and it says that for .net I have to use DeleteFileRequest. I checked the documentation for this request and looks like it requires file ID, but I'm not sure where I can get it. I tried to pass parameters in the ParameterCollection, similar to the Download Request, but it doesn't work this way:

var req = new DeleteFileRequest()
            {
                Parameters = new ParameterCollection()
                {
                    new KeyValuePair<string, object>("Target",  new EntityReference("xxx_entityName", locId)),
                    new KeyValuePair<string, object>("FileAttributeName",  "xxx_attributeName")
                }
            };

Didn't find any samples for this request in Google, so if someone has any experience with this - please share. Thank you.

Alexei P
  • 63
  • 1
  • 7

3 Answers3

1

You can get simply get File GUID from simple Retrieve or RetrieveMultiple message request in the CRM Organization Service object and after retrieving it, you can simply make the following deleteFileRequest:

DeleteFileRequest deleteFileRequest = new DeleteFileRequest()
{
    FileId = new Guid(entityResult.Entities[0].Attributes["FieldLogicalName"].ToString())
};

DeleteFileResponse deleteFileResponse = (DeleteFileResponse)organizationService.Execute(deleteFileRequest);
1

This Thread is quite old, but the answers were not that exact: First of all, if you look at the entity containing the file field with oData, you will find that the file field contains a Guid (and there is a second _name field with the file name). However at least spkl wont generate an earlybound for the file field. So the first discovery is: You need to access the field LateBound and the containing Data Type is Guid! This code will work : myEntity.GetAttributeValue<Guid>("<filefieldname>");

And now Waleeds code will work, I've wrapped it in an Extension function to not clutter my logic:

        public static void DeleteFile(this Entity entity, string fieldName, IOrganizationService service)
        {
            if (!entity.Contains(fieldName))
                return;

            var fileId = entity.GetAttributeValue<Guid>(fieldName);
            if (fileId == Guid.Empty)
                return;

            DeleteFileRequest deleteFileRequest = new DeleteFileRequest()
            {
                FileId = fileId
            };

            service.Execute(deleteFileRequest);
        }

And finally call it after my Code has finished parsing the file:

var fileIdEntity = Service.Retrieve(target.LogicalName, target.Id, new Microsoft.Xrm.Sdk.Query.ColumnSet(fieldname)).GetAttributeValue<Guid>(fieldname);
//Downloading and Parsing here
fileIdEntity.DeleteFile(fieldname, Service);

By the way: Upload and Delete both trigger Plugins registered on the file field as the Guid in the file field changes.

Kunterbunt
  • 11
  • 1
0

I was able to get the File ID (GUID) that is required for the DeleteFileRequest. It presents in the target Entity in file attribute during file upload. So in the plug-in registered at ModifiedOn (can't use File field as a trigger), you can see in the file field the ID. You can save it somewhere for later use. Unfortunately still not sure how to get it for the already uploaded file. Please share your thoughts if you know how to do it.

Alexei P
  • 63
  • 1
  • 7