NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


<< Back To All Blogs

Retrieving data from SharePoint SOAP Requests using LINQ

Wednesday, May 13th, 2009

As I have been doing quite a bit of work with the SharePoint web services I recently came across an interesting issue related to querying SOAP XML, and specifically in SharePoint. This issue also applies to other SOAP requests, and XML requests in general that have Namespace prefixes.

The issue arises while trying to do a very common LINQ query such as the following:
var lists = from list in doc.Descendants("List")
select
new SharePointList
{
Description = list.Attribute("Description").Value,
Id = list.Attribute("ID").Value,
Title = list.Attribute("Title").Value
};

I was positive that my query was correct, but it was continually returning 0 results.

Enter the world of namespacing. I assumed that because the List element was not specifically prefixed that it would not matter, but I was wrong.

From a few levels above, in this example the element was GetListCollectionResponse, there was a namespace declared which was http://schemas.microsoft.com/sharepoint/soap/.

You cannot simply add the namespace with a colon in LINQ because it is an invalid element name.

You need to declare a specific XMLNamespace element like so:
XNamespace s = "http://schemas.microsoft.com/sharepoint/soap/";

Then, modify the above query to change the doc.Descendants element to the following:
doc.Descendants(s + "List")

After this minor modification everything worked exactly as expected.

Namespacin' Tom Out.

Tags

SharePoint CSharp XML LINQ

Related Blogs

Fixing SharePoint error Log with "Preserving template record"
SharePoint STSADM AddSolution Object Reference not set to an Instance of the Object error
Fixing "Error in loading DLL" in SharePoint
Using Ext JS in SharePoint... a tip
Resolving "The application failed to initialize properly (0xc000007b)" in MOSS 2007

Comments

Currently no comments.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations