title: sharepoint web services in subwebs
body: <p>I'm trying to build a web service that is sensitive to the subweb context (i.e., it exposes a WebMethod that needs to be able to touch lists in particular subwebs).</p>
  
  <p>I have deployed the web app with the following files:</p>
  
  <pre><code>ISAPI/MyService.asmx
  ISAPI/MyServiceWsdl.aspx
  ISAPI/MyServiceDisco.aspx
  </code></pre>
  
  <p>The codebehind:</p>
  
  <pre><code>[WebService]
  public class MyService : System.Web.Services.WebService
  {
      [WebMethod]
      public ListSettings GetListSettings(string listName)
      {
          SPWeb site = SPControl.GetContextWeb(this.Context);
          SPList list = site.Lists[listName];
  
          return new ListSettings(list);
      }
  
      [WebMethod]
      public void UpdateListSettings(string listName, ListSettings settings)
      {
          SPWeb site = SPControl.GetContextWeb(this.Context);
          SPList list = site.Lists[listName];
  
          list.EnableFolderCreation = settings.EnableFolders;
          list.Update();
      }
  }
  
  public class ListSettings
  {
      public ListSettings() { }
  
      internal ListSettings(SPList list)
      {
          EnableFolders = list.EnableFolderCreation;
      }
  
      public bool EnableFolders { get; set; }
  }
  </code></pre>
  
  <p>This seems to work just fine at the root web of my site collection.  However, when I make requests to <a href="http://moss/subweb/_vti_bin/MyService.asmx" rel="nofollow noreferrer">http://moss/subweb/_vti_bin/MyService.asmx</a>, and stop the web method on a breakpoint to inspect the HttpContext, I find that the request has been made to <a href="http://moss/_vti_bin/MyService.asmx" rel="nofollow noreferrer">http://moss/_vti_bin/MyService.asmx</a>.</p>
  
  <p>I read a number of resources that describes the adjustments needed in the WSDL/disco files and in the spdisco.aspx file in ISAPI and made the adjustments I though I was supposed to make.  Like so:</p>
  
  <p>In MyServiceDisco.aspx:</p>
  
  <pre><code>&lt;%@ Page Inherits="System.Web.UI.Page" Language="C#" %&gt;
  &lt;%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint.Utilities" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint" %&gt;
  &lt;% Response.ContentType = "text/xml"; %&gt;
  
  &lt;discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/"&gt;
    &lt;contractRef ref=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %&gt; 
              docRef=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %&gt; 
              xmlns="http://schemas.xmlsoap.org/disco/scl/" /&gt;
    &lt;soap address=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %&gt; 
          xmlns:q1="http://tempuri.org/" binding="q1:MyServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" /&gt;
    &lt;soap address=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %&gt; 
          xmlns:q2="http://tempuri.org/" binding="q2:MyServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" /&gt;
  &lt;/discovery&gt;
  </code></pre>
  
  <p>At the top of MyServiceWsdl.aspx:</p>
  
  <pre><code>&lt;%@ Page Inherits="System.Web.UI.Page" Language="C#" %&gt;
  &lt;%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint.Utilities" %&gt;
  &lt;%@ Import Namespace="Microsoft.SharePoint" %&gt;
  &lt;% Response.ContentType = "text/xml"; %&gt;
  </code></pre>
  
  <p>And at the bottom:</p>
  
  <pre><code>&lt;wsdl:service name="MyService"&gt;
      &lt;wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap"&gt;
        &lt;soap:address location=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %&gt; /&gt;
      &lt;/wsdl:port&gt;
      &lt;wsdl:port name="MyServiceSoap12" binding="tns:MyServiceSoap12"&gt;
        &lt;soap12:address location=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %&gt; /&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  </code></pre>
  
  <p>And finally, the additions to spdisco.aspx:</p>
  
  <pre><code>&lt;contractRef ref=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/MyService.asmx?wsdl", '"'); %&gt;
              docRef=&lt;% SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/MyService.asmx", '"'); %&gt; 
              xmlns="http://schemas.xmlsoap.org/disco/scl/" /&gt;
    &lt;discoveryRef ref=&lt;% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/MyService?disco"),Response.Output); %&gt; 
                  xmlns="http://schemas.xmlsoap.org/disco/" /&gt;
  </code></pre>
  
  <p>At long last, I thought I had done everything necessary to get the service working properly in every web in the site collection, but alas, it doesn't seem to be right.  What did I forget to do?</p>
  
  <p><strong>Update</strong>: another tidbit of information is that trying to get the discovery XML leads to a "Not Found" sharepoint error.  In other words, accessing <a href="http://moss/mysubsite/_vti_bin/MyService.asmx?disco" rel="nofollow noreferrer">http://moss/mysubsite/_vti_bin/MyService.asmx?disco</a> leads to a Sharepoint "File Not Found" error.</p>
  
  <p><strong>Update</strong>: turns out there was a minor typo in MyServiceDisco.aspx that caused the "File Not Found" error.  However, I fixed that and now the service is completely broken.  Using the WCF testing client, I get an error message of "Server was unable to process request. ---> Object reference not set to an instance of an object" followed by a stack trace.  From an application I'm working on, when I invoke the generated proxy class "MyServiceSoapClient", the response is "The remote server returned an error: NotFound".</p>
  
  <p>Arrrgggh!</p>
  
  <p><strong>Update</strong>: Alright, the "object reference not set..." message was the result of a boneheaded mistake on my part.  Things seem to be working now, <strong>except</strong> that the SPContext.Current.HttpRequestContext.Request.Path property always shows the url to the service under the root web instead of the current web.  I've used Wireshark to make sure the client is POSTing to the correct url (and it is: <a href="http://moss/subweb/_vti_bin/MyService.asmx" rel="nofollow noreferrer">http://moss/subweb/_vti_bin/MyService.asmx</a>), I've used the ASP.NET tracing facility and haven't noticed anything awry (the request shows up in the trace with the correct url), and I've used the IIS tracing utility (logman.exe) which shows nothing unexpected.  The only thing that doesn't seem right is the HttpRequest context once I hit the breakpoint in my service.  </p>