Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Internal Server Error on calling UploadDocument from web API

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!

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Internal Server Error on calling UploadDocument from web API

    Thanks for sharing the details here Michele :)

  • Verified answer
    RE: Internal Server Error on calling UploadDocument from web API

    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!

  • RE: Internal Server Error on calling UploadDocument from web API

    I've solved with SharePoint REST API

    Thank you very much Ravi!

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Internal Server Error on calling UploadDocument from web API

    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.

  • RE: Internal Server Error on calling UploadDocument from web API

    Thank you Ravi,

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

  • Verified answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Internal Server Error on calling UploadDocument from web API

    Hi,

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

  • RE: Internal Server Error on calling UploadDocument from web API

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

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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans