How to customize WebService in Moss

Source: Internet
Author: User

All WebServices provided in Moss are placed in virtual directory _ vti_bin, and the corresponding physical directory isC: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ ISAPI. 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. The project "right-click --- properties --- signature --- isProgramSet signature ", no password is used. Service. CS is the logic of the actual WebService,CodeAs 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.

There are two methods:

1.Drag mosslibrary2.dll in the bin directory% WINDOWS % \ assemblyFolder.
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>".

III: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 logic file of the specified service. asmx uses the code in service. Cs in the mosslibrary2 project.

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

1. Copy Service. asmxC: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ template \ layouts directory, open the command line tool of vs2005, and use 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:
<% @ 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

Copy servicedisco. aspx, servicewsdl. aspx, and service. asmxC: \ Program Files \ common files \ microsoft shared \ Web Server Extensions \ 12 \ ISAPI directory. Then we can check whether the deployment is successful through the following address.Http: // carysun/_ vti_bin/service. asmx.

For example:

Vi. Client call

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

 
Carysun. Service Se =NewWindowsapplication1.carysun. Service ();
Se. usedefacrecredentials =True;
MessageBox. Show (SE. getsitelistcount ());

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

The final effect is:

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.