Skip to main content

Notifications

Customer experience | Sales, Customer Insights,...
Suggested answer

Client-side: Need to check the file name & extension of Field Type = Field

Posted on by 2,811

Hello Everyone,

Thanks for reading my question.

I've created a Field of type File and place it on the form. I need to do basic validation of the file such as File extension and File content. Now, with the help of Dataverse REST Builder I've the code to find the file name/extension & file content but the challenge is if I run the code on the fieldOnChange then the needed information doesn't exist in the database by the time. So, I need to find some other event on which I can trigger the code.

4530.FileOnChange.png

this.fileToProcessOnChange = function(executionContext){ 
    prg_DataTransfer.checkFileExtension(executionContext);
}

this.checkFileExtension = function (executionContext){
    $.ajax({
        type: "GET",
        url: Xrm.Utility.getGlobalContext().getClientUrl()   "/api/data/v9.2/prg_temporarydatas(cfb732f7-7acd-4193-9bfc-d4b352669f1c)?$select=prg_filetoprocess_name",
        headers: {
            "OData-MaxVersion": "4.0",
            "OData-Version": "4.0",
            "Content-Type": "application/json; charset=utf-8",
            "Accept": "application/json",
            "Prefer": "odata.include-annotations=*"
        },
        async: true,
        success: function (data, textStatus, xhr) {
            var result = data;
            //console.log(result);
            // Columns
            var prg_temporarydataid = result["prg_temporarydataid"]; // Guid
            var prg_filetoprocess_name = result["prg_filetoprocess_name"]; // Text
            console.log("File Name: "   prg_filetoprocess_name);
        },
        error: function (xhr, textStatus, errorThrown) {
            console.log(xhr);
        }
    });
}

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Client-side: Need to check the file name & extension of Field Type = Field

    Hello,

    You can try below code to get the file Name using client api  on onSave event-

    formContext.getAttribute("cr127_filecolumn").fileName;

    CR108.PNG

  • Zohaib Uddin Khan Profile Picture
    Zohaib Uddin Khan 2,811 on at
    RE: Client-side: Need to check the file name & extension of Field Type = Field

    Thanks Bipin for the workaround. Because of some unforeseen challenges we're not allowed to create any plugin or custom workflow. Having said that I appreciate your reply.

  • Suggested answer
    Bipin D365 Profile Picture
    Bipin D365 28,964 Super User 2024 Season 1 on at
    RE: Client-side: Need to check the file name & extension of Field Type = Field

    Hello,

    You can write plugin on Post Create/Update to do the same validation and throw an error when validation failed.

  • Zohaib Uddin Khan Profile Picture
    Zohaib Uddin Khan 2,811 on at
    RE: Client-side: Need to check the file name & extension of Field Type = Field

     MikeC282 - Thanks for the reply mate.

    Do you mind explaining it further? If I understand you correctly you mean:

    1. OnChange of File I'll do force save.
    2. OnSave event I'll execute the code I've written to check the file name and extension.

    With above steps it wouldn't guarantee the needed information doesn't exist in the database by the time. Please let me know your thoughts! 

  • Suggested answer
    MikeC282 Profile Picture
    MikeC282 2,095 on at
    RE: Client-side: Need to check the file name & extension of Field Type = Field

    Hey bud,

    Could you not just force a save on the record before running the REST API to check for file name and extension? That way it'll exist?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans