Custom WebService in Moss

Source: Internet
Author: User

(Transfer)

 

All WebServices provided in Moss are placed in the virtual directory _ vti_bin, and the corresponding physical directory is C: Program filescommon filesmicrosoft sharedweb server extensions12isapi. You may think the directory _ vti_bin name is a bit strange. The name is from Vermeer technologies inconfigurated of the company. The company's only product is FrontPage, which was acquired by Microsoft in 1996.

The following steps are required to implement a WebService:

1. Create a WebService Project

1. Use vs2005 to create a webserivce project to implement our WebService. Then I add a class library to implement the logic of WebService. The project structure is as follows:

Sign the mosslibrary2 class library, right-click the project --- properties --- signature --- sign the Assembly ", and no password is used. Service. CS is the logic of the actual WebService. The Code is as follows:

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
  public Service ()
  {  }
  [WebMethod]
  public string HelloWorld()
  {
    return "Hello World";
  }
  [WebMethod]
  public string GetSiteListCount()
  {
    SPWeb myWeb=SPContext.Current.Web;
    SPListCollection lists=myWeb.Lists;
    return (myWeb.Title + " contains " + lists.Count.ToString() + " Web sites.");
  }  
}

 

2. Add the mosslibrary2 class library to GAC (if put in Bin, skip the following operations)

There are two methods:

1. Drag mosslibrary2.dll in the bin directory to the % WINDOWS % Assembly folder.

2. Open the command line tool vs2005and use the gacui.exe tool. The command is as follows:

Gacutil.exe-if "<full file system path to DLL> ".

3. Modify the service. asmx File

<%@ WebService Language="C#" Class="MyServiceClass, MyServiceAssembly, Version=1.0.0.0,
                     Culture=neutral, PublicKeyToken=8f2dca3c0f2d0131" %>

You can find mosslibrary2.dll in the % WINDOWS % Assembly folder and right-click it to view its properties. the logical file of asmx uses the service in the mosslibrary2 project. the code in CS.

 

4. Generate the service. disco and WebService description files service. WSDL.

1. Copy Service. asmx to the C: Program filescommon filesmicrosoft sharedweb server extensions12templatelayouts directory, and then open the command line tool of vs2005 and run the following command:

Disco http: // carysun/_ layouts/service. asmx

The service. disco and service. WSDL files are generated.

2. Set <? XML version = "1.0" encoding = "UTF-8"?> Replace the statement with the following statement: (these statements can be found on the ISAPI directory page)

<%@ 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"; %>

 

In fact, the original pure XML is transformed into a page for parsing. In addition, the parsing of this page is processed by moss.

3. Set

<contractRef ref=http://carysun/_layouts/service.asmx?wsdl
docRef="http://carysun/_layouts/service.asmx" xmlns="http://schemas.xmlsoap.org/disco/scl/" />
<soap address="http://carysun/_layouts/service.asmx" xmlns:q1=http://tempuri.org/
          binding="q1:ServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />
<soap address="http://carysun/_layouts/service.asmx" xmlns:q2=http://tempuri.org/
          binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

Replace:

<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/" />
4. Set  
  
<Soap: address location = "http: // carysun/_ layouts/service. asmx"/> and
<Soap12: address location = "http: // carysun/_ layouts/service. asmx"/>

 

 

Replace:

<Soap: address location = <% sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl

(Request), response. Output); %>/>

And <soap12: address location = <% sphttputility. addquote (sphttputility. htmlencode (spweb. originalbaseurl

(Request), response. Output); %>/>

The changes to contractref and soap address are actually re-encoded with the soap query URL on the page.

Moss-hosted web services can be correctly located based on dynamic requests during runtime.

5. Rename service. disco and service. WSDL to servicedisco. aspx and servicewsdl. aspx.

5. Deploy WebService

Set servicedisco. aspx, servicewsdl. aspx and service. copy the three asmx files to the C: Program filescommon filesmicrosoft sharedweb server extensions12isapi directory. Then we can use the following address to check whether the deployment is successful. Http: // carysun/_ vti_bin/service. asmx.

For example:

6. Client call (an"Request failed due to HTTP status 401: unauthorized"Error, because IIS does not enable Anonymous Access)

Create a web site, add the WebService application, and add the following code when you click an event in the button:

  carysun.Service se= new WindowsApplication1.carysun.Service();
  se.UseDefaultCredentials = true;
  MessageBox.Show(se.GetSiteListCount());

Se. usedefacrecredentials = true; this code sets trust. Otherwise, an error indicating no permission is reported.

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.