Here we will use SharePoint object model to retrieve the data in InfoPath 2010. Follow below steps:
First
Design a blank infopath form (Start -> All Programs -> Microsoft
Office -> Microsoft InfoPath Designer 2010, then in the form Select
Blank Form from the Popular Form Templates and click on Design Form).
Then
in the form Put a Button and a Repeating table from the Controls tab.
You can change the button control properties like ID and Text etc. Also
you can add the column names into the repeating table. So the form
should like below:
Now
we will change the fields in the repeating table by right click on the
field and click on Text Box properties and then in the properties
window, you can change the Field name. and also can make this to
readonly. Do this for the 3 columns (3 textboxes). like below:
One you rename the fields, the fileds under the myFields should look like below:
Here
we will do one more modifications to the repeating table. By default in
repeating table you can add new records in the infopath form. But here
we only need need to show the records in the infopath form. so to
disable that, right click on the repeating table click on repeating
table properties, there deselect the checkbox "Allow user to insert and
delete rows" as shown in the fig below. Then click on Ok.
Now the above steps finishes our design of the form. Now to write code follow the below steps:
Right
click on the button, go to Button Properties... From the Properties
window click on Edit Form Code button... This will open visual studio
where we will write the code.
Here we will use SharePoint object model.
So first add reference to the MicroSoft.SharePoint.dll by going to the
References folder and right click and click add references. The dll will
be available under .Net category.
Once added then write the using statement like below:
using Microsoft.SharePoint;
Write below code in the button click event:
public void btnGetEmployees_Clicked(object sender, ClickedEventArgs e)
{
string myNamespace = NamespaceManager.LookupNamespace("my");
using (SPSite thisSite = new SPSite(ServerInfo.SharePointListUrl.ToString()))
{
using (SPWeb web = thisSite.OpenWeb())
{
SPList listEmployees = web.Lists["Employees"];
SPQuery objquery = new SPQuery();
SPListItemCollection items = listEmployees.GetItems(objquery);
if (items.Count > 0)
{
foreach (SPListItem item in items)
if (item != null)
{
using (XmlWriter writer =
MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:group1",
NamespaceManager).AppendChild())
{
writer.WriteStartElement("group2", myNamespace);
writer.WriteElementString("rptEmployeeID", myNamespace, item["Title"].ToString());
writer.WriteElementString("rptFirstName", myNamespace, item["fname"].ToString());
writer.WriteElementString("rptLastName", myNamespace, item["lname"].ToString());
writer.WriteEndElement();
writer.Close();
}
}
}
}
}
}
Point to remember here:
-
While coding for the WriteElementString you should follow the same
order in which the fields are defined in the infopath form fields.
Example if you will change order then it will not work.
writer.WriteElementString("rptFirstName", myNamespace, item["fname"].ToString());
writer.WriteElementString("rptEmployeeID", myNamespace, item["Title"].ToString());
writer.WriteElementString("rptLastName", myNamespace, item["lname"].ToString());
Now
you can save the form and deploy the form. While deploying if you got
this error "The form template cannot be published to the server.
Sandboxed solutions are not enabled on the SharePoint server farm". Then
follow this article to resolve.
Once successfully deployed the data should come like below:
Comments