I'm building a form in InfoPath 2007 for software purchasing. I'm getting down to the final steps, and i'm not sure if i'll be able to do what i want with this last step, but it seems logical that there would be a way to do so.
My form submits (using C#) to a Sharepoint List. There's quite a bit of C# coding in the form, to retrieve information from active directory and submit repeating rows to the sharepoint list. I got everything working as far as that goes, my question is this:
On my form i have a drop down field (which is populated from a SharePoint list) which has a list of all the available software to purchase. When you select the software title (SoftwareTitle) it also populates a field with the price (SoftwarePrice) of the software. When they submit the form the software title and price populate the corrosponding columns in a sharepoint list.
I wanted to accomodate software purchases that may not be listed in out list of available software, so made a checkbox that the user can check to specify that they don't see the software in the list. Checking this box, hides the drop down field and shows a text box field where they can type in the name of the software (OtherSoftwareTitle) and the price (OtherSoftwarePrice).
Is it possible to get the two "Other" fields to populate to the same column that the regular software title and price populate to? It sounds like it would be fairly simple but i can't seem to make it work.
Now for a curve ball on the same topic. Since the software title and price are in a repeating table, if a user selects a Software title from the drop down field and then clicks to "Insert a new item" and in the second row, they check the box and enter in a software title and price, could those two fields submit to seperate rows in the sharepoint list?
I already have it setup, if the user selects a drop down field item and then inserts another row and selects another software title from the drop down, it will create to list items with the differant software names, it just seems that i can't get it to work when one of the rows is using the OtherSoftwareTitle or Price field.
Here's my code if that helps:
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.DirectoryServices;
using System.Windows.Forms;
using System.Web;
namespace Software_Request_Form_Rev2
{
public partial class FormCode
{
// Member variables are not supported in browser-enabled forms.
// Instead, write and read these values from the FormState
// dictionary using code such as the following:
//
// private object _memberVariable
// {
// get
// {
// return FormState["_memberVariable"];
// }
// set
// {
// FormState["_memberVariable"] = value;
// }
// }
// NOTE: The following procedure is required by Microsoft Office InfoPath.
// It can be modified using Microsoft Office InfoPath.
public void InternalStartup()
{
((ButtonEvent)EventManager.ControlEvents["SubmitButton"]).Clicked += new ClickedEventHandler(SubmitButton_Clicked);
EventManager.FormEvents.Loading += new LoadingEventHandler(FormEvents_Loading);
}
public void FormEvents_Loading(object sender, LoadingEventArgs e)
{
try
{
// Get the user name of the current user.
string userName = this.Application.User.UserName;
// Create a DirectorySearcher object using the user name
// as the LDAP search filter. If using a directory other
// than Exchange, use sAMAccountName instead of mailNickname.
DirectorySearcher searcher = new DirectorySearcher(
"(mailNickname=" + userName + ")");
// Search for the specified user.
SearchResult result = searcher.FindOne();
// Make sure the user was found.
if (result == null)
{
MessageBox.Show("Error finding user: " + userName);
}
else
{
// Create a DirectoryEntry object to retrieve the collection
// of attributes (properties) for the user.
DirectoryEntry employee = result.GetDirectoryEntry();
// Assign the specified properties to string variables.
string FullName = (employee.Properties["displayName"].Value != null) ? employee.Properties["displayName"].Value.ToString() : "";
string Mail = (employee.Properties["mail"].Value != null) ? employee.Properties["mail"].Value.ToString() : "";
string Location = (employee.Properties["physicalDeliveryOfficeName"].Value != null) ? employee.Properties["physicalDeliveryOfficeName"].Value.ToString() : "";
//Retrieve the Street Address from Active Directory and delimit to show only the mailstop number.
string Address = (employee.Properties["streetAddress"].Value != null) ? employee.Properties["streetAddress"].Value.ToString() : "";
string searchKey = "MS";
int searchKeyIndex = Address.IndexOf(searchKey);
string MailStop = Address.Substring(searchKeyIndex + searchKey.Length).Trim();
string Phone = (employee.Properties["telephoneNumber"].Value != null) ? employee.Properties["telephoneNumber"].Value.ToString() : "";
string Department = (employee.Properties["department"].Value != null) ? employee.Properties["department"].Value.ToString() : "";
// Create an XPathNavigator to walk the main data source
// of the form.
XPathNavigator xnMyForm = this.CreateNavigator();
XmlNamespaceManager ns = this.NamespaceManager;
// Set the fields in the form.
xnMyForm.SelectSingleNode("/my:myFields/my:RequestorFullName", ns)
.SetValue(FullName);
xnMyForm.SelectSingleNode("/my:myFields/my:RequestorEmail", ns)
.SetValue(Mail);
xnMyForm.SelectSingleNode("/my:myFields/my:RequestorOffice", ns)
.SetValue(Location);
xnMyForm.SelectSingleNode("/my:myFields/my:RequestorMailstop", ns)
.SetValue(MailStop);
xnMyForm.SelectSingleNode("my:myFields/my:RequestorExtension", ns)
.SetValue(Phone);
xnMyForm.SelectSingleNode("/my:myFields/my:RequestorDepartment", ns)
.SetValue(Department);
// Clean up.
xnMyForm = null;
searcher.Dispose();
result = null;
employee.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("The following error occurred: " +
ex.Message.ToString());
throw;
}
}
public void SubmitButton_Clicked(object sender, ClickedEventArgs e)
{
// Delete all Method nodes from the CAML Batch XML
XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();
XPathNodeIterator iter = batch.Select("/Batch/Method");
int methodNodesCount = iter.Count;
XPathNavigator firstMethodNav =
batch.SelectSingleNode("/Batch/Method[1]",
NamespaceManager);
XPathNavigator lastMethodNav =
batch.SelectSingleNode("/Batch/Method[" + methodNodesCount.ToString() + "]",
NamespaceManager);
firstMethodNav.DeleteRange(lastMethodNav);
// Retrieve the rows of the repeating table
XPathNavigator root = MainDataSource.CreateNavigator();
XPathNodeIterator rows = root.Select(
"/my:myFields/my:rows/my:row", NamespaceManager);
// Retrieve the values for the custom list item
string title = root.SelectSingleNode("my:myFields/my:title",
NamespaceManager).Value;
//Retrieve the RequestorFullName of the form
string RequestorFullName = root.SelectSingleNode("my:myFields/my:RequestorFullName",
NamespaceManager).Value;
//Retrieve the RequestorDepartment of the form
string RequestorDepartment = root.SelectSingleNode("my:myFields/my:RequestorDepartment",
NamespaceManager).Value;
//Retrieve the RequestorEmail of the form
string RequestorEmail = root.SelectSingleNode("my:myFields/my:RequestorEmail",
NamespaceManager).Value;
//Retrieve the AccountNumber of the form
string AccountNumber = root.SelectSingleNode("my:myFields/my:ChargeWorkTo",
NamespaceManager).Value;
//Retrieve the RequestorExtension of the form
string RequestorExtension = root.SelectSingleNode("my:myFields/my:RequestorExtension",
NamespaceManager).Value;
//Retrieve the OfficeLocation of the form
string OfficeLocation = root.SelectSingleNode("my:myFields/my:RequestorOffice",
NamespaceManager).Value;
//Retrieve the MailStop of the form
string MailStop = root.SelectSingleNode("my:myFields/my:RequestorMailstop",
NamespaceManager).Value;
// Loop through the rows of the repeating table
// and construct the CAML Batch XML
int counter = 1;
while (rows.MoveNext())
{
// Retrieve the SoftwareTitle
string SoftwareTitle = rows.Current.SelectSingleNode(
"my:SoftwareTitle", NamespaceManager).Value;
// Retrieve the SoftwarePrice
string SoftwarePrice = rows.Current.SelectSingleNode(
"my:SoftwarePrice", NamespaceManager).Value;
// Retrieve the Other SoftwareTitle
string OtherSoftwareTitle = rows.Current.SelectSingleNode(
"my:OtherSoftwareTitle", NamespaceManager).Value;
// Retrieve the Other SoftwarePrice
string OtherSoftwarePrice = rows.Current.SelectSingleNode(
"my:OtherSoftwarePrice", NamespaceManager).Value;
// Add an item to the CAML Batch XML
AddMethodNode(counter, title, SoftwareTitle, SoftwarePrice, RequestorFullName, RequestorDepartment, RequestorEmail, AccountNumber, RequestorExtension, OfficeLocation, MailStop, OtherSoftwarePrice, OtherSoftwareTitle);
// Increment the counter
counter++;
}
// Submit the Title of the request
batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']",
NamespaceManager).SetValue(title);
// Submit the rows to the Lists web service to update the custom list
DataConnections["CustomListItemsSubmit"].Execute();
}
private void AddMethodNode(int id, string title, string SoftwareTitle, string SoftwarePrice, string RequestorFullName, string RequestorDepartment, string RequestorEmail, string AccountNumber, string RequestorExtension, string OfficeLocation, string MailStop, string OtherSoftwarePrice, string OtherSoftwareTitle)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.AppendFormat("<Method ID=\"{0}\" Cmd=\"New\">", id.ToString());
sb.AppendFormat("<Field Name=\"Title\">{0}</Field>", title);
sb.AppendFormat("<Field Name=\"SoftwareTitle\">{0}</Field>", SoftwareTitle);
sb.AppendFormat("<Field Name=\"SoftwareTitle\">{0}</Field>", OtherSoftwareTitle);
sb.AppendFormat("<Field Name=\"SoftwarePrice\">{0}</Field>", SoftwarePrice);
sb.AppendFormat("<Field Name=\"SoftwarePrice\">{0}</Field>", OtherSoftwarePrice);
sb.AppendFormat("<Field Name=\"RequestorFullName\">{0}</Field>", RequestorFullName);
sb.AppendFormat("<Field Name=\"RequestorDepartment\">{0}</Field>", RequestorDepartment);
sb.AppendFormat("<Field Name=\"RequestorEmail\">{0}</Field>", RequestorEmail);
sb.AppendFormat("<Field Name=\"AccountNumber\">{0}</Field>", AccountNumber);
sb.AppendFormat("<Field Name=\"OfficeLocation\">{0}</Field>", OfficeLocation);
sb.AppendFormat("<Field Name=\"MailStop\">{0}</Field>", MailStop);
sb.AppendFormat("<Field Name=\"PhoneNumber\">{0}</Field>", RequestorExtension);
sb.AppendFormat("</Method>");
XmlDocument methodXML = new XmlDocument();
methodXML.LoadXml(sb.ToString());
XPathNavigator batch = DataSources["CustomListCAML"].CreateNavigator();
XPathNavigator batchNav = batch.SelectSingleNode("/Batch", NamespaceManager);
batchNav.AppendChild(methodXML.DocumentElement.CreateNavigator());
}
}
}