Search code examples
sharepointado.netwcf-data-servicesodata

Setting base address for ADO.NET Data Service Provider


We are using the ADO.NET Data Service Provider Toolkit to implement a custom OData service endpoint running inside SharePoint 2010 (using .NET 3.5).

When the service is accessed in the root of a site collection the returned base address is correct, e.g.,

 http://localhost/_vti_bin/service.svc/ returns a base address (in the returned atom document) as <feed xml:base="http://localhost/_vti_bin/service.svc />

But when the service endpoint is accessed in a subsite, the additional path segments are ignored, e.g.

http://localhost/subsite/_vti_bin/service.svc/ returns a base address (in the returned atom document) as <feed xml:base="http://localhost/_vti_bin/service.svc />

Unfortunately, this incorrect behavior confuses PowerPivot (which seem to use the returned base address to access subsequent queries).

Is there a way to explicitly the xml:base attribute from within the provider code?


Solution

  • This can be corrected by using the same IDispatchMessageInspector trick (as used by Pablo Castro to support JSON formatting) and adding the following code:

            HttpRequestMessageProperty httpmsg = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
            UriTemplateMatch match = (UriTemplateMatch)request.Properties["UriTemplateMatchResults"];
    
            match.RequestUri = new Uri(SPContext.Current.Web.Url + match.RequestUri.PathAndQuery, UriKind.Absolute);
            match.BaseUri = new Uri(SPContext.Current.Web.Url + match.BaseUri.AbsolutePath, UriKind.Absolute);
    

    which basically changes the Base and Request Uris to include the subsite path.