Thursday, June 12, 2008

InfoPath using Web Service Data connection

Hi,

Read the following scenario you probably come across while development InfoPath using Web Service Data Connection. (Here I assume you know how to create Web Service Data connection using InfoPath 2007)

I have created one InfoPath form in my local machine, where I am filling dropdown from web service of my local machine,

For that I am creating data connection library, but it takes design time web service URL reference in data connection.

Now I want to publish this form to server, but on server it is not working because it considers local web service URL.

On server I have created same data connection library and change the web service reference in all .udcx file but still form template take web reference which I have added design time.

Because In my local machine web service URL is different and on server web service URL is different.

When I try to see preview of my InfoPath template on server it shows following message

"One or more data connection in your form are specified with server-relative link, but no published location has been defined for the form. Data connection will retrieve data from the server specified in design time. To enable server-relative link publish your form to Microsoft Office server"

I have divided this solution in two parts.

1. Create Web service Project (i.e. here I have created web service as Library it will generate .dll file assembly for web service not web service as web application) .Here we will use Web Service Template if you don’t have install this template please download it to create Web Service Project. You can see template in picture.


Now create project like this


Give Strong name to this Assembly.

Deploy it into a GAC .Now open MyService.asmx file in Notepad and replace <%@WebsService /> Tag with following according to your project here is one sample.

<%@ WebService Language="C#" Class="SharePointKings.MyService, SharePoiintKings, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=bce3f92a230905aa" %>

Now create one Directory in 12/Template/Layouts Directory Name in Our case we will create

Directory “SharePointKings” under this directory put .dll from your project in our case it will be SharePointKings.dll.

Now from RUN write “inetmgr” now suppose your site Directory name is “2020” expand this site go to Layouts under Layouts now you will find directory called “SharePointKings” as we created it under 12/Template/Layouts so it will listed here. Now create virtual directory SharePointKings from here.

In first part we have created our web service as Assembly and configure it to use in InfoPath Web service data connection.

2. Now here I assume you know how to create Web service data connection and you want to populate dropdown list from web service.

When you open InfoPath Code Project and from this code project right click on project add Web Reference.

As we have assume that our site port is “2020” so our site URL is like this “http://servername:2020” and add reference http://servername:2020/_layouts/SharePointKings/Myservice.asmx now all web methods will be listed there. Name this web reference “MyService”

Ok now as you add this web reference you will find few additional files created in your InfoPath Code Project in which one is “Reference.cs” open this file you will find default constructor now add second additional constructor which take parameters for Web Reference URL.

public MYService(string WebseriveUrl)

{

this.Url = WebseriveUrl;

if ((this.IsLocalFileSystemWebService(this.Url) == true))

{

this.UseDefaultCredentials = true;

this.useDefaultCredentialsSetExplicitly = false;

}

else

{

this.useDefaultCredentialsSetExplicitly = true;

}

}

Now in your InfoPath Form Load Event Write Following code.

First we Create Dynamic URL For WebService like this so it works for our testing server as well in production server we don’t need to change any thing in code so, here we are creating dynamic URL though we have given hard coded refrecnce while adding web referce.

public void FormEvents_Loading(object sender, LoadingEventArgs e)

{

try

{

string webserviceurl = string.Empty;

webserviceurl = SPContext.Current.Site.Url + "/_layouts/SharePointKings/MYService.asmx"; ExecuteConnection("YourConnectionName",webserviceurl);

}catch(Exception ex){throw ex;}

}

private void ExecuteConnection(string ConnectionName,string WebServiceUrl)

{

WebServiceConnection cn = (WebServiceConnection)this.DataConnections[ConnectionName];

string url = WebServiceUrl;

UriBuilder uri = new UriBuilder(url);

cn.ServiceUrl = uri.Uri;

cn.Timeout = 60;

cn.Execute();

}

This way you can execute Web service connection yousing dynamic web service URL.you don’t need to change any code while deploying your code to production server .

download in word format

2 comments:

Anonymous said...

hi this is very good but i am not able to apply this i donot understood

"ExecuteConnection("YourConnectionName",webserviceurl);"
what i have to give in place of "YourConnectionName"

thanks

Jayesh Prajapati said...

Hi,
To create a connection,follow this steps.
1.Open InfoPath Form.
2.click on Design task button.
3.it will open new pan on right site.
4.click Data Source link.
5.Under "Action" Section you will find Manage data connection click it.
6.new dialog box will open click on add button.
7.Create New Connection with Submit data.
8.select web service data connection options.
9.follow rest of the steps
10.hope you will find comfortable.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com