我想通过网络服务获取 Sharepoint 2007 列表的内容。我正在使用这段代码,我主要是从 the MSDN page for GetListItems 复制的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
namespace testGetListItems
{
class Program
{
static void Main(string[] args)
{
sharepoint.Lists listService = new sharepoint.Lists();
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlNode ndQuery = xmlDoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xmlDoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml =
"<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" +
"<DateInUtc>TRUE</DateInUtc>";
ndViewFields.InnerXml = "<FieldRef Name='Field1' /> <FieldRef Name='Field2'/>";
ndQuery.InnerXml = "<Where><And><Gt><FieldRef Name='Field1'/>" +
"<Value Type='Number'>5000</Value></Gt><Gt><FieldRef Name='Field2'/>" +
"<Value Type= 'DateTime'>2003-07-03T00:00:00</Value></Gt></And></Where>";
try
{
bool checkoutResult=listService.CheckOutFile("http://sharepoint/sites/mysite/myFile.xlsx", "false", null);
XmlNode ndListItems =
listService.GetListItems("Test List", null, ndQuery,
ndViewFields, null, ndQueryOptions, null);
}
catch (System.Web.Services.Protocols.SoapException ex)
{
Console.WriteLine("Message:\n" + ex.Message + "\nDetail:\n" +
ex.Detail.InnerText +
"\nStackTrace:\n" + ex.StackTrace);
}
}
}
}
对 CheckOutFile() 的调用工作正常。但是 GetListItems() 调用给我这个错误:
An unhandled exception of type 'System.Net.WebException' occurred in System.Web.Services.dll
Additional information: The request failed with HTTP status 401: Unauthorized.
我不明白为什么 CheckOutFile() 成功但 GetListItems() 失败,特别是因为我正在 check out 的文档在正在访问的列表中通过 GetListItems()。
请您参考如下方法:
更新:这在测试控制台应用程序中有效,但在我的主应用程序中无效。暂时留下这个答案,但在我解决这个问题之前我不会接受它。
原来问题出在网络服务的 URL 上。即使我将其添加为:
http://sharepoint/sites/mySite/_vti_bin/Lists.asmx
app.config 文件将其列为:
http://sharepoint/_vti_bin/Lists.asmx
即使在我删除引用并重新添加之后,这仍然是一个问题;我不得不手动更改 app.config 内容。一旦我这样做了,GetListItems() 调用就成功了。
