Access Sharepoint list from .Net 3.5 as Web Service using CAML

Today I had a need to access a sharepoint list that was on our intranet site, I had to consume this list from a .net application. I used Lists.asmx web service for this. In this example I will be using .net 3.5 in the next post I want to show using .net 4.0

As always I just used a simple windows forms application to test this. After I created a new windows forms project, I added the service reference, on the Add service Reference click Advanced… and in the Service Reference Settings click Add Web Reference, this will let you add web reference in the traditional way as was done in .net 2.0


The url for my list.asmx was at location http://intranet.xxxxx/_vti_bin/lists.asmx

I gave the web reference name xxxSPList


On button1 click I wrote the code to query the list, I have commented the code so should be self explanatory

Code Snippet
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         ‘Create a new instance of the proxy class to access the list
  3.         Dim ws As New xxxSPList.Lists
  4.         ‘Setup security
  5.         ‘If you are using a specfic userID and password to access the list use the line below
  6.         ‘ws.Credentials = New System.Net.NetworkCredential(“useridgoeshere”, “passwordgoeshere”, “domainnamegoeshere”)
  7.         ‘If you want to use the currently logged on user creddentials, use the line below
  8.         ws.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
  9.         ‘Create a new xml document to query the web service
  10.         Dim queryDocument = New XmlDocument
  11.         Dim queryNode As XmlNode = queryDocument.CreateElement(“Query”)
  12.         ‘The actual CAML query, here systemName is a variable value that I am embedding inside the query, basically my sharepoint list as
  13.         ‘column called Application, I want to query that coulmn for a specific name
  14.         Dim systemName = “CDOL”
  15.         queryNode.InnerXml = “<Where><Eq><FieldRef Name=’Application’ /><Value Type=’Text’>” & systemName & “</Value></Eq></Where>”
  16.         ‘In the selectFields choose the columns from the list that need to appear in the query result
  17.         Dim selectFields As XmlNode = queryDocument.CreateElement(“ViewFields”)
  18.         selectFields.InnerXml = “<FieldRef Name=’Title’/><FieldRef Name=’Alert_x0020_Description’ />” & _
  19.                                 “<FieldRef Name=’Message_x0020_Expiration_x0020_Date’ /><FieldRef Name=’Priority’ /> “
  20.         ‘In use the lists guid to query. If you do not know how to get the guid for list, below is asimple trick.
  21.         ‘Go the list in the sharepoint, select the value uin the first column right click properties
  22.         ‘and in the address you should be able to fnd the guid
  23.         Dim result = ws.GetListItems(“{26C62B2E-5E40-4D2D-BF3C-B510938B4181}”, Nothing, queryNode, selectFields, Nothing, Nothing, Nothing)
  24.         Dim xData = XElement.Parse(result.InnerXml)
  25.         Dim alerts As New List(Of Alert)
  26.         For Each x In xData.DescendantsAndSelf(XName.Get(“row”, “#RowsetSchema”))
  27.             alerts.Add(New Alert With {.Title = GetAttributeValue(x.Attribute(“ows_Title”)), _
  28.                                        .Description = GetAttributeValue(x.Attribute(“ows_Alert_x0020_Description”)), _
  29.                                        .ExpirationDate = GetAttributeValue(x.Attribute(“ows_Message_x0020_Expiration_x0020_D”)), _
  30.                                        .Priority = GetAttributeValue(x.Attribute(“ows_Priority”)), _
  31.                                        .ID = x.Attribute(“ows_UniqueId”).Value _
  32.                                     })
  33.             ‘Todo: strip out on the guid inside the {}
  34.         Next
  35.     End Sub
  36.     Private Function GetAttributeValue(ByVal att As System.Xml.Linq.XAttribute) As String
  37.         Return If(att Is Nothing, Nothing, att.Value)
  38.     End Function

Leave a Reply

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

You are commenting using your 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