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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Internal Server Error on calling UploadDocument from web API

(0) ShareShare
ReportReport
Posted on by 45

Hi,

I'm experiencing some issues with uploading documents in Dynamics 365 (to sharepoint) with the rest web API.

In Dynamics 365, I've configured the integration with SharePoint and it's working fine. Now, i've the requirement to programmatically upload a document for a given entity.

I've seen that from the web apps, to upload a document is called the UploadDocument endpoint:

pastedimage1572787794109v2.png

I've replicated the POST request from postman, but i'm getting two types of results:

  • "204 - no content" (success) when sharepoint document location already exists for the target entity
  • "500 - Internal server error" when it doesn't exists (whit the generic error "0x80040216 - An unexpected error occurred.")

I'm calling it with a oauth2 token issued for the admin user.

I'm relying on the Dynamics 365 API, instead of sharepoint, because i would like to maintain the same folder nomenclature with the web-uploaded documents (entity name trailing random code), and so let dynamics do all the sharepoint work.

This is a sample of the body of the request I'm sending (empty guid for opportunityid is intended), that's the same body the web app is using:

{
	"Content":"cHJvdmEgZG9jdW1lbnRvIHRlc3Rv",
	"Entity": {
		"@odata.type":"Microsoft.Dynamics.CRM.sharepointdocument",
		"locationid":"",
		"title":"doctest.txt"
	},
	"OverwriteExisting": true,
	"ParentEntityReference": {
		"@odata.type": "Microsoft.Dynamics.CRM.opportunity",
		"opportunityid":"00000000-0000-0000-0000-000000000000"
	},
	"FolderPath": ""
}

Bye!

I have the same question (0)
  • Michele Cristofani Profile Picture
    45 on at

    Just realized that the "trailing random code" on folder name is the entity id without dashes...

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    It is unsupported to use any undocumented methods. Even if it works now, this may not work with the upcoming updates.

  • Michele Cristofani Profile Picture
    45 on at

    Thank you Ravi,

    So, which is the standard way to create document locations in sharepoint (related to dynamics entities) and upload files in them?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    You can either use Flow or SharePoint Rest API's.

    www.c-sharpcorner.com/.../

    code.msdn.microsoft.com/SharePoint-Integration-c5f21604

    docs.microsoft.com/.../working-with-folders-and-files-with-rest

    Hope this helps.

  • Michele Cristofani Profile Picture
    45 on at

    I've solved with SharePoint REST API

    Thank you very much Ravi!

  • Verified answer
    Michele Cristofani Profile Picture
    45 on at

    If anyone needs this: I've choosen the Graph API over the SharePoint API https://docs.microsoft.com/en-us/graph/api/resources/onedrive?view=graph-rest-1.0

    Two main reasons:

    • Since i'm using it from an external app, i prefer to manage all API permissions from azure portal.
      Sharepoint needs to manage apps permissions inside its site for v1 api (not tested with v2)
    • Same as above for OAuth2 token issueing, could be done with the same enpoint and client id for every API needed by the same app
    • It's likely more future proof than sharepoint's API

    It seems to have, as of today, some limitation compared to Sharepoint API, but for my requirements what covers is more than enough, so i've not investigated further on this.

    Bye all!

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Thanks for sharing the details here Michele :)

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 76

#3
Martin Dráb Profile Picture

Martin Dráb 64 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans