Some time ago I wrote a post describing my issues trying to add a service reference to the SharePoint 2013 REST API. I’ve done some more investigation since then and I have some additional information.
First off, I talked with my friend Paul Schaeflein who wrote a great blog post on how to use the REST API service proxy. I asked Paul if he’d had any issues creating the service proxy and he said he hadn’t. This baffled me until I realized that he had been targeting an on-prem farm while I had been targeting SharePoint Online. I went to one of my VM’s that had SharePoint 2013 SP1 on it, attempted to add the service reference and it worked like a charm. So it seems that the issues I was experiencing were specific to SharePoint Online. The remainder of this post applies to SharePoint Online only.
In my previous post I said that the service reference dialog automatically adds ‘/_vti_bin/ListData.svc’ to the URL for SharePoint sites and this was why Visual Studio could not add the service reference. I was mistaken.
The service reference dialog does make requests with ‘/_vti_bin/ListData.svc’ appended to the URL. But if these calls fail, it also makes requests to the URL you entered in the dialog. What it doesn’t seem to do, at least in the case of SharePoint Online, is send authentication information with the call. We can see this calling pattern in the following Fiddler trace.
We can use Fiddler to prove that the missing authentication information is the problem. I made a request to get the service metadata in the browser and then dragged that request over the Composer in Fiddler. I then copied the Cookies (which contain the authentication information) to the clipboard.
Then, with Fiddler still running, I made the request to add the service reference in Visual Studio. I took the first failed request to the correct URL and dragged it over into the Composer.
Finally I pasted the cookies from the clipboard into the Composer, pressed Execute and viola, I received the service metadata from SharePoint Online!
I’ve added an entry to the Office 365 Developer UserVoice regarding this issue. Please feel free to go and vote or comment.