1

This is part of a Visual Studio 2017 application being developed on a Windows 10 box. Here is my code. I've tried various configurations but the result stays the same. Not sure where to go from here.

The actual error is thrown when the cnx.ExecuteQuery() line runs. The user name and password are drawn from the config file without error.

        private string SaveFile(string fileName, byte[] fileContent)
        {
            string retVal = string.Empty;
            try
            {
                string spSiteUrl = "https://<mysite>.sharepoint.com/SitePages/TestFiles";
                string username = Convert.ToString(ConfigurationManager.AppSettings["KeyUserName"]);
                string password = Convert.ToString(ConfigurationManager.AppSettings["KeyPassword"]);
                using (ClientContext cnx = new ClientContext(spSiteUrl))
                {

                    SecureString secret = new SecureString();
                    foreach (char c in password)
                    {
                        secret.AppendChar(c);
                    }
                    cnx.Credentials = new SharePointOnlineCredentials(username, secret);
                    Web w = cnx.Web;
                    cnx.Load(w, website => website.Lists, website => website.ServerRelativeUrl);

                    cnx.ExecuteQuery();
                    retVal = "Success";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
            }
            return retVal;
        }
0
1

This is a known issue specific to the particular machine where you are executing this code. In order to fix this issue, need to change the DNS setting in your machine.

Follow the below steps to change the DNS setting :

  1. Go to Control Panel --> Network and Internet --> Change Adapter Settings --> Select the Network through which you are connecting to Internet
  2. Right click on it and Select Properties. Then select Internet Protocol Version 4 (TCP/IPV4) and select properties
  3. Then select the "Use the following DNS Server Addresses" instead obtain DNS server addresses automatically and give the DNS whatever you want. Here i gave the free DNS address which is offered by google.
  4. Google public DNS addresses : 8.8.8.8 and 8.8.8.4 
  5. Then say OK to apply these changes.

Then run the code, it will work.

For details steps, follow the below article :

Root Element Missing when connecting to SharePoint online via CSOM/Powershell on windows 10 machine

1
  • I agree that you probably need to change DNS. I would lean more towards a DNS provider that is less likely to track you, however. I choose CloudFlare (1.1.1.1 with alternate 1.0.0.1). See also: beebom.com/best-dns-servers Jul 13 '21 at 16:04
0

In addition to specifying the DNS addresses (as mentioned above), I also had to disable (uncheck): Internet Protocol Version 6 (TCP/IPv6). Confirmed that using my automatically obtained DNS OR keeping IPv6 enabled, would cause the issue in my environment.

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.