Skip to main content

Notifications

Dynamics 365 general forum
Suggested answer

¿How to Upload documents to SharePoint by Development C# in Dynamics 365 Online?

Posted on by 1,086

Does anyone has used Sharepoint Rest API, I need to develop a component "PLUGIN" that automates that task for uploading fles to Sharepoint,

all examples I found uses Other Microsoft Libraries like "Microsoft.SharePoint.Client"(CSOM) so in plugins there is not  way of using those libraries in Dynamics Online, maybe you could merge them but that would be like not supported, so Sharepoint seems to expose a REST API but I'm unable of consuming that.

this is what you could find on google but a 401 error rises.

HttpWebRequest endpointRequest = (HttpWebRequest)WebRequest.Create($"{siteurl}/_api/web/lists");
var s = new System.Security.SecureString();
"abc123".ToList().ForEach(o => s.AppendChar(o));
HttpWebRequest endpointRequesta = (HttpWebRequest)HttpWebRequest.Create("https://test.sharepoint.com/sites/CRM/_api/contextinfo");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
NetworkCredential cred = new System.Net.NetworkCredential("admin@trainee4.onmicrosoft.com", s);
endpointRequest.Credentials = cred;
endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
HttpWebResponse endpointResponse1 = (HttpWebResponse)endpointRequest.GetResponse();

by the way in line 8 if I do something like this it works

SharePointOnlineCredentials cred= new SharePointOnlineCredentials("admin@test.onmicrosoft.com", s);
endpointRequest.Credentials = cred;

¿So whats the wrong with that? It Uses the Client CSOM -  Microsoft.SharePoint.Client

any example, references links ?

Update 1: I can't use Power Automate.

Update 2: SPO(SharePoint Online) uses "Claims base authentication" so basic auth won't work, will do if uses either SharePointOnlineCredentials or custom implementation Custom claims with cookies, thats it's why it works.

Update 3: Maybe this it's well known but in Sharepoint Online it's recommended the use of an App(SharePoint Add-in) Create and Add-in , I think for this cases (I mean were I can't use PowerAutomate) it's better use that, where it provides the AppID, ClientId and Secret. I'll do it and share some thougths

thanks, Regards.

  • Suggested answer
    RE: ¿How to Upload documents to SharePoint by Development C# in Dynamics 365 Online?

    Hi,

    Please check the below blog posts:

    https://ajitpatra.com/2020/01/06/d365-call-sharepoint-api-from-plugin-custom-workflow-c-part-1/

    ajitpatra.com/.../

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

Dynamics 365 Community Update – Sep 16th

Welcome to the next edition of the Community Platform Update. This is a weekly…

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,522 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,441 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans