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