1
votes

I am currently trying to connect a C# program with a Sharepoint. What I have: a Sharepoint URL, Username and a Password, no Sharepoint.dll (client machine, no Microsoft.Sharepoint namespace, so I use Sharepoint Web Services), Visual Studio 2010 Ultimate.

I followed this tutorial: http://www.scribd.com/doc/8634090/Accessing-SharePoint-Data-Using-C-Without-Running-Code-On-the-SharePoint-Server-Part-1

What I did so far:

added a "Service Reference" to my project with the URL of my Sharepoint like

https://.../content/10003194/_vti_bin/lists.asmx

and my code:

//no TestSP.Lists here...
TestSP.ListsSoapClient cl = new TestSP.ListsSoapClient();
cl.Open();

String listGUID = "A9E7107B-7AF6-42DC-A1A7-6C898D55D23E";
String viewGUID = "684F5F8A-B32E-461C-BD53-677F84A9C101";

//this is where it crashes
System.Xml.Linq.XElement data = cl.GetListItems(listGUID, viewGUID, null, null, "1000", null, "");

IEnumerable<XElement> elements = data.Elements();

foreach (XElement el in elements)
{
    Console.WriteLine(el);
}

it crashes at the specified line with:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM,Basic realm="..."'

which is most likely because I did not specify a username and password. Various tutorials on the web showed shat the credentials can be set like this:

TestSP.Credentials = System.Net.CredentialCache.DefaultCredentials;

Problem is: there is no TestSP.Lists, so I used TestSP.ListsSoapClient, which has no Credentials, only ClientCredentials, which are read-only.

1

1 Answers

1
votes

You've probably used the new "Add Service Reference", and not "Add Web Reference". This creates a wrapper similar to WCF (with the Client suffix), and not a Soap based wrapper. You can specify its credentials using:

TestSP.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

The "Add Web Reference" is hidden in Visual Studio 2010. If you want, you can still use it to generate code similar to your tutorial, but the Service Reference should work as well.

See also: ClientBase<>.ClientCredentials


Side note: If all you do is reading list items based of a view, you may want to consider using an xml view - Getting XML Data From a SharePoint List. You can easily get the content by performing a web request. It has some issues with item limit, but it is generally more convenient.