简体   繁体   English

Sharepoint 2010 Web服务GetListItems错误400错误的请求

[英]Sharepoint 2010 Web Service GetListItems Error 400 Bad Request

I have written some code to get items from a remote sharepoint list. 我已经编写了一些代码来从远程共享点列表中获取项目。 In a stand alone windows form app, it works fine. 在独立的Windows窗体应用程序中,它可以正常工作。 However, the real task is for this to be added into an existing WCF web service. 但是,真正的任务是将其添加到现有的WCF Web服务中。 The exact same code with the exact same web service URL referenced throws this error in the event viewer: 与引用的Web服务URL完全相同的完全相同的代码在事件查看器中引发此错误:

Exception: Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request."). 异常:在System.dll中捕获了异常:“ System.Net.WebException”(“远程服务器返回了错误:(400)错误的请求。”)。 Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request.") 在System.dll中捕获到异常:“ System.Net.WebException”(“远程服务器返回错误:(400)错误的请求。”)

This happen on this line of code: 这发生在以下代码行中:

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

I will include the entire method (with some names changed to protect the innocent :)) below. 我将在下面包括整个方法(更改了一些名称以保护无辜的人:)。 Please keep in mind that it works fine in a stand alone win app. 请记住,它在独立的win应用程序中可以正常工作。 Only in the WCF service is this a problem. 仅在WCF服务中,这才是问题。 What is even more odd to me is that it hangs. 对我来说更奇怪的是它挂了。 Only in the event viewer do I see this error. 只有在事件查看器中,我才能看到此错误。 I have a try catch and I would have thought it would go to the catch. 我有一个尝试渔获物,我本来以为它会成功。 I even added an extra try catch just to see and no dice. 我什至添加了一个额外的尝试捕获,只是为了查看而不是骰子。 It's not the URL. 不是URL。 It's not the credentials. 这不是凭证。 I even hard coded them at one point just to make sure of that. 我什至只对它们进行了硬编码,以确保这一点。 Maybe there is some hidden configuration setting in WCF? 也许在WCF中有一些隐藏的配置设置?

Any help is appreciated. 任何帮助表示赞赏。 Here's the code: 这是代码:

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;
        }

I found out that I was accidentally only loading rtFa and FedAuth cookies, but not loading the rest of the cookies from a successful signin. 我发现我不小心只是加载rtFaFedAuth cookie,而没有成功登录就加载其余cookie。 that was it turns out causing me the error you described. 原来是导致我描述的错误。 Getting all of the cookies from the session remedied the issue. 从会话中获取所有cookie可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM