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:

Please advise.