Programmatically Accessing SharePoint List XML Data in C# Code


SharePoint list data can be accessed in custom applications in C# without using SharePoint Object Model or Web Services. The list data can be retrieved as XML using a specific URL to the list (even using a web browser). This is described in this post. This URL can be requested from within the custom code to get the XML. The problem while creating an XMLReader object using the above URL is, it cannot authenticate the user (or application pool identity) into the SharePoint site unless anonymous authentication is enabled in the SharePoint site (which is false in most of the SharePoint implementations). A work around for this problem is to use HTTPWebRequest object to make a request to the list URL and to parse the response stream. Following is an example which takes app pool identity to authenticated into SharePoint site and to get the list data as XML.


String myURL = "<your site URL>/_vti_bin/owssvr.dll?Cmd=Display&List={List GUID}&XMLDATA=TRUE";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(myURL);
myRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(0);
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), enc);
string result = reader.ReadToEnd();

The string “result” can be used to create an XML document.

If you are unable to use custom app pools for your web application (older versions of IIS) you can create a NetworkCredentail object to specify custom credentials. In order to do that, replace the line


myRequest.Credentials = CredentialCache.DefaultCredentials;

with the following block.


CredentialCache credCache = new CredentialCache();
credCache.Add(new Uri(myURL), "NTLM", new NetworkCredential("<user name>", "<password>", "<domain>"));
myRequest.Credentials = credCache;

Advertisements

5 thoughts on “Programmatically Accessing SharePoint List XML Data in C# Code

  1. Thanks a bunch, NLV. It makes it a lot cleaner. I’ll modify all my posts to use the source tag and will do the same for future posts. Thanks again for the valuable info.

  2. It doesn’t have to be in the SharePoint server. This works from the C# code hosted in any server. The code is making a http request into a SharePoint site passing credentials for authentication (if the SharePoint site doesn’t allow anonymous access).

  3. Thanks for this post. I tried this example, but didn’t receive any xml formatted data. It shows only like html element. How should I rectify this. Thanks in advance

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s