must I need to use C# and CSOM to conect to the Sharepoint rest web api?
-
18-02-2021 - |
Domanda
I recently had a question and how to connect to the REST web api using c# and it gave a hard time trying to connect, documentation does not give any clue how to use the rest web api(code) but community forums does, but in the code there is a CSOM nugget that seems to handle the negotiation when requests are made code below.
var s = new System.Security.SecureString();
"123abc".ToList().ForEach(o => s.AppendChar(o));
SharePointOnlineCredentials cred = new SharePointOnlineCredentials("admin@test.onmicrosoft.com", s);
HttpWebRequest endpointRequesta = (HttpWebRequest)HttpWebRequest.Create("https://test.sharepoint.com/sites/CRM/_api/contextinfo");
endpointRequest.Method = "GET";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.Credentials = cred; //<-- Notice this
endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
HttpWebResponse endpointResponse1 = (HttpWebResponse)endpointRequest.GetResponse();
my questions are
Here it's using SharePointOnlineCredentials Object from CSOM ¿Why can't I use System.Net.NetworkCredential("admin@test.onmicrosoft.com", "123abc")(basic Auth)? well I can use it but it raise an error 401 but with SharePointOnlineCredentials runs well. but ¿why?
So it make me think then that I'm forced to use .net or C# for calling this sharepoint REST Apis ¿What if i just need to call this rest api as normal webservice with any languaje?
regards.
Soluzione
SharePoint Online uses claims based authentication, basic Auth would not work for SharePoint Online.
You could refer to here for more: Make a RESTful API Call to SharePoint Online from Console program
Altri suggerimenti
It depends on what tools you will use to connect to SharePoint,
Either of below you can use to connect,
- PowerShell : There is a list of command lines ready to use, we have sp powershell and pnp
- SharePoint rest api , 1 option if you intend to connect via client side browser
Hope it helps! Happy SharePointing!
PNP will (should) make your live a lot easier ? I use PNP js, but there is a nuget package for C#. If that works as the .js modules work it's quite easy to connect to SharePoint https://github.com/pnp/pnpcore