I have written some code to get items from a remote sharepoint list. In a stand alone windows form app, it works fine. However, the real task is for this to be added into an existing WCF web service. The exact same code with the exact same web service URL referenced throws this error in the event viewer:
Exception: Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request."). Exception caught: 'System.Net.WebException' in System.dll ("The remote server returned an error: (400) Bad Request.")
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. Only in the WCF service is this a problem. 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. 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?
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;
}