SharePoint自带了WebService,通常情况下是跨服务器的访问,因为同一服务器上不同网站可以简单的通过提升权限代码就可以访问数据。
以跨服务器读取某个list为例,对项目添加“http://<server-url>/_vti_bin/Lists.asmx”和“http://<server-url>/_vti_bin/SiteData.asmx”的引用。
对两个引用的命名分别为ListService和SiteDataService
//访问列表库,通过CAML查询返回的是一个XML文件,不需要解析直接给DataSet类型赋值 public static DataSet GetItems() { ListService.Lists listsWS = new ListService.Lists(); //listsWS.Credentials = System.Net.CredentialCache.DefaultCredentials; //该句在编译环境可以代替下一句,但是跨服务器应用肯定要用下一句 listsWS.Credentials = new NetworkCredential(用户名, 密码, 域名); XmlDocument doc = new System.Xml.XmlDocument(); doc.LoadXml("<Document><Query><Where>CAML</Where></Query><ViewFields /><QueryOptions /></Document>"); XmlNode listQuery = doc.SelectSingleNode("//Query"); XmlNode listViewFields = doc.SelectSingleNode("//ViewFields"); XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions"); Guid g = GetWebID(); XmlNode items = listsWS.GetListItems("ListName", string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString()); NameTable nt = new System.Xml.NameTable(); XmlNamespaceManager nsMgr = new XmlNamespaceManager(nt); nsMgr.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/2/wordml"); XmlNode y = items.SelectSingleNode("*", nsMgr); DataSet ds = new DataSet(); if (y != null) { XmlReader xmlReader = new XmlTextReader(y.InnerXml, XmlNodeType.Element, null); ds.ReadXml(xmlReader); } return ds; } private static Guid GetWebID() { SiteDataService.SiteData siteDataWS = new sinopecSiteData.SiteData(); siteDataWS.Credentials = new NetworkCredential(用户名, 密码, 域名); SiteDataService._sWebMetadata webMetaData; SiteDataService._sWebWithTime[] arrWebWithTime; SiteDataService._sListWithTime[] arrListWithTime; SiteDataService._sFPUrl[] arrUrls; string roles; string[] roleUsers; string[] roleGroups; uint i = siteDataWS.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups); Guid g = new Guid(webMetaData.WebID); return g; }
以上代码要特别注意两点:
1。身份验证。对web引用可以直接在webconfig里面直接修改地址,相应的修改代码中的验证登录信息就可以,建议都弄到配置文件里,方便统一维护。
2。解析XML。
以下是引用的service对应的映射列表
WSS Web Services | Web Reference |
Administration Service | http://<server-url:port-number>/_vti_adm/admin.asmx |
Alerts Service | http://<server-url>/_vti_bin/alerts.asmx |
Document Workspace Service | http://<server-url>/_vti_bin/dws.asmx |
Forms Service | http://<server-url>/_vti_bin/forms.asmx |
Imaging Service | http://<server-url>/_vti_bin/imaging.asmx |
List Data Retrieval Service | http://<server-url>/_vti_bin/dspsts.asmx |
Lists Service | http://<server-url>/_vti_bin/lists.asmx |
Meetings Service | http://<server-url>/_vti_bin/meetings.asmx |
Permissions Service | http://<server-url>/_vti_bin/permissions.asmx |
Site Data Service | http://<server-url>/_vti_bin/sitedata.asmx |
Site Service | http://<server-url>/_vti_bin/sites.asmx |
Users and Groups Service | http://<server-url>/_vti_bin/usergroup.asmx |
Versions Service | http://<server-url>/_vti_bin/versions.asmx |
Views Service | http://<server-url>/_vti_bin/views.asmx |
Web Part Pages Service | http://<server-url>/_vti_bin/webpartpages.asmx |
Webs Service | http://<server-url>/_vti_bin/webs.asmx |