0

there is not solid documentation about how to make calls to sharepoint online web api, in MSN documentation it infers that you are developing an add-in for sharepoint, I'm just making a test in c#.

I'm aware of CSOM client nugget but I can't use that. Any example to how use that Web api?

1 Answer 1

1

I wrote a console app, you may take a reference:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace TeamifySharePointClassicSite.CsRest
{
    class RestCs
    {
        static void Main(string[] args)
        {
            var siteurl = "https://abc.sharepoint.com";
            var pwd = "iampassword";
            var userName = "[email protected]";

            SecureString securePassword = new SecureString();
            pwd.ToCharArray().ToList().ForEach(s => securePassword.AppendChar(s));

            var credentials = new SharePointOnlineCredentials(userName, securePassword);

            HttpWebRequest endpointRequest = (HttpWebRequest)WebRequest.Create($"{siteurl}/_api/web/lists");
            endpointRequest.Credentials = credentials;
            endpointRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
            endpointRequest.Method = "GET";
            endpointRequest.Accept = "application/json;odata=verbose";
           
            HttpWebResponse endpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
            StreamReader streamR = new StreamReader(endpointResponse.GetResponseStream());

            Console.WriteLine(streamR.ReadToEnd());

            Console.ReadKey();
        }
    }
}

You can also use other authentication methods such as OAuth:

BR

3
  • i read about the header "X-FORMS_BASED_AUTH_ACCEPTED" when i add it an exception occurs like my user does not exists and it does, but if i remove that a 403(forbidden) error is raise.
    – PabloDev
    Nov 24, 2020 at 3:29
  • Check if Legacy Authentication is disabled for tenant. techcommunity.microsoft.com/t5/microsoft-sharepoint-blog/…
    – Baker_Kong
    Nov 24, 2020 at 4:48
  • i think this is a weird issue because LegacyAuthProtocolsEnabled is set to True, but now an error, Cannot contact web site 'trainee4.sharepoint.com' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'. but as i said it gives me all that when the header its set to "X-FORMS_BASED_AUTH_ACCEPTED", if i ignore that an 403 error is raise. but I'm using a trial version i think that could be the main issue, but i wanted to tested.
    – PabloDev
    Nov 24, 2020 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.