Sunday, February 10, 2013

InfoPath


How to fill values (Data) Repeating Table in InfoPath?

Hi All,
Here is sample code to fill value in repeating table at runtime (Dynamically)
Suppose you have repeating table Name “tblCustomer” and has two column name “Text” and “value”
Here is code to fill value from database or from web service.
Repeating table structure in data source like this


private void fillRepeatingTable(string table, string tr)
{
string value, Text;
value = string.Empty;
Text = string.Empty;
DataTable dt = null;
//this is method that Will return DataTable
dt = this.CustomerDataSource();
//cloumn name Value in Repeating Table
value = "//my:Value";
//column name Text in Repeating Table
Text = "//my:Text";

XPathNavigator root = this.MainDataSource.CreateNavigator();
XPathNavigator row = root.SelectSingleNode("//my:" + tr + "[1]", NamespaceManager);
// make a copy of the node by cloning it
XPathNavigator row1;
if (dt != null)
{
foreach (System.Data.DataRow dr in dt.Rows)
{
// set the new values of the row’s fields
row1 = row.Clone();
row1.SelectSingleNode(value, NamespaceManager).InnerXml =Convert.ToString(dr["Value"]);
row1.SelectSingleNode(Text, NamespaceManager).InnerXml = Convert.ToString(dr["Text"]).Replace("&", " "); ;
XPathNavigator parent = row1.SelectSingleNode("//my:" + table, NamespaceManager);
parent.AppendChild(row1);
}
row.DeleteSelf();
}
}



Access denied error when I use web services in InfoPath

Hi All,
When I use user profile service or any other web service in InfoPath and run it on my local server it works fine and when I deploy same form on production server where there is SharePoint farm it fails to run and give access denied error. I have used host header for SharePoint site and I refer that host header to open that site.
The solution for this problem is to disable the loopback check on each SharePoint server in farm.
To set the DisableLoopbackCheck registry key yourself, follow these steps:
Set the DisableStrictNameChecking registry entry to
  1. For more information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:
    (http://support.microsoft.com/kb/281308/ ) Connecting to SMB share on a Windows 2000-based computer or a Windows Server 2003-based computer may not work with an alias name
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry key:
    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
  4. Right-click Lsa, point to New, and then click DWORD Value.
  5. Type DisableLoopbackCheck, and then press ENTER.
  6. Right-click DisableLoopbackCheck, and then click Modify.
  7. In the Value data box, type 1, and then click OK.
  8. Quit Registry Editor, and then restart your computer.
    And run IISRESET on each server
    Reference: http://support.microsoft.com/kb/896861


InfoPath Fill Drop-down List Box dynamically (Run time)

Hi All,
Some time you need to fill Drop-Down List Box of InfoPath runtime from database or webservice.
here is solution and demo code will help you to solve this purpose. this solution is best when you are trying to bind small amount of data to drop-down.
here I have taken Customer drop-down and it will fill runtime from Repeating table.
Here in image you can see we have Customer drop-down and below it one Repeating table.
  1. Open InfoPath.
  2. Click on design new template.
  3. drag dropdown on the InfoPath Form and give name "Customer"
  4. drag repeating table on InfoPath form.
  5. now go to the data source pane and rename the binding shown in the figure.
  6. Now, double click on customer Drop-down, In Data tab List box entry section select radio button “lookup value in the Form’s data source”.
  7. In Entries select “tblCustomer”
  8. Value : myValue , Text: myText.
  9. And then Hide Repeating table from Conditional Formation in Display tab.
  10. Download the Code (Click Here)
  11. Here is Main Code


private void fillDropDown(string table, string tr, string DropDownName)
{
string value, Text;
value = string.Empty;
Text = string.Empty;
DataTable dt = null;
//if you have more than one Dropdown you can use switch case
switch (DropDownName)
{
case "Customer":
//here you can use webservice to fill dropdown..
dt = this.CustomerDataSource();
value = "//my:Value";
Text = "//my:Text";
break;
}
XPathNavigator root = this.MainDataSource.CreateNavigator();
XPathNavigator row = root.SelectSingleNode("//my:" + tr + "[1]", NamespaceManager);
// make a copy of the node by cloning it
XPathNavigator row1;
if (dt != null)
{
foreach (System.Data.DataRow dr in dt.Rows)
{
// set the new values of the row’s fields
row1 = row.Clone();
row1.SelectSingleNode(value, NamespaceManager).InnerXml =Convert.ToString(dr["Value"]);
row1.SelectSingleNode(Text, NamespaceManager).InnerXml = Convert.ToString(dr["Text"]).Replace("&", " "); ;
XPathNavigator parent = row1.SelectSingleNode("//my:" + table, NamespaceManager);
parent.AppendChild(row1);
}
row.DeleteSelf();
}
}

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 referencehttp://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


No comments:

Post a Comment