>
Blog
Book
Portfolio
Search

7/29/2006

3996 Views // 0 Comments // Not Rated

InfoPathology - Part 12 - SharePoint Web Services

Web Services are wonderful devices that allow us to run code from almost anywhere we have access to. The best part of all is that it is as easy as deploying the code's output to a shared location, and then adding a web reference to it. However, creating a SharePoint web service is a bit more involved. Below is my simplified (from MSDN) method of creating these web services from scratch.

  1. Open the .NET environment command prompt from the Start Menu.
  2. Type: disco http://localhost/<project name>/<class name of web service>.asmx
  3. In the path you typed this command from, two files will be created: <web service name>.wsdl and <web service name>.disco
  4. Open up the <web service name>.wsdl file.  Replace the first line with this:

    Code Listing 1

    1. <%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
    2. <%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
    3. <%@ Assembly Name="Microsoft.SharePoint, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    4. <%@ Import Namespace="Microsoft.SharePoint" %>
    5. <% Response.ContentType = "text/xml"; %>
  5. Replace the fourth-to-last line with this:

    Code Listing 2

    1. <soap:address location="<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %>" />
  6. Save in the SharePoint web services folder on the server as <web service name>wsdl.aspx.  By default, this is C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\ISAPI.
  7. Open the <web service name>.disco file.  Replace the entire file with this:

    Code Listing 3

    1. <%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
    2. <%@ Assembly Name="Microsoft.SharePoint, Microsoft.SharePoint, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
    3. <%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
    4. <%@ Import Namespace="Microsoft.SharePoint" %>
    5. <% Response.ContentType = "text/xml"; %>
    6. <discovery xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.xmlsoap.org/disco/" >
    7. <contractRef ref="<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %>" docRef="<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"');" %> xmlns="http://schemas.xmlsoap.org/disco/scl/" />
    8. <soap address="<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"');" %> xmlns:q1="http://tempuri.org/" binding="q1:<web service class name>Soap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
    9. </discovery>
  8. Replace all instances of "<web service name>" with the name of the class (directly in between "q1:" and "Soap" with no spaces or anything in between).
  9. Save in the SharePoint web serivces folder on the server as <web service name>disco.aspx.  (This is the same folder as in Step #5.)
  10. Copy the .asmx file from your class' project folder to this folder as well.
  11. Copy the project's .DLL file into the web services bin folder on the server.  (This is the bin folder of the folder in Step #5.)

Back in InfoPath, the URL of the SharePoint web service just deployed would be http://<server name>/_vti_bin/<web service class name>.asmx. Enter this in the Data Connection wizard after choosing a web service as the source of data. InfoPath will read the .wsdl file and give you a list of the web methods available.

After choosing the method, InfoPath will also execute the code. If the method takes parameters, you may optionally give them a sample value. If you don't, the default value for the data type is used (black for strings, zeros for integers, etc.) If this default value will crash the code, then the Data Connection wizard will fail.

For example, consider a web method that takes a string parameter corresponding to the name of a SharePoint list. The code goes into a WSS site and opens that list. Since you can't have lists with blank names, then this code will fail since it couldn't open the list. Therefore, set the sample value equal to the name of an existing list in the WSS site, or place the offending code in a Try...Catch construct. Remember, the code doesn't necessary have to work; it needs to simply not fail and throw and exception.

However, you must be careful with the Try...Catch situation. Once InfoPath can successfully test the method, a schema will be built based upon the result. For example, consider a web method that returns a System.Data.Dataset, where the code blows up (but is handled) before a data table can be added. The wizard will be successful, but if you attempt to bind a drop down list to this data source, you'll find that there is no repeating section available!

The final topic to mention regarding SharePoint web services is redeployment. If any code is changed, but the web methods signatures are exactly the same, you only need to copy the new .dll file to the to server's web service's bin folder (the same folder as in Step #10). Since the signatures and return types are the same the .wsdl file will not change, so only the new code needs to be copied over.

If the web methods are changed or deleted, or new ones are added, but the class and project names stay the same, repeat Steps #1-5 and then copy over the new .dll. The .disco file never needs to be altered, unless for some reason the name of the class or project namespace changes. If this is the case, repeat the entire process.

No Tags

No Files

No Thoughts

Your Thoughts?

You need to login with Twitter to share a Thought on this post.


Loading...