C# Sharepoint 2010 Web Service GetListItems错误400错误请求

C# Sharepoint 2010 Web Service GetListItems错误400错误请求,c#,.net,wcf,sharepoint,C#,.net,Wcf,Sharepoint,我已经编写了一些代码来从远程sharepoint列表获取项目。在独立的windows窗体应用程序中,它工作正常。然而,真正的任务是将其添加到现有的WCF web服务中。引用了完全相同的web服务URL的完全相同的代码在事件查看器中引发此错误: 异常:捕获到异常:System.dll中的“System.Net.WebException” (“远程服务器返回错误:(400)错误请求。”)。例外情况 捕获到System.dll(“远程服务器”)中的“System.Net.WebException” 返

我已经编写了一些代码来从远程sharepoint列表获取项目。在独立的windows窗体应用程序中,它工作正常。然而,真正的任务是将其添加到现有的WCF web服务中。引用了完全相同的web服务URL的完全相同的代码在事件查看器中引发此错误:

异常:捕获到异常:System.dll中的“System.Net.WebException” (“远程服务器返回错误:(400)错误请求。”)。例外情况 捕获到System.dll(“远程服务器”)中的“System.Net.WebException” 返回错误:(400)请求错误。“)

这发生在这行代码上:

XmlNode nodeListItems =
                        listService.GetListItems
                        (listName, string.Empty, query, viewFields, "0", queryOptions, null);
我将在下面介绍整个方法(更改一些名称以保护无辜者:)。请记住,它在独立win应用程序中运行良好。只有在WCF服务中,这才是一个问题。对我来说更奇怪的是它挂着。只有在事件查看器中才能看到此错误。我要试一试,我本以为会成功的。我甚至添加了一个额外的尝试捕捉只是为了看,没有骰子。这不是URL。这不是证书问题。为了确保这一点,我甚至一度对它们进行了硬编码。也许WCF中有一些隐藏的配置设置

感谢您的帮助。代码如下:

private string GetPhoneNumber(string AccountNumber)
        {
            var retvalue = "";

            try
            {
                Lists listService = new Lists();

                /*Authenticate the current user by passing their default 
                credentials to the Web service from the system credential 
                cache. */

                //listService.Credentials = new NetworkCredential(sUsername, sPassword, domain);
                listService.Credentials = System.Net.CredentialCache.DefaultCredentials;

                // listService.Credentials = new System.Net.NetworkCredential("un", "pw");
                /*Set the Url property of the service for the path to a subsite. 
                Not setting this property will return the lists in the root Web site.*/
                listService.Url =
                "somesharepointsite/_vti_bin/Lists.asmx";

                /* Instantiate an XmlDocument object */
                XmlDocument xmlDoc = new XmlDocument();

                /* Assign values to the string parameters of the GetListItems method, 
                using GUIDs for the listName and viewName variables. For listName, 
                using the list display name will also work, but using the list GUID 
                is recommended. For viewName, only the view GUID can be used. 
                Using an empty string for viewName causes the default view to be used.*/
                string listName = "Telephone Numbers";

                /*Use the CreateElement method of the document object to create 
                elements for the parameters that use XML.*/
                XmlElement query = xmlDoc.CreateElement("Query");
                XmlElement viewFields =
                    xmlDoc.CreateElement("ViewFields");
                XmlElement queryOptions =
                    xmlDoc.CreateElement("QueryOptions");

                /*To specify values for the parameter elements (optional), assign 
                CAML fragments to the InnerXml property of each element.*/
                query.InnerXml = "<Where>" +
                                    "<And><Gt><FieldRef Name = 'ID'/><Value Type='Counter'>0</Value></Gt>" +
                                    "<Eq><FieldRef Name = 'Title'/><Value Type = 'Text'>" + AccountNumber + "</Value></Eq>" +
                                    "</And>" +
                                 "</Where>";
                viewFields.InnerXml = "";
                queryOptions.InnerXml = "";

                /* Declare an XmlNode object and initialize it with the XML response 
                from the GetListItems method. The last parameter specifies the GUID 
                of the Web site containing the list. Setting it to null causes the 
                Web site specified by the Url property to be used.*/

                try
                {
                    XmlNode nodeListItems =
                        listService.GetListItems
                        (listName, string.Empty, query, viewFields, "0", queryOptions, null);

                    if (nodeListItems != null)
                    {
                        /*Loop through each node in the XML response and get data.*/
                        foreach (XmlNode listItem in nodeListItems)
                        {
                            var Phone = "No phone number";

                            if (listItem.Name == "rs:data")
                            {
                                for (int f = 0; f < listItem.ChildNodes.Count; f++)
                                {
                                    if (listItem.ChildNodes[f].Name == "z:row")
                                    {
                                        Phone = listItem.ChildNodes[f].Attributes["ows_Telephone"].Value.TrimEnd('0', '.');
                                        retvalue = Phone;
                                    }

                                }
                            }
                        }
                    }

                    if (retvalue == "") retvalue = "No result found.";
                }
                catch (WebException ex)
                {
                    retvalue = ex.Message;
                }
            }
            catch (WebException ex)
            {
                retvalue = ex.Message;
            }
            catch (Exception ex)
            {
                retvalue = ex.Message;
            }

            return retvalue;
        }
私有字符串GetPhoneNumber(字符串AccountNumber)
{
var-retvalue=“”;
尝试
{
Lists listService=新列表();
/*通过传递默认值对当前用户进行身份验证
从系统凭据到Web服务的凭据
缓存*/
//凭据=新的网络凭据(sUsername、sPassword、域);
listService.Credentials=System.Net.CredentialCache.DefaultCredentials;
//listService.Credentials=新系统.Net.NetworkCredential(“un”、“pw”);
/*为子网站的路径设置服务的Url属性。
不设置此属性将返回根网站中的列表*/
Url=
“somesharepointsite/_vti_bin/Lists.asmx”;
/*实例化XmlDocument对象*/
XmlDocument xmlDoc=新的XmlDocument();
/*为GetListItems方法的字符串参数赋值,
对listName和viewName变量使用GUID。对于listName,
使用列表显示名称也可以,但使用列表GUID
建议使用。对于viewName,只能使用视图GUID。
为viewName使用空字符串会导致使用默认视图*/
string listName=“电话号码”;
/*使用文档对象的CreateElement方法创建
使用XML的参数的元素*/
xmlementquery=xmlDoc.CreateElement(“查询”);
XmlElement视图字段=
CreateElement(“视图字段”);
XmlElement查询选项=
CreateElement(“查询选项”);
/*要指定参数元素的值(可选),请指定
CAML片段到每个元素的InnerXml属性*/
query.InnerXml=“”+
"0" +
“”+AccountNumber+“”+
"" +
"";
viewFields.InnerXml=“”;
queryOptions.InnerXml=“”;
/*声明一个XmlNode对象并用XML响应初始化它
从GetListItems方法。最后一个参数指定GUID
包含列表的网站的。将其设置为null将导致
由要使用的Url属性指定的网站*/
尝试
{
XmlNode节点列表=
listService.GetListItems
(listName,string.Empty,查询,视图字段,“0”,查询选项,null);
if(nodeListItems!=null)
{
/*循环遍历XML响应中的每个节点并获取数据*/
foreach(节点列表中的XmlNode listItem)
{
var Phone=“无电话号码”;
如果(listItem.Name==“rs:data”)
{
对于(int f=0;f
我发现我只是意外地加载了
rtFa
FedAuth
cookies,而没有加载成功登录的剩余cookies。这就是造成你所描述的错误的原因。从会话获取所有Cookie解决了此问题

所以我从来没有发现真正的问题。我最终成了我们的朋友