I am trying to connect SharePoint online (office 365 cloud base) through c#.net code.I am using "Microsoft.SharepointOnline.CSOM" Nuget package version V16.1.7414.1200 and application .net framework 4.5.2. I already tried all different type of code by looking on google. nothing is working for me. Here I put all the sample codes, I tried. Sample code 1
using (ClientContext clientContext = new ClientContext(“https://example.com”))
{
var psd = "password".ToCharArray();
SecureString secureString = new SecureString();
foreach (char c in psd) secureString.AppendChar(c);
//psd.ToList().ForEach(secureString.AppendChar);
clientContext.Credentials = new SharePointOnlineCredentials("username", secureString);
// Web oWeb = clientContext.Web;
List oList = clientContext.Web.Lists.GetByTitle("Title");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "<View><RowLimit>100</RowLimit></View>";
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem,
items => items.Include(
item => item.Id,
item => item.DisplayName,
item => item.HasUniqueRoleAssignments));
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
Console.WriteLine(@"ID: {0} \nDisplay name: {1} \nUnique role assignments: {2}",
oListItem.Id, oListItem.DisplayName, oListItem.HasUniqueRoleAssignments);
}
}
Sample Code 2
using (ClientContext spcontext = new ClientContext(siteurl))
{
spcontext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
spcontext.FormsAuthenticationLoginInfo = new FormsAuthenticationLoginInfo("username", "password");
spcontext.Load(spcontext.Web, w => w.Title, w => w.ServerRelativeUrl, w => w.Lists);
try
{
spcontext.ExecuteQuery();
Console.WriteLine(spcontext.Web.ServerRelativeUrl);
}
catch (Exception ex)
{
//throw;
}
}
Sample Code 3
using (ClientContext clientContext = new ClientContext(webSPOUrl))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, passWord);
Web web = clientContext.Web;
clientContext.Load(web);
try
{
clientContext.ExecuteQuery();
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
//Console.WriteLine(web.Title);
//Console.ReadLine();
}
I am getting the error for ".ExecuteQuery()" all the times on all sample codes.FYI- there is no proxy setting on my machine
"Error creating the Web Proxy specified in the 'system.net/defaultProxy' configuration section."}
So I also change the default proxy setting in my app.config file.
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist>
<add address="[a-z]+\.[a-z]+\.[a-z]+" />
</bypasslist>
<module/>
</defaultProxy>
But after above config setting changes, I am getting the error. The underlying connection was closed: Unable to connect to the remote server. Inner exception for all the times is as below. {"An invalid argument was supplied"} -2147467259
Code reference is from below URL https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code
Is there Proxy server in your network environment?
If there is, we need to set the IP and port of proxy server as below.
In another way, we can modify the HOST file to specify the IP address and domain name.
Familiar thread for your reference:
The remote name could not be resolved: '*****online.sharepoint.com' in O365 Online Sharepoint in ASP.net