Infopath 2010 form binding data to dropdownlist from web service

A short code snippet to get the data from the web service and binding to dropdownlist.

First of all create a web service that returns a data set ,create a dataconnection from infopath that retrieves data from web service and binding data source to drop down list.

 

1) Creating web service

[WebMethod]
public DataSet RegCust()
{
string returnstr = “Insert failed”;
SqlConnection dbConn = new SqlConnection(“database=ims;server=192.168.0.111;uid=sa;password=@System”);
string sqlstr = “select productid,productname,unitprice,numofitems,TotalAmount from tblorderdetails”;
SqlCommand dbComm = new SqlCommand(sqlstr, dbConn);
try
{
dbConn.Open();
SqlDataAdapter da = new SqlDataAdapter(dbComm);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
returnstr = “Insert succeeded”;
}
returnstr = “Insert succeeded”;
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
dbConn.Close();
return ds;
}

Debug this and copy the URL:http://localhost:1983/getCompanyName.asmx

Image

2)Create dataconnection to retrive data from web service using infopath 

Steps:1) Click on “Data-Data Connections” from top ribbon

Image

2)Add a new connection from the DataConnections wizard

Image

3) Select operation Retrive 

Image

4)Select a source type,since it is a web service..we select first option

Image

5)Get the web servie details using the URL Copied from the above stepImage

7)Select the method that returns datasetImage

8)Clcik next and next

 

Binding Data Source to DropDownList

1)Right click on the drop down list and select “drop down list properties” and select “Get choices from the external data sources”

Image

 

2)Select the field that you want to display in the drop down list as follows

Image

3)Preview to see the output

Advertisements

About alekhyakodamsharepoint

Hi.. I am alekhya working as a sharepoint developer.
This entry was posted in Infopath 2010. Bookmark the permalink.

One Response to Infopath 2010 form binding data to dropdownlist from web service

  1. aaedla says:

    Nice Post Alekhya,

    I have created web service to get one group users list from AD and created data connection to show users in infopath form drop down list but I am not getting any values. I followed every step as you mentioned above, the only difference is I am trying to get users from Active Directory. I am getting static but not dynamic, means I have added test users in code.

    I got data set when I tried to invoke web service on server. Can you please let me know If I am doing some thing wrong? Any help would be appreciated.

    Here I am attaching my web service code:

    public string GetProperty(SearchResult searchResult, string propertyName)
    {
    if (searchResult.Properties.Contains(propertyName))
    {
    return searchResult.Properties[propertyName][0].ToString();
    }
    else
    {
    return string.Empty;
    }
    }

    public DataTable CreateDataTable()
    {
    DataTable dtUsers = new DataTable();
    dtUsers.Columns.Add(“UserName”);
    return dtUsers;
    }
    [WebMethod]
    public DataSet GetActiveDirectoryUsers()
    {
    DirectoryEntry entry = null;
    DirectorySearcher searcher = null;
    DataSet dsUsers = null;

    try
    {
    DataTable dtUsers = new DataTable();
    dtUsers.Columns.Add(“UserName”);

    DataRow row1 = dtUsers.NewRow();
    row1[“UserName”] = “ashok”;
    dtUsers.Rows.Add(row1);
    dsUsers = new DataSet();
    dsUsers.Tables.Add(dtUsers);

    entry = new DirectoryEntry(“LDAP://OU=XXX_USERS,OU=XX_XX,DC=XXXXX,DC=com”);
    searcher = new DirectorySearcher(entry);
    //searcher.SearchScope=SearchScope.Subtree;
    searcher.Filter = “(&(objectClass=user))”;
    SearchResultCollection resultColl = searcher.FindAll();
    if (resultColl.Count > 0)
    {
    //DataTable dtUsers = new DataTable();
    //dtUsers.Columns.Add(“UserName”);
    foreach (SearchResult resultSet in resultColl)
    {
    if (!string.IsNullOrEmpty(GetProperty(resultSet, “title”)) && !string.IsNullOrEmpty(GetProperty(resultSet, “Manager”)))
    {
    DataRow row = dtUsers.NewRow();
    row[“UserName”] = GetProperty(resultSet, “Name”);
    dtUsers.Rows.Add(row);
    }
    }
    }
    }
    catch
    {
    }
    finally
    {
    if (searcher != null) searcher.Dispose();
    if (entry != null) entry.Dispose();
    }

    return dsUsers;
    }

Leave a Reply

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