Data Service Exception "Unauthorised" when connecting to Sharepoint OData

Posted on | 88 words | ~1 min
ODATA Sharepoint WCF Data Services

If you are struggling to fetch data from a Sharepoint OData service and getting an error as below:

 [DataServiceClientException: Unauthorized]
   System.Data.Services.Client.QueryResult.Execute() +436914
   System.Data.Services.Client.DataServiceRequest.Execute(DataServiceContext context, QueryComponents queryComponents) +133 

Then ensure you are setting the Credentials property of your Data Service Context, as below:

//  Create the data context.
SharepointDataContext dc = new SharepointDataContext(new Uri("http://dksp/_vti_bin/listdata.svc"));

// Provide default credentials, without this authorisation will fail! dc.Credentials = System.Net.CredentialCache.DefaultCredentials;

// Etc… var accounts = from a in dc.Accounts select a;

Just another issue you may come across when using Sharepoint OData services!