What is the cause of a 403 error when connecting to a Microsoft List on C#?

William Thompson 125 Reputation points
2024-02-07T13:56:33.38+00:00

What is the cause of a 403 error when connecting to a Microsoft List on C#? As part of the format of the API's in the code I am using the username and password one would normally use. I am told that since the code resides outside the ecosystem where the Microsoft List resides, it will be difficult to make a connection. Is this true?

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 60,331 Reputation points
    2024-02-07T15:59:23.5333333+00:00

    403 means you don't have access. Note that it is forbidden which means the credentials you're using are valid but don't have access to the requested resource.

    We have no context of what you're actually trying to do so please provide us the code you're using and the steps needed to replicate your problem. Also confirm if you're using the Graph SDK or equivalent (which you should be) so we can assume things like the URL is well formed.

    0 comments No comments

  2. William Thompson 125 Reputation points
    2024-02-12T16:44:40.42+00:00

    the program is written in .NET Framework instead of .Net Core because I read that the Microsoft.Sharepoint.Client namespace is not available with .Net Core. The project is a WinForm project and the UI gets and sets the following text variables:

    • URL address which is stored in the variable txtSiteUrl in and used in the code below with txtSiteUrl.Text.Trim()
    • Username which is stored in the variable txtUserName in and used in the code below with txtUserName.Text.Trim()
    • Password which is stored in the variable txtPassword in and used in the code below with txtPassword.Text.Trim()
    • The title of the List

    is stored in the variable txtListTitle in and used in the code below with txtListTitle.Text.Trim()

    Now, I was able to open a browser in incognito mode and go to the site url and, after logging in with the username and password I was able to navigate to the List.

    Here is the code:

       private void btnGetListData_Click(object sender, EventArgs e)
            {
                using(ClientContext context = new ClientContext(txtSiteUrl.Text.Trim()))
                {
                    context.AuthenticationMode = ClientAuthenticationMode.Default;
                    context.Credentials = new NetworkCredential (txtUserName.Text.Trim(), txtPassword.Text.Trim(), txtSiteUrl.Text.Trim());
    
                    Web webObj = context.Web;
    
                    List listObj = webObj.Lists.GetByTitle(txtListTitle.Text.Trim());
    
                    CamlQuery query  = CamlQuery.CreateAllItemsQuery(100);
                    ListItemCollection items = listObj.GetItems(query);
    
                    context.Load(items);
                    context.ExecuteQuery();
               }
          }
    

    I was able to wrap the "using" block of code in a try...catch statement and output the exception message to a text and set it to a label in the Form's UI. Here is the 403 and 404 errors I got:

    stomange

    Please advise.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.