Leggere gli item di una lista tramite WebService (SharePoint)
Un esempio di come è possibile in SharePoint 2007 (WSS3 - MOSS) leggere gli item di una lista da remoto tramite i suoi Web Service, convertire il risultato XML in un DataSet e fare il bind con una GridView.
XML: sp.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="sp.aspx.vb" Inherits="sp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>SharePoint e WebService</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</form>
</body>
</html>
Visual Basic .NET: sp.aspx.vb
Imports System.Xml
Imports System.Data
Imports System.IO
Partial Class sp
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim url As String = "http://sharepoint/SgartTest"
Dim n As System.Xml.XmlNode = GetItems(url, "Compagnie")
Dim reader As StringReader = New StringReader(n.OuterXml)
Dim ds As System.Data.DataSet = New System.Data.DataSet()
ds.ReadXml(reader)
GridView1.DataSource = ds.Tables(1)
GridView1.DataBind()
End Sub
Private Function GetItems(ByVal webPath As String, ByVal listName As String) As System.Xml.XmlNode
Dim listsWS As SPLists.Lists = New SPLists.Lists()
listsWS.Url = webPath + "/_vti_bin/lists.asmx"
listsWS.UseDefaultCredentials = True
Dim doc As System.Xml.XmlDocument = New System.Xml.XmlDocument()
doc.LoadXml("<Document><Query /><ViewFields /><QueryOptions /></Document>")
Dim listQuery As System.Xml.XmlNode = doc.SelectSingleNode("//Query")
Dim listViewFields As System.Xml.XmlNode = doc.SelectSingleNode("//ViewFields")
Dim listQueryOptions As System.Xml.XmlNode = doc.SelectSingleNode("//QueryOptions")
Dim g As Guid = GetWebID(webPath)
Dim items As System.Xml.XmlNode = listsWS.GetListItems(listName, String.Empty, listQuery, listViewFields, String.Empty, listQueryOptions, g.ToString())
Return items
End Function
Private Function GetWebID(ByVal webPath As String) As Guid
Dim siteDataWS As SPSiteData.SiteData = New SPSiteData.SiteData()
siteDataWS.UseDefaultCredentials = True
Dim webMetaData As SPSiteData._sWebMetadata = Nothing
Dim arrWebWithTime As SPSiteData._sWebWithTime() = Nothing
Dim arrListWithTime As SPSiteData._sListWithTime() = Nothing
Dim arrUrls As SPSiteData._sFPUrl() = Nothing
Dim roles As String = Nothing
Dim roleUsers As String() = Nothing
Dim roleGroups As String() = Nothing
siteDataWS.Url = webPath + "/_vti_bin/sitedata.asmx"
Dim i As UInteger = siteDataWS.GetWeb(webMetaData, arrWebWithTime, arrListWithTime, arrUrls, roles, roleUsers, roleGroups)
Dim g As Guid = New Guid(webMetaData.WebID)
Return g
End Function
End Class
Attenzione il parametro listName si riferisce al Display name della lista oppure al guid della lista in questo formato {BAFBCDCF-0F29-4B55-BD6A-16A9F48190BD} comprensivo di parentesi graffe. E' preferibile usare sempre quest'ultimo come riportato sulla documentazione Lists.UpdateListItems Method