JOJ
踏踏实实做人,认认真真做事!放纵自己就是毁灭自己!

其实关于MOSS中使用自定义WebService这种问题,google一搜会有很多很多!为什么我还要写呢? 我认为有些事情必须自己亲自做一次看到效果了,心里才踏实! 其次也当做自己的学习扎记吧! 本篇文章知识来源于 杨永刚先生的<<Sharepoint Server2007 案例实战开发>> 我不过是依葫芦画瓢而已了 :)

MOSS自带Web service

MOSS自己提供的web服务在:C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI 目录下面

几个常用的Web 服务:

  1. 列表服务 : http://localhost:80/_vti_bin/Lists.asmx  ==>可以完成列表的增删改查操作,访问列表视图和列表附件
  2. 搜索服务:http://localhost:80/_vti_bin/Lists.asmx  ==>提供Query方法,构造XML查询返回结果
  3. UserGroup服务:http://localhost:80/_vti_bin/Lists.asmx ==>用户和用户组的增删改查,数据读取,权限设定
  4. Imageing服务:http://localhost:80/_vti_bin/Lists.asmx ==>管理站的中的图片库,文档库

当然里面还有很多其他webservice了,自己去挖掘!利用这些web服务,客服端程序来操作MOSS站的而不用登录网页维护!

 

自定义Web Service

  1. 新建一个 ASP.NET Web Service 项目
  2. 编写业务逻辑方法
  3. 生成disco和wsdl文件 如何生成这两个文件呢? (使用VS2008命令提示 输入: disco http://localhost:17906/MyTestWebService/Service.asmx /out:c:\moss)
  4. 修改disco和wsdl文件,把后缀修改为 aspx
  5. 把修改后的disco和wsdl文件和asmx文件复制到 C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI 下,并把程序集放在站的下面的bin目录里面
  6. 运行
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.SharePoint;
using System.Data;
using Microsoft.SharePoint.WebControls;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public string GetTree()
    {
        string str = string.Empty;
        try
        {
            using (SPWeb web = SPControl.GetContextWeb(HttpContext.Current))
            {
                str =”MyTest:”+ web.Name;
            }
        }
        catch (Exception ex)
        {
            str = "Error Message:" + ex.Message;
        }
        return str;
    }    
}
生成的Service.disco 文件:
<?xml version="1.0" encoding="utf-8"?>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
  <contractRef ref="http://localhost:17906/MyTestWebService/Service.asmx?wsdl" docRef="http://localhost:17906/MyTestWebService/Service.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
  <soap address="http://localhost:17906/MyTestWebService/Service.asmx" xmlns:q1="http://tempuri.org/" binding="q1:ServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
  <soap address="http://localhost:17906/MyTestWebService/Service.asmx" xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
</discovery>

修改为:

<%@ 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"; %>
<discovery xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/disco/">
  <contractRef ref=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %>
 docRef=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> xmlns="http://schemas.xmlsoap.org/disco/scl/" />
  <soap address=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> xmlns:q1="http://tempuri.org/" binding="q1:SPFilesSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
</discovery>
把修改后的文件保存为: Servicedisco.aspx
 
生成的 Service.wsdl 文件
 
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
      <s:element name="HelloWorld">
        <s:complexType />
      </s:element>
      <s:element name="HelloWorldResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
      <s:element name="GetTree">
        <s:complexType />
      </s:element>
      <s:element name="GetTreeResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="GetTreeResult" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>
    </s:schema>
  </wsdl:types>
  <wsdl:message name="HelloWorldSoapIn">
    <wsdl:part name="parameters" element="tns:HelloWorld" />
  </wsdl:message>
  <wsdl:message name="HelloWorldSoapOut">
    <wsdl:part name="parameters" element="tns:HelloWorldResponse" />
  </wsdl:message>
  <wsdl:message name="GetTreeSoapIn">
    <wsdl:part name="parameters" element="tns:GetTree" />
  </wsdl:message>
  <wsdl:message name="GetTreeSoapOut">
    <wsdl:part name="parameters" element="tns:GetTreeResponse" />
  </wsdl:message>
  <wsdl:portType name="ServiceSoap">
    <wsdl:operation name="HelloWorld">
      <wsdl:input message="tns:HelloWorldSoapIn" />
      <wsdl:output message="tns:HelloWorldSoapOut" />
    </wsdl:operation>
    <wsdl:operation name="GetTree">
      <wsdl:input message="tns:GetTreeSoapIn" />
      <wsdl:output message="tns:GetTreeSoapOut" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetTree">
      <soap:operation soapAction="http://tempuri.org/GetTree" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:binding name="ServiceSoap12" type="tns:ServiceSoap">
    <soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="HelloWorld">
      <soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
    <wsdl:operation name="GetTree">
      <soap12:operation soapAction="http://tempuri.org/GetTree" style="document" />
      <wsdl:input>
        <soap12:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap12:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="Service">
    <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
      <soap:address location="http://localhost:17906/MyTestWebService/Service.asmx" />
    </wsdl:port>
    <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12">
      <soap12:address location="http://localhost:17906/MyTestWebService/Service.asmx" />
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>
修改为:
1.把<?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"; %> 
2.把 wsdl:service 节点[划线部分]修改为:
<wsdl:service name="Service"> <wsdl:port name="ServiceSoap" binding="tns:ServiceSoap"> <soap:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %> /> </wsdl:port> <wsdl:port name="ServiceSoap12" binding="tns:ServiceSoap12"> <soap12:address location=<% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %>  /> </wsdl:port> </wsdl:service> 
 把文件保存为: Servicewsdl.aspx

说明: 其实上面的就是更改地址,因为最初的是固定的地址!SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request), '"'); %>这句其实就是获取当前请求地址!其实上面修改的代码你只需要打开C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI 里面的任意的 两个.wsdl 和.disco 文件,把那段代码复制粘贴出来即可!

附:如何把程序集放到GAC呢? 用vs2008命令提示 输入 gacutil –if c:\dll\you.dll   enter 然后 iisreset [重启IIS]  enter

 

把Service.asmx,Servicewsdl.aspx,Servicedisco.aspx 这三个文件复制到C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\ISAPI目录下面,把该项目的程序集放在站点的bin目录里面(或者放在GAC目录里面)啰嗦一下,由于这里建的是webservice项目,你生成成功后看不到dll,只有把他发布网站的形式发布出来才有dll!

现在访问地址变为: http://server:80:/_vti_bin/Service.asmx  ,如果访问正常说明自定义webservice也正常了!

不过这个地址在浏览器访问,你调用webservice的方法,他不会返回东西给你! 只有通过程序添加web 引用,在调用这个webservice!

 

另一个项目:

添加web引用: 地址为:http://server:80/_vti_bin/Service.asmx

MyCus.Service mcs = new MyCus.Service();
mcs.UseDefaultCredentials = true; //注意设置这个属性为true
Response.Write(mcs.GetTree());

实践的时候遇到这个错误了:

Possible SOAP version mismatch: Envelope namespace http://schemas.xmlsoap.org/wsdl/ was unexpected. Expecting http://schemas.xmlsoap.org/soap/envelope/.

解决方案:

上面修改的地方只有一处 <% SPEncode.WriteHtmlEncodeWithQuote(Response, SPWeb.OriginalBaseUrl(Request) + "?wsdl", '"'); %>  后面加wsdl! 由于我的大意! 把所有的地方的复制为一样的了!

 

经过我的测试! 一切ok!

 

Technorati 标签: sharepoint,moss,webservice
posted on 2010-05-12 17:30  JoinJ  阅读(837)  评论(0编辑  收藏  举报