0

I'm trying to query SharePoint list (Office 365) using the code below but I keep getting the following error message

'The remote server returned an error: (403) Forbidden.'

I tried every possible solution I could think of but no luck! Any suggestions or ideas of how to access SharePoint 365 using c#?

using (ClientContext clientContext = new ClientContext("https://test.sharepoint.com/"))
        {
            SecureString passWord = new SecureString();

            foreach (char c in "test123".ToCharArray()) passWord.AppendChar(c);

               clientContext.Credentials = new SharePointOnlineCredentials("first.last@mytestsite.onmicrosoft.com", passWord);

            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.ExecuteQuery();
            Console.WriteLine(web.Title);
            Console.ReadLine();

        }
3

Your code seems to be correct, I have checked with my working copy and couldn't find any problem. Few areas to double check:

  1. Make sure the site URL is correct. You might be using the TOP level URL of your Tenant, where your user does not have access.
  2. Make sure you are using the correct user for access.
  3. Make sure you are using the latest Microsoft.SharePoint.Client dlls
  • Eric, Thank you for your comment. however, I'm using the TOP level URL and I'm using my user name and password to access the contents & I'm a site admin. Also, I'm using .NET 2015 and I think all my dll's are up to date. I'm thinking it might be some setting on the SharePoint setting itself but I'm not sure what is it. I have been trying for about a month now and no luck so far. I appreciate you help & ideas. – user63189 Jan 7 '17 at 3:49
  • If possible, could you please download the latest SharePoint Online Client Object Model libraries from NuGet and try once more. Current published dll version is 16.1.6008.1200. Since you have been trying for long time, checking this new dll might worth a shot. link – UBK Jan 7 '17 at 18:16
  • I tried that and I'm still getting The remote server returned an error: (403) Forbidden. I think it might be some setting on the office 365 sharepoint portal to allow access somehow. I'm a site admin on our office 365 but I just don't get why I keep getting the same error. – user63189 Jan 8 '17 at 2:05
  • Is there a 2 factor authentication enabled in your O365 SharePoint portal? Many companies set up 2 factor authentication which does not allow direct access using CSOM from outside network. You need to be inside the company network in order to use it. Also, in my current org my O365 account is tied with my NTID abcd@comp.com, but its part of a domain, so I had to use abcd@domain.corp.com in my CSOM to access from the comp machines. – UBK Jan 9 '17 at 18:01
0

Had the same problem too. Using SharePointOnlineCredentials solved this, it's important to use user email as a username parameter.

Here is the link: Develop application on SharePoint Online Using CSOM

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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