指间(蒋建华)--天行健,君子当自强不息

        专注于微软产品及.Net技术的blog
  博客园  :: 首页  :: 联系 :: 订阅 订阅  :: 管理

在sharepoint2007 中使用自定义web service

Posted on 2009-03-31 18:37  蒋建华  阅读(497)  评论(0编辑  收藏  举报

步骤:
1.创建webservice
2.生成并修改.wsdl,.disco文件
3.将Web服务文件部署到 _vti_bin 目录
4.设置服务调用路径
5.调用服务

具体步骤:

1.创建webservice,编译,以SPCustomService.asmx为例
 
2.生成并修改.wsdl,.disco文件
  (1).copy webservice生成的bin到要部署的sharepoint 网站的虚拟目录bin下
  (2).打开layouts目录(layouts地址http://www.cnblogs.com/yungboy/admin/file://program/ Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS),拷贝webservice下的.asmx文件到layouts下
  (3).开始--运行,输入cmd,转向layouts目录,运行disco http://服务器:端口号/_layouts/SPCustomService.asmx(e.g.disco http://demo:8001/_layouts/SPCustomService.asmx),生成 webservice文件名.disco和webservice文件名.wsdl.若没有disco.exe文件,则需要安装microsoft sdks,打开C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin,拷贝disco.exe到layouts目录下,运行disco http://服务器:端口号/_layouts/SPCustomService.asmx

 (4).修改webservice文件名.disco
 打开 .disco 和 .wsdl 文件,将开放式 XML 处理指令 <?xml version="1.0" encoding="utf-8"?> 替换为下列指令:
 <%@ Page Language="C#" Inherits="System.Web.UI.Page" %>
 <%@ Assembly Name="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
 <%@ Import Namespace="Microsoft.SharePoint.Utilities" %>
 <%@ Import Namespace="Microsoft.SharePoint" %>
 <% Response.ContentType = "text/xml"; %>

  (5).在 .disco 文件中,修改合同引用和 SOAP 地址标记,使其类似于如下示例,以便通过 Microsoft.SharePoint.Utilities.SPHttpUtility 类的使用,将文本路径替换为代码生成的路径,同时替换在 binding 属性中指定的方法名称:
 <contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request) + "?wsdl"),Response.Output); %>
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>
xmlns="http://schemas.xmlsoap.org/disco/scl/" />
<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>
xmlns:q1="http://tempuri.org/" binding="q1:HelloWorld" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>
xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
 
  (6).在 .wsdl 文件中,对指定的 SOAP 地址进行以下类似的替换:
  <soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

3.将Web服务文件部署到 _vti_bin 目录
 _vti_bin http://www.cnblogs.com/yungboy/admin/file://program/ Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI 目录,后者包含用于 Windows SharePoint Services 的默认 Web 服务文件。将新的 SPCustomServicewsdl.aspx 和 SPCustomServicedisco.aspx 文件及 SPCustomService.asmx 以及dll文件复制到 ISAPI 文件夹中。
4.打开位于 \Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI 中的 spdisco.aspx 文件,并添加下面的代码,这可以为您的 Web 服务指定 .asmx 文件:
 <contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/SPCustomService.asmx?wsdl"), Response.Output); %>
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/SPCustomService.asmx"), Response.Output); %>
xmlns=" http://schemas.xmlsoap.org/disco/scl/ " />
<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/SPCustomService.asmx?disco"),Response.Output); %>
xmlns="http://schemas.xmlsoap.org/disco/" />

5.调用服务
 输入http://服务器:端口号/_layouts/SPCustomService.asmx,测试服务,重起IIS

参考:http://msdn.microsoft.com/zh-cn/library/ms464040.aspx