DCSIMG
Writing Custom Web Services for SharePoint - Gady Elkarif's Blog

Gady Elkarif's Blog

Writing Custom Web Services for SharePoint

Writing Custom Web Services for SharePoint

As a SharePoint developer, you may want to use some of the capabilities of Windows SharePoint Services to create custom Web services of your own.

In this sample I will show you how to use the SharePoint to create a custom Web service inside SharePoint.

Writing a Custom Web Service

Create new ASP.NET Web Service Application (MySPServices) as shown in the following figure:

NewProject

Next, add new Service Library (MySPServicesLibrary) project to the ASP.NET Application:

AddNewProject

Add to MySPServicesLibrary project a reference to System.Web.Services.

Rename Class1.cs in MySPServicesLibrary project to MySPService.cs, and insert your web service code. In this sample the following code describes MySPService.cs:

    1 using System;

    2 using System.Collections.Generic;

    3 using System.Linq;

    4 using System.Text;

    5 using System.ComponentModel;

    6 using System.Web.Services;

    7 

    8 namespace MySPServicesLibrary

    9 {

   10     /// <summary>

   11     /// Summary description for Service1

   12     /// </summary>

   13     [WebService(Namespace = "http://tempuri.org/")]

   14     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

   15     [ToolboxItem(false)]

   16     public class MySPService : System.Web.Services.WebService

   17     {

   18 

   19         [WebMethod]

   20         public void AddEntity(Entity customer)

   21         {

   22 

   23         }

   24     }

   25 

   26     public class Entity

   27     {

   28         public string Name { get; set; }

   29     }

   30 }

In MySPServicesLibrary, righclick on the project name and select properties, then select Signing tab, for signing the assembly:

Signing

Build MySPServicesLibrary project.

Open Visual Studion Command Prompt, and type the following command:

sn -Vr MySPServicesLibrary.dll

This will result a Public Key Token. View the markup of the MySPService.asmx (Right Click –> View Markup) and replace the following line:

<%@ WebService Language="C#" CodeBehind="Service1.asmx.cs" Class="MySPServices.Service1" %>

With this line, and update the PublicKeyToken you get from using the sn tool.

<%@ WebService Language="C#" Class="MySPServicesLibrary.MySPService, MySPServicesLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=55D195EC7508F2D2" %>

In MySPServices application, removes Service1.asmx.cs, and rename Service1.asmx to MySPService.asmx.

Add reference from MySPServices application to MySPServicesLibrary.dll and test that your web service is working.

Generating and Modifying Static Discovery and WSDL Files

Copy MySPServicesLibrary.dll into the GAC (C:\WINDOWS\assembly).

Open the Visual Studio .NET command prompt, and go to the LAYOUT directory:

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS

Copy MySPService.asmx into this directory.

Run the following command:

disco.exe /MySPService.asmx">/MySPService.asmx">http://<your service url>/MySPService.asmx

The output is two files:

  • MySPService.disco
  • MySPService.wsdl
Open CustomerService.disco and CustomerService.wsdl, and replace the first line in both files:
<?xml version="1.0" encoding="utf-8"?>
with the following content:

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

 

Rename the files MySPService.disco to MySPServicedisco.aspx and MySPService.wsdl to MySPServicewsdl.aspx .

Updating MySPServicedisco.aspx

In the .disco file, replace the contactRef and soap tags with the following content:

 

        <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=""

          <% 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://schemas.microsoft.com/sharepoint/soap/" binding="q1:MySPServiceSoap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

          <soap address=""

          <% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %> xmlns:q2="http://schemas.microsoft.com/sharepoint/soap/" binding="q2:MySPServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

        </discovery>

Updateing MySPServicewsdl.aspx

In the .wsdl file, replace soap:address and soap12:address tags with the following content:

  <wsdl:service name="MySPService">

    <wsdl:port name="MySPServiceSoap" binding="tns:MySPServiceSoap">

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

    </wsdl:port>

    <wsdl:port name="MySPServiceSoap12" binding="tns:MySPServiceSoap12">

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

    </wsdl:port>

Copy the files MySPServicedisco.aspx MySPServicewsdl.aspx and MySPService.aspx into directory: C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

Goto the following URL to verify the web service successfully created:

http://MyServer/_vti_bin/MySPService.asmx

http://MyServer/_vti_bin/MySPService.asmx?wsdl

http://MyServer/_vti_bin/MySPService.asmx?disco

Consuming the SharePoint Web Service

Create new console application project and add a web reference to the new custom web service. The following code show how I can use the web service I created:

    1         static void Main(string[] args)

    2         {

    3             try

    4             {

    5                 MySPServiceSoapClient svc = new MySPServiceSoapClient();

    6 

    7                 Entity entity = new Entity()

    8                 {

    9                     Name = "SampleEntity"

   10                 };

   11 

   12                 svc.AddEntity(entity);              

   13 

   14                 svc.Close();

   15             }

   16             catch (Exception ex)

   17             {

   18             }

   19         }

If you get the following security exception:

The HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'Negotiate,NTLM'.

Go to the client app.config and change the security settings of the WCF binding as follows:

                    <security mode="TransportCredentialOnly">

                        <transport clientCredentialType="Windows" proxyCredentialType="Windows"

                            realm="" />

                        <message clientCredentialType="UserName" algorithmSuite="Default" />

                    </security>

פורסם: Mar 31 2009, 01:27 PM by egady | with 23 comment(s)
תגים:,

תוכן התגובה

Topics about Culture » Writing Custom Web Services for SharePoint כתב/ה:

Pingback from  Topics about Culture  &raquo; Writing Custom Web Services for SharePoint

# March 31, 2009 3:19 PM

Mat כתב/ה:

Do we need to do the disco step is asmx is part of the project? I have situation where asmx is part of the code and I am trying to call the asmx from a User control rendered through a webpart. I am getting hit with a authorization required.

# April 6, 2009 9:43 PM

egady כתב/ה:

It's not part of the project, you do it manually.

# April 12, 2009 4:05 PM

Mark כתב/ה:

How can this be packaged up and deployed as a feature so that we don't have to manually copy it all over the place and more easily maintain it?

# April 22, 2009 6:59 PM

Mark Wagner כתב/ה:

Excellent posting and explanation.  After 3 years of modifying web service files for SharePoint I found it tedious, so I wrote a utility that will automatically generate the disco.aspx and wsdl.aspx files for your web service.  I encourage you to check it out.

www.crsw.com/.../Post.aspx

# April 23, 2009 4:31 AM

Mark Wagner כתב/ה:

After creating your custom web service you can Automatically Generate the SharePoint disco.aspx and wsdl.aspx files using the SPDev utility found here.  Works great and takes only seconds. www.crsw.com/.../Post.aspx

# May 5, 2009 7:36 AM

Web Design Edinburgh - Website Design, SEO, eCommerce, Internet Marketing in Edinburgh כתב/ה:

Using organic SEO, this is not going to be possible for you to achieve and do not believe anyone who tells you it is. In a situation like this when you are not willing to wait for organic SEO to take its course you should look at Pay Per Click (PPC) Advertising

# August 14, 2009 9:05 PM

C320bee Radiator For Sale, Find Streetpilot C320 כתב/ה:

Pingback from  C320bee Radiator For Sale, Find Streetpilot C320

# May 21, 2010 10:47 AM

240sx Sclc, 240sx Blurb כתב/ה:

Pingback from  240sx Sclc, 240sx Blurb

# May 21, 2010 9:07 PM

Oldsmobile Cutlass Cruiser Radiator Chevrolet Corsica, Gatwick To Corsica כתב/ה:

Pingback from  Oldsmobile Cutlass Cruiser Radiator Chevrolet Corsica, Gatwick To Corsica

# May 22, 2010 3:57 AM

Oe Replacement Dodge B300, Buy Selectivend Cb300 Cold Drink כתב/ה:

Pingback from  Oe Replacement Dodge B300, Buy Selectivend Cb300 Cold Drink

# May 22, 2010 1:02 PM

Relay 2 Factory Warranty Information Chevrolet Uplander, Lkq Auto Parts Orlando - 433.rkwrh.com כתב/ה:

Pingback from  Relay 2 Factory Warranty Information Chevrolet Uplander, Lkq Auto Parts Orlando - 433.rkwrh.com

# May 24, 2010 8:50 PM

Alero Part Including Achieva Oldsmobile Toronado, Valero Radiator Aluminum - 251.1fh.org כתב/ה:

Pingback from  Alero Part Including Achieva Oldsmobile Toronado, Valero Radiator Aluminum - 251.1fh.org

# May 24, 2010 9:50 PM

Volkswagen Golf Rabbit 5 Door Hatchback Buying Check, 2008 Volkswagen Rabbit Colors - 482.tgrconversions.com כתב/ה:

Pingback from  Volkswagen Golf Rabbit 5 Door Hatchback Buying Check, 2008 Volkswagen Rabbit Colors - 482.tgrconversions.com

# May 25, 2010 3:44 AM

626 Aftermarket Mazda Heater, Asus Mypal A626 User - 103.computeronlinebingo.com כתב/ה:

Pingback from  626 Aftermarket Mazda Heater, Asus Mypal A626 User - 103.computeronlinebingo.com

# May 25, 2010 9:15 AM

370z Car Nissan 350z, 350z Used Automotive - 407.computeronlinebingo.com כתב/ה:

Pingback from  370z Car Nissan 350z, 350z Used Automotive - 407.computeronlinebingo.com

# May 25, 2010 11:30 AM

Aftermarket D450 Dodge Ram 3500 Grand Caravan, Es350 Game - 370.tgrconversions.com כתב/ה:

Pingback from  Aftermarket D450 Dodge Ram 3500 Grand Caravan, Es350 Game - 370.tgrconversions.com

# May 25, 2010 1:42 PM

Scion Engine Light Malfunction, Daihatsu Sirion Model 2003 - 332.animejin.com כתב/ה:

Pingback from  Scion Engine Light Malfunction, Daihatsu Sirion Model 2003 - 332.animejin.com

# May 25, 2010 2:08 PM

Cj5a Original Series, Cj5a Replacement Wheel Cylinder Jeep Cj7 - 276.an74.com כתב/ה:

Pingback from  Cj5a Original Series, Cj5a Replacement Wheel Cylinder Jeep Cj7 - 276.an74.com

# May 26, 2010 12:50 AM

1981 - 2001 @ Marauder Episodes Online, Aftermarket Villager Parts Marauder Mercury Ln7 - 324.eumreborn.com כתב/ה:

Pingback from  1981 - 2001 @ Marauder Episodes Online, Aftermarket Villager Parts Marauder Mercury Ln7 - 324.eumreborn.com

# May 31, 2010 9:32 PM

I’m having a problem with referencing a dll from a web service. - Question Lounge כתב/ה:

Pingback from  I&#8217;m having a problem with referencing a dll from a web service. - Question Lounge

# January 6, 2011 8:29 AM

I’m having a problem with referencing a dll from a web service. | NetMasti.com The inside stories - BUZZ Service כתב/ה:

Pingback from  I&#8217;m having a problem with referencing a dll from a web service. | NetMasti.com  The inside stories - BUZZ Service

# January 6, 2011 11:45 AM

traffic travis 4 discount כתב/ה:

Pingback from  traffic travis 4 discount

# April 19, 2011 12:53 AM
שלח תגובה

(שדה חובה)  

(שדה חובה)  

(אופציונלי)

(שדה חובה) 

Please add 5 and 5 and type the answer here:


Enter the numbers above: