This blog article shows you one of the possible ways to resolve the error “System.Net.WebException The remote server returned an error (403) Forbidden” when you add a record to SharePoint using CSOM. When you call ExecuteQuery you hit the error below.
By analyzing the code below it looks fine.
//youe url must be correct
string siteUrlSource = “https://someone.sharepoint.com/DevSite/”;
//this is where you use your declared url
ClientContext clientContextSource = new ClientContext(siteUrlSource);
//For SharePoint Online this is the important section
string password = “password”;
SecureString securePassword = new SecureString();
foreach (char c in password) {
securePassword.AppendChar(c);
}
clientContextSource.Credentials = new SharePointOnlineCredentials(“someone@someone.onmicrosoft.com”, securePassword);
ClientContext clientContext = new ClientContext(siteUrlSource); // 403 error line
AddItemToList(clientContext, “TestList”, “Item 4”);
Also: Microsoft.SharePoint.Client.ServerException: ‘Cannot complete this action
There is actually a double declaration.
ClientContext clientContextSource = new ClientContext(siteUrlSource);
and
ClientContext clientContext = new ClientContext(siteUrlSource);
To correct the error I do the following.
AddItemToList(clientContextSource, “TestList”, “Item 4”);