Creating a custom Hello World web service in SharePoint

We need to do the following five steps

1) Create the web service.

2) Run disco.exe to get static disco and wsdl file for the web service.

3) Put asmx, disco and wsdl file to ISAPI folder.

4) Rename disco and wsdl file to aspx. Replace static url’s in them.

5) Add an entry to spdisco.aspx

Let’s proceed

· Create a new ASP.NET web service project.

· Rename Service.asmx to HWService.asmx.

· Right click it – select View Mark Up.

· And put the following mark up. Here we will be creating the web service with inline code.

<%@ WebService Language=”C#” Class=”MyInlineService.HelloWorldService” %>

using System.Web.Services;

namespace MyInlineService

{

public class HelloWorldService: WebService

{

[WebMethod]

public string HelloWorld()

{

return “Hello World”;

}

}

}

· Now Run Disco.exe for our HWService.asmx to create static disco and wsdl file.

· Open visual studio command prompt and run the following command

disco http://localhost:1814/HelloWorldSPService/HWService.asmx

· This will create HWService.wsdl and HWService.disco files at the current directory.

· Now copy all the three files HWService.asmx,HWService.wsdl and HWService.disco to ISAPI folder.

· Rename HWService.disco to HWServicedisco.aspx and HWService.wsdl to HWServicewsdl.aspx.

· Now we will open these new renamed wsdl and disco files to make the following modifications.

Open HWServicewsdl.aspx.

Replace

<?xml version=”1.0″ encoding=”utf-8″?>

with

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

And

<soap:address location=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; />

To

<soap:address location=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> />

Open HWServicedisco.aspx

Replace

<?xml version=”1.0″ encoding=”utf-8″?>

with

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

And

<contractRef ref=”http://localhost:1814/HelloWorldSPService/HWService.asmx?wsdl&#8221; docRef=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns=”http://schemas.xmlsoap.org/disco/scl/&#8221; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q1=”http://tempuri.org/&#8221; binding=”q1:HelloWorldServiceSoap” xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

<soap address=”http://localhost:1814/HelloWorldSPService/HWService.asmx&#8221; xmlns:q2=”http://tempuri.org/&#8221; binding=”q2:HelloWorldServiceSoap12″ xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

with

<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/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q1=”http://tempuri.org/&#8221; binding=”q1:HelloWorld” xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

<soap address=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>

xmlns:q2=”http://tempuri.org/&#8221; binding=”q2:ServiceSoap12″ xmlns=”http://schemas.xmlsoap.org/disco/soap/&#8221; />

· Adding it’s entry in spdisco.aspx

Open the spdisco.aspx and add the following entry

<contractRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?wsdl”), Response.Output); %>

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx”), Response.Output); %>

xmlns=” http://schemas.xmlsoap.org/disco/scl/ />

<discoveryRef ref=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + “/_vti_bin/HWService.asmx?disco”),Response.Output); %>

xmlns=”http://schemas.xmlsoap.org/disco/&#8221; />

Now your web service should be availabe to be used within sharepoint context

http://servername:port/_vti_bin/HWService.asmx.

That’s it!!


Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

5 thoughts on “Creating a custom Hello World web service in SharePoint”

  1. Be careful if you are reading this page in Firefox. Some of the text seems to get clipped. The text is there, you just can’t see it. Copy/paste may or may not work. View source will work, but IE seems to be your best bet on this page.

    Like

Share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s