web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / How can I check if a S...
Power Apps
Unanswered

How can I check if a SharePoint file exists programmatically in an MDA?

(0) ShareShare
ReportReport
Posted on by 101

I have some client-side javascript in a model-driven app that should show/hide controls based on whether a particular file in the associated record's SharePoint document location exists.

 

My thought was to send an HEAD request with XMLHttpRequest; if I receive a 200 OK response, then I should be able to assume the file exists. If I open the path of the file in my browser, it opens successfully. However, when I call it from my script, the response is 403 Forbidden - presumably because the request does not carry appropriate authentication.

 

Am I missing some easier/better way of accomplishing my objective? I can't see any method in the Xrm Web Api that would allow me to do it.

I have the same question (0)
  • Linn Zaw Win Profile Picture
    2,996 on at

    You won't be able to do Xrm.WebApi call because SharePoint file information is not stored in Dataverse.

    This article will teach you how to get a SharePoint OAuth Access Token.

    https://www.engineeredcode.com/blog/getting-a-sharepoint-oauth-access-token-in-a-dynamics-365-web-resource

     

    I am not sure if the above article (about the solution with HTML webresource) would solve your issue. If it is not, you can definitely build cloud flow in Power Automate with HTTP request trigger. You can call the flow from JavaScript as mentioned in the blog post below by passing through the current record GUID. In the flow, try to build the full SharePoint URL of the Document Location of the record based on the GUID input parameter. Then, check the existence of the a particular file and respond back to the JavaScript which will show/hide controls based on the outcome.

    https://crmminds.com/2022/11/02/dynamics-365-call-power-automate-flow-from-javascript/

     

  • Verified answer
    Spinner Profile Picture
    101 on at

    Thanks @LinnZawWin.

     

    After some further thought and research I found another way to accomplish my aim. I didn't need the file content, but only needed to confirm that it existed. To do that I used Xrm.WebApi to query the Dataverse table sharepointdocument. This seems to be a special sort of table that does not display any rows, and it cannot be queried with the usual $filter parameter. However, it can be queried with the fetchXml parameter.

     

     

     

    //make Xrm call
    let fetchQueryData = `<fetch distinct="false" mapping="logical" returntotalrecordcount="true" page="1" count="1" no-lock="false">
    <entity name="sharepointdocument">
    <attribute name="documentid" />
    <attribute name="sharepointdocumentid" />
    <attribute name="fullname" />
    <attribute name="relativelocation" />
    <attribute name="filetype" />
    <attribute name="absoluteurl" />
    <attribute name="editurl" />
     <order attribute="relativelocation" descending="false" />
     <filter>
     <condition attribute="isrecursivefetch" operator="eq" value="1" />
     </filter>
     <filter type="and">
     <condition attribute="absoluteurl" operator="eq"
     value="` + path + `" />
     </filter>
     <link-entity name="prefix_***entityname***" from="prefix_***entityname***id" to="regardingobjectid" alias="bb">
     <filter type="and">
     <condition attribute="prefix_***entityname***id" operator="eq" uitype="prefix_***entityname***"
     value="`+ entityId.slice(1, -1) + `" />
     </filter>
     </link-entity>
    </entity>
    </fetch>`;
    
    return Xrm.WebApi.retrieveMultipleRecords("sharepointdocument", "?fetchXml=" + encodeURIComponent(fetchQueryData)).then(/*handle result*/)

     

     

    This allows filtering by absoluteurl. Curiously, the query stops working if I remove the <link-entity> node, although I have no need for it in the output.

     

    I can then check whether the results are empty (i.e. the file was not found) or not, and use the result of that test to perform the actions I want.

  • Linn Zaw Win Profile Picture
    2,996 on at

    It's good to know about it.

    Thanks for sharing your answer with us, @Spinner.

    Thanks for highlighting that the query stops working if you remove the <link-entity> node. That is the piece that I was missing and why my query did not work previously when I tested before responding to your thread..

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard