Thursday, July 12, 2007

WebService in Sharepoint

One of the majour part which I have worked in my project is connecting webpart and web service.

May be this article will give a an Idea of how we can Add a asmx page into sharepoint
and also how to use the existing web service in Share point .


1) Create an ASP.NET Web service in Microsoft Visual Studio 2005.
Create a class library within the Web service that defines the programming logic for the Web service.


Create a strong name for the class library:
add your assembly to the global assembly cache (GAC),
Open TestFiles.asmx page Remove the CodeBehind attribute and Add the class
<%@ WebService Language="C#" Class="Files,clsUploadService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c741d2e07f0e3abc" %>


where "Files" represents the class name that you provide in Files.cs and " clsUploadService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c741d2e07f0e3abc" is the one which we get from Reflector.exe ( this procedure is similar to the process of getting assembly name )

2) Generate and edit a static discovery file and a Web Services Description Language (WSDL) file.
To provide discovery and description for your custom Web service, you must create a .disco file and a .wsdl file
In Windows Explorer, copy the TestFiles.asmx file of your Web service to \\ Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS.
Disco.exe at the command promp ( from Microsoft tools - command) disco http: // mytestwebsite:4704 / _layouts/TestFiles.asmx
( mytestwebsite:4704 is the server name ) after this open the wsdl and disco files with this link
http://mytestwebsite:4704/_layouts/TestFiles.asmx?disco
http://mytestwebsite:4704/_layouts/TestFiles.asmx?wsdl
save both file as aspx files TestFilesdisco.aspx and TestFilesWSDL.aspx
In both file replace 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"; %>



3) replaces literal paths with code generated paths
in disco file
docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(SPWeb.OriginalBaseUrl(Request)),Response.Output); %>
xmlns="http://schemas.xmlsoap.org/disco/scl/" />

xmlns:q1="http://tempuri.org/" binding="q1:HelloWorld" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

xmlns:q2="http://tempuri.org/" binding="q2:ServiceSoap12" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

in WSDL file
/>


4) Copying the Web Service Files to the _vti_bin Directory
The _vti_bin virtual directory maps physically to the Local_Drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI directory, which contains the default Web service files used in Windows SharePoint Services. Copy the new MyCustomWebServicewsdl.aspx and MyCustomWebServicedisco.aspx files, and also the MyCustomWebService.asmx file, to the ISAPI folder.
To verify navigate to http://mytestwebsite:4704/_vti_bin/TestFiles.asmx.

5 ) Add the New Web Service in spdisco.aspx

To make your Web service discoverable in Visual Studio as a Web service alongside the default Windows SharePoint Services Web services, open the spdisco.aspx file located in \Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI and add the following code, specifying the .asmx file for your Web service.

docRef=<% SPHttpUtility.AddQuote(SPHttpUtility.HtmlEncode(spWeb.Url + "/_vti_bin/ TestFiles.asmx"), Response.Output); %>
xmlns=" http://schemas.xmlsoap.org/disco/scl/ " />

xmlns="http://schemas.xmlsoap.org/disco/" />

To create web part which connects to WebService ( TestWebpartServices ) and returns data
add a web reference to http://mytestwebsite:4704/_vti_bin/TestFiles.asmx. and name that has mytestwebsite
public class TestWebpartServices : WebPart
{
private mytestwebsite.Files file = new global::TestWebpartServices.mytestwebsite.Files();
protected override void RenderContents(System.Web.UI.HtmlTextWriter writer)
{

file.PreAuthenticate = true;
file.Credentials = CredentialCache.DefaultCredentials;
writer.Write(file.HelloWorld());
}
}
and after this create web Part from this dll and Import this in application you can see a message "Hello World" .

when I was creating web service application in 2005 we dont have any option to edit the proxy class .i.e reference.cs file will not create .. but if we do this with the class project we can see reference.cs file




MOSS 2007 Training

Microsoft® Office SharePoint® Server 2007

I got a chance to undergo training on MOSS 2007 , since our client Palladiume USA wanted to shift to sharepoint portal .

The Training was conducted by Mr Gurjit , It was about 1 week .

Windows SharePoint Services is a versatile technology included in Microsoft Windows Server 2003 that enables organizations and business units of all sizes to increase the efficiency of business processes and improve team productivity.