get data from sharepoint list datasource in infopath programmatically

A short code snippet to get the data from a datasource on change event of a drop-down list in Infopath 2010 form.

In the code below the method InternalStartup() is called to register the event change for the dropdown and the method mydropdown_Changed() is called to access the actual datasource "MyDATASOURCE" for retrieving the values for Field1 or sharepoint Title column from the sharepoint list datasource(MyDATASOURCE).

The parameter passed to the Datasource via the webservice is ID of the drop-down item.

using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;

namespace Getdatanamespace
{
public partial class FormCode
{
public void InternalStartup()
{
EventManager.XmlEvents["/my:myform/my:table1/my:mydropdown"].Changed += new XmlChangedEventHandler(mydropdown_Changed);
}

public void mydropdown_Changed(object sender, XmlEventArgs e)
{

XPathNavigator form = MainDataSource.CreateNavigator();

// Set parameter for web service request and query connection
DataSources["MyDATASOURCE"].CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:queryFields/q:SharePointListItem_RW/q:ID",NamespaceManager).SetValue(e.NewValue); ->> sending the ID as a parameter to the webservice call which will access the datasource

DataSources["MyDATASOURCE"].QueryConnection.Execute();

// Create navigator on web service response
XPathNavigator resultnav = DataSources["MyDATASOURCE"].CreateNavigator();

// Set fields with values from web service response
XPathNavigator Field1 = form.SelectSingleNode("/my:myform/my:table1/my:field1", NamespaceManager);
Field1.SetValue(resultnav.SelectSingleNode("/dfs:myFields/dfs:dataFields/d:SharePointListItem_RW/d:Title",NamespaceManager).Value);
}

}
}

0 comments:

Post a Comment

Disclaimer

This is a personal weblog. The opinions expressed here represent my own and not those of my employer or anyone else. Should you have any questions or concerns please e-mail me at sharepointprogrammingblogger@gmail.com .

Copyright (c) 2010 @ myshaepointwork.blogspot.com. All rights are reserved.Do Not Copy.

@ Learning SharePoint.com