Weird DTD Error In Connecting To SharePoint Online Using CSOM
We use client-side object model (CSOM) on some of the custom solutions we built for SharePoint. It allows us to access SharePoint from client applications and we also don’t have to install SharePoint on our development machine. Other advantages of using client-side object model over server-side object model are worthy of another topic though.
While we usually have successes at our first try connecting to SharePoint on-premise and SharePoint Online, there are some instances where we hit the wall and get stuck at some errors that don’t really make sense. One example is this error we recently got from connecting to SharePoint Online using the client-side object model:
To start breaking things down, at least to help us one step closer to resolving this issue, we tried to look into some of the basic information for things mentioned in the error description.
DTD means Document Type Declaration which basically means that schema for this document type in the syntax defined in XML specification is at this location. DTD processing appears to pose a security risk which is the reason why some systems prohibit DTDs. While the error clearly states what the problem was and how to fix it, it doesn’t really help much in our case since we were just connecting using SharePoint Online credentials using the client-object model and just getting a list of sites under that tenant. We really don’t have anything in the code which uses this XMLReadingSettings, so we really don’t have a way to set the DTDProcessing property to Parse as what it suggests on the error details. No hope here, so we moved on to other things.
We searched SharePoint community forums and found some that mentioned that this could either be firewall or ISP related. We successfully connected to SharePoint on our first try, we were prompted with this error on our succeeding tries, so above is likely the reason. We tried to connect using different internet providers, locations and even using Azure virtual machines but we still got the DTD error. To understand the error more, we used Fiddler, a debugging tool that allows us to inspect network traffic. The issue seems to be happening on usernames with a custom domain, eg., @.com. Somewhere within the requests, there’s one made to msoid. that returns an error in the response which results to the DTD error message.
We tried disabling IPv6 and setting Google DNS but both did not work. What worked for us eventually was adding an entry to our hosts file. The file is on C:\WIndows\System32\drivers\etc.
Add a localhost entry at the bottom, replacing with the custom domain you’re using for your SharePoint Online account. You would need to run Notepad or any text editor as an Administrator to save your changes. This apparently makes sure that the msoid URL will be invalid for the connection request to work.
Is it my Code???
Error description explains a solution of enabling the DTD processing property on XML Reader Settings and while this might be applicable for some, usually this doesn’t help especially when you don’t have something in your code that references this XML Reader Settings.
Firewall or ISP???
There are times when running custom SharePoint code works at some location and not on others. Try connecting from another area, at work or at home and see if it works.
DNS Hosts File???
The most effective solution for the error is to add an entry to the hosts file.