public void FormEvents_Submit(object sender, SubmitEventArgs e)
{
try
{
XPathNavigator domNav = MainDataSource.CreateNavigator();
XPathNodeIterator activity = domNav.Select("/my:TimesheetDetails/my:EmployeeInformation/my:TimeReporting/my:PerActivity", NamespaceManager);
#region Node path strings
//If you're node structure changes you need only change this code for the most repeatable strings
string dateString = "";
string dayString = "";
string onString = "";
string timesheetDetails = "/my:TimesheetDetails";
string employeeInfo = timesheetDetails + "/my:EmployeeInformation";
string timeReporting = employeeInfo + "/my:TimeReporting";
string perActivity = timeReporting + "/my:PerActivity";
#endregion
#region Loop Variables
//Information to be retrieved
string date;
string hours;
string department;
string employeename;
string weekOf;
string location;
string taskActivity;
string activityDescription;
string jiModNumber;
string customerName;
string newCustomer = "";
string paNumber;
string billable;
string billablehours;
string billingRatehours;
string rates;
string overnight;
string overnightclaim;
int overnightamount;
string overtime;
XPathNodeIterator locNodes;
XPathNodeIterator taskActivityNodes;
XPathNodeIterator activityDescNodes;
XPathNodeIterator jiModNumberNodes;
XPathNodeIterator customerNameNodes;
XPathNodeIterator paNumberNodes;
XPathNodeIterator billableNodes;
XPathNodeIterator overtimeNodes;
XPathNodeIterator billablehoursNodes;
XPathNodeIterator billingRateNodes;
XPathNodeIterator hourNodes;
XPathNodeIterator dateNodes;
XPathNodeIterator ratesNodes;
overnightclaim = domNav.SelectSingleNode(employeeInfo + "/my:ONClaim", NamespaceManager).Value;
overnightamount = int.Parse(overnightclaim);
#endregion
while (activity.MoveNext())
{
for (int i = 0; i < 7; i++)
{
#region Select Day Strings
//Change the day strings once
switch (i)
{
case 0:
//Select Monday
dateString = "my:MDate";
dayString = "my:Mon";
onString = "my:monON";
break;
case 1:
//Select Tuesday
dateString = "my:TDate";
dayString = "my:Tue";
onString = "my:tuesON";
break;
case 2:
//Select Wednesday
dateString = "my:WDate";
dayString = "my:Wed";
onString = "my:wedON";
break;
case 3:
//Select Thursday
dateString = "my:ThDate";
dayString = "my:Thu";
onString = "my:thuON";
break;
case 4:
//Select Friday
dateString = "my:FDate";
dayString = "my:Fri";
onString = "my:friON";
break;
case 5:
//Select Saturday
dateString = "my:SaDate";
dayString = "my:Sat";
onString = "my:satON";
break;
//Select Sunday
case 6:
dateString = "my:SuDate";
dayString = "my:Sun";
onString = "my:sunON";
break;
}
#endregion
#region Select Information
#region Static Information
department = domNav.SelectSingleNode(timesheetDetails + "/my:Department", NamespaceManager).Value;
employeename = domNav.SelectSingleNode(employeeInfo + "/my:EmployeeName", NamespaceManager).Value;
weekOf = domNav.SelectSingleNode(employeeInfo + "/my:WeekOf", NamespaceManager).Value;
#endregion
locNodes = activity.Current.SelectDescendants(perActivity + "/@my:Location",this.NamespaceManager.DefaultNamespace,true);
location = locNodes.Current.SelectSingleNode("my:Location", NamespaceManager).Value;
locNodes.Current.MoveToNext();
taskActivityNodes = activity.Current.SelectDescendants(perActivity + "/@my:TaskActivity", this.NamespaceManager.DefaultNamespace, true);
taskActivity = taskActivityNodes.Current.SelectSingleNode("my:TaskActivity", NamespaceManager).Value;
taskActivityNodes.Current.MoveToNext();
activityDescNodes = activity.Current.SelectDescendants(perActivity + "/@my:ActivityDescription", this.NamespaceManager.DefaultNamespace, true);
activityDescription = activity.Current.SelectSingleNode("my:ActivityDescription", NamespaceManager).Value;
activityDescNodes.Current.MoveToNext();
jiModNumberNodes = activity.Current.SelectDescendants(perActivity + "/@my:JIModNumber", this.NamespaceManager.DefaultNamespace, true);
jiModNumber = activity.Current.SelectSingleNode("my:JIModNumber", NamespaceManager).Value;
jiModNumberNodes.Current.MoveToNext();
customerNameNodes = activity.Current.SelectDescendants(perActivity + "/@my:CustomerName", this.NamespaceManager.DefaultNamespace, true);
customerName = activity.Current.SelectSingleNode("my:CustomerName", NamespaceManager).Value;
customerNameNodes.Current.MoveToNext();
paNumberNodes = activity.Current.SelectDescendants(perActivity + "/@my:PANumber", this.NamespaceManager.DefaultNamespace, true);
paNumber = activity.Current.SelectSingleNode("my:PANumber", NamespaceManager).Value;
paNumberNodes.Current.MoveToNext();
billableNodes = activity.Current.SelectDescendants(perActivity + "/@my:Billable", this.NamespaceManager.DefaultNamespace, true);
billable = activity.Current.SelectSingleNode("my:Billable", NamespaceManager).Value;
billableNodes.Current.MoveToNext();
overtimeNodes = activity.Current.SelectDescendants(perActivity + "/@my:Overtime", this.NamespaceManager.DefaultNamespace, true);
overtime = activity.Current.SelectSingleNode("my:Overtime", NamespaceManager).Value;
overtimeNodes.Current.MoveToNext();
billablehoursNodes = activity.Current.SelectDescendants(perActivity + "/@my:BillableHours", this.NamespaceManager.DefaultNamespace, true);
billablehours = activity.Current.SelectSingleNode("my:BillableHours", NamespaceManager).Value;
billablehoursNodes.Current.MoveToNext();
billingRateNodes = activity.Current.SelectDescendants(perActivity + "/@my:BillingRate", this.NamespaceManager.DefaultNamespace, true);
billingRatehours = activity.Current.SelectSingleNode("my:BillingRate", NamespaceManager).Value;
billingRateNodes.Current.MoveToNext();
hourNodes = activity.Current.SelectDescendants(perActivity + "/@my:HoursPerDay/" + dayString, this.NamespaceManager.DefaultNamespace,false);
hours = activity.Current.SelectSingleNode("my:HoursPerDay/" + dayString, NamespaceManager).Value;
hourNodes.Current.MoveToNext();
dateNodes = activity.Current.SelectDescendants(employeeInfo + "/@" + timeReporting + "/" + dateString, this.NamespaceManager.DefaultNamespace,false);
date = activity.Current.SelectSingleNode(timeReporting + "/" + dateString, NamespaceManager).Value;
dateNodes.Current.MoveToNext();
ratesNodes = activity.Current.SelectDescendants(perActivity + "/@my:rates", this.NamespaceManager.DefaultNamespace, true);
rates = activity.Current.SelectSingleNode("my:rates", NamespaceManager).Value;
ratesNodes.Current.MoveToNext();
if ((overnightamount > 0) && (overnightclaim != ""))
{
overnight = domNav.SelectSingleNode("/my:TimesheetDetails/my:Overnights/" + onString, NamespaceManager).Value;
overnightamount -= 1;
}
else
{
overnight = "";
}
#endregion
#region AddCAML
XPathNavigator batch = DataSources["AddCAML"].CreateNavigator();
batch.SelectSingleNode("/Batch/Method/Field[@Name='Title']", NamespaceManager).SetValue(date);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Department']", NamespaceManager).SetValue(department);
batch.SelectSingleNode("/Batch/Method/Field[@Name='EmployeeName']", NamespaceManager).SetValue(employeename);
batch.SelectSingleNode("/Batch/Method/Field[@Name='WeekOf']", NamespaceManager).SetValue(weekOf);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Location']", NamespaceManager).SetValue(location);
batch.SelectSingleNode("/Batch/Method/Field[@Name='TaskActivity']", NamespaceManager).SetValue(taskActivity);
batch.SelectSingleNode("/Batch/Method/Field[@Name='ActivityDescription']", NamespaceManager).SetValue(activityDescription);
batch.SelectSingleNode("/Batch/Method/Field[@Name='JIModNumber']", NamespaceManager).SetValue(jiModNumber);
batch.SelectSingleNode("/Batch/Method/Field[@Name='CustomerName']", NamespaceManager).SetValue(customerName);
batch.SelectSingleNode("/Batch/Method/Field[@Name='NewCustomer']", NamespaceManager).SetValue(newCustomer);
batch.SelectSingleNode("/Batch/Method/Field[@Name='PANumber']", NamespaceManager).SetValue(paNumber);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Billable']", NamespaceManager).SetValue(billable);
batch.SelectSingleNode("/Batch/Method/Field[@Name='Hours']", NamespaceManager).SetValue(hours);
batch.SelectSingleNode("/Batch/Method/Field[@Name='TotalBillable']", NamespaceManager).SetValue(billablehours);
batch.SelectSingleNode("/Batch/Method/Field[@Name='OvertimeRates']", NamespaceManager).SetValue(rates);
if ((overnight != null) &&( overnight != "") )
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='OvernightStays']", NamespaceManager).SetValue(overnight);
}
else
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='OvernightStays']", NamespaceManager).SetValue("0");
}
if ((billingRatehours != null) && (billingRatehours != "") )
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='BillingRate']", NamespaceManager).SetValue(billingRatehours);
}
else
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='BillingRate']", NamespaceManager).SetValue("0");
}
if ((overtime != null) && (overtime != ""))
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='Overtime']", NamespaceManager).SetValue(overtime);
}
else
{
batch.SelectSingleNode("/Batch/Method/Field[@Name='Overtime']", NamespaceManager).SetValue("false");
}
if ((hours != "0.00") && (hours != "0") && (hours != ""))
{
DataConnections["Web Service Submit"].Execute();
domNav.SelectSingleNode("/my:TimesheetDetails/my:Lock", NamespaceManager).SetValue("1");
domNav.SelectSingleNode("/my:TimesheetDetails/my:SubmittedToOpenAccounts", NamespaceManager).SetValue("1");
DataConnections["Main submit"].Execute();
}
else if ((overnight != "0") && (overnight != "") && (overnight != "false"))
{
DataConnections["Web Service Submit"].Execute();
domNav.SelectSingleNode("/my:TimesheetDetails/my:Lock", NamespaceManager).SetValue("1");
domNav.SelectSingleNode("/my:TimesheetDetails/my:SubmittedToOpenAccounts", NamespaceManager).SetValue("1");
DataConnections["Main submit"].Execute();
}
#endregion
}
}
}
catch (Exception ex)
{
string error = ex.Message;
}
try
{
XPathNavigator domNav = MainDataSource.CreateNavigator();
string employeeName = domNav.SelectSingleNode("/my:TimesheetDetails/my:EmployeeInformation/my:EmployeeName", NamespaceManager).Value;
System.Net.Mail.MailMessage MailMsg = new System.Net.Mail.MailMessage();
MailMsg.Subject = "Timesheet approved";
string managerEmail = domNav.SelectSingleNode("/my:TimesheetDetails/my:ManagerEmail", NamespaceManager).Value;
string employeeEmail = domNav.SelectSingleNode("/my:TimesheetDetails/my:EmployeeInformation/my:UserEmail", NamespaceManager).Value;
MailAddress sentfrom = new MailAddress(managerEmail);
MailMsg.To.Add(employeeEmail);
MailMsg.From = sentfrom;
MailMsg.IsBodyHtml = true;
string URL = "http://server/formlibrary";
string HREF = "<a href=\"";
string FormName = domNav.SelectSingleNode("/my:TimesheetDetails/my:FormName", NamespaceManager).Value + ".xml";
string ApprovalURL = HREF + URL + FormName + "?&Source=http://server/formlibrary&OpenIn=Browser\">Click here</a>";
MailMsg.Body = "The following timesheet has been approved by your manager: " + ApprovalURL;
SmtpClient sendmail = new SmtpClient();
sendmail.Host = "mailserver";
sendmail.Send(MailMsg);
}
catch (Exception x)
{
logMessage(x.Message, EventLogEntryType.Error);
}
e.CancelableArgs.Cancel = false;
}
Was my code in the end, with an associated CAML file that looks like this:-
<?xml version="1.0" encoding="UTF-8"?>
<Batch OnError="Continue">
<Method ID="1" Cmd="New">
<Field Name='Title'></Field>
<Field Name='Department'></Field>
<Field Name="EmployeeName"></Field>
<Field Name="WeekOf"></Field>
<Field Name="Location"></Field>
<Field Name="TaskActivity"></Field>
<Field Name="ActivityDescription"></Field>
<Field Name="JIModNumber"></Field>
<Field Name="CustomerName"></Field>
<Field Name="NewCustomer"></Field>
<Field Name="PANumber"></Field>
<Field Name="Hours"></Field>
<Field Name="Billable"></Field>
<Field Name="TotalBillable"></Field>
<Field Name="OvertimeRates"></Field>
<Field Name="OvernightStays"></Field>
<Field Name="BillingRate"></Field>
<Field Name="Overtime"></Field>
</Method>
</Batch>
You can rename the Title column to something else within sharepoint but the CAML needs to be called "Title".
All my sharepoint columns are just Single Line text fields or Booleans, but it may work with other types, I have no requirement for that so haven't tested it.
Hope that helps, sorry my code isn't 100% commented as it was a case of hack together ASAP to get the system up and running. My only qualm is that if one line of XML fails when submitting the "submitted" box comes up so the manager thinks its gone through to the list whereas only half of it may have, but that's only been an issue when I've made schema changes and existing forms have had a single piece of missing xml data.