Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm in yet another fix. This time its with getting my infopath form with custom code to work in my form library. Here is the code I have written to calculate the difference between 2 date time picker controls and it works fine in the preview mode. But when I publish the form to SharePoint as an admin-managed template, the end date date time picker control doesn't work hence the calculation doesnt work as well. I have viewed the ULS logs and event viewers and I can't find any errors. Anyone with ideas on what might possibly be wrong? Thanks.



C#
using Microsoft.Office.InfoPath;
using System;
using System.Xml;
using System.Xml.XPath;
using System.Collections.Generic;
using Microsoft.SharePoint;
namespace Test
{
    public partial class FormCode
    {
                       public object res
        {
            get
            {
                return FormState["res"];
            }
            set
            {
                FormState["res"] = value;
            }
        }
        public object result
        {
            get
            {
                return FormState["result"];
            }
            set
            {
                FormState["result"] = value;
            }
        }
                public void InternalStartup()
        {
            EventManager.XmlEvents["/my:myFields/my:group1/my:EndDT"].Changed += new XmlChangedEventHandler(EndDT_Changed);
        }
        public void EndDT_Changed(object sender, XmlEventArgs e)
        {
            
            
            // Write your code here to change the main data source.
            if (e.Operation == XmlOperation.ValueChange)
            {
                //Retrieve the values for the start and end time
                string startTimeValue = e.Site.SelectSingleNode("../my:StartDT",
                  NamespaceManager).Value;
                string endTimeValue = e.Site.SelectSingleNode("../my:EndDT",
                  NamespaceManager).Value;
                if (!String.IsNullOrEmpty(startTimeValue)
                  && !String.IsNullOrEmpty(endTimeValue))
                {
                    //Convert the time values to DateTime objects
                    //TimeSpan startTime = TimeSpan.Parse(startTimeValue);
                    //TimeSpan endTime = TimeSpan.Parse(endTimeValue);
                    DateTime startTime;
                    DateTime endTime;
                    //string date1 = "2015-03-23 12:34:56";
                    //string date2 = "2015-03-23 12:34:56";
                    try
                    {
                        startTime = DateTime.Parse(startTimeValue);
                        endTime = DateTime.Parse(endTimeValue);
                        //int startTime2 = startTime.Date.Hour;
                        //int endTime2 = endTime.Date.Hour;
                        //int a = startTime.Hour;
                        //int b = endTime.Hour;
                        TimeSpan diff = endTime.Subtract(startTime);
                        string dateInput = diff.ToString();
                        int day = 0, hour = 0, minutes = 0;
                        try
                        {
                            List<string> dateSplit = new List<string>(dateInput.Split('.', ':'));
                            if (dateSplit.Count == 4)
                            {
                                for (int i = 2; i < dateSplit.Count; i++) // Loop with for. 
                                {
                                    day = int.Parse(dateSplit[0].ToString());
                                    hour = int.Parse(dateSplit[1].ToString());
                                    minutes = int.Parse(dateSplit[2].ToString());
                                }
                                res = (int)day * 24;
                                result = (string)res + hour + ":" + minutes;
                            }
                            else if (dateSplit.Count == 3)
                            {
                                for (int i = 2; i < dateSplit.Count; i++) // Loop with for. 
                                {
                                    day = int.Parse(dateSplit[0].ToString());
                                    hour = int.Parse(dateSplit[1].ToString());
                                    minutes = int.Parse(dateSplit[2].ToString());
                                }
                                res = day;
                                result = res + ":" + hour;
                            }
                        }
                        catch (Exception ek)
                        {
                        }
                    }
                    catch (FormatException ex)
                    {
                    //Retrieve the time difference field node and remove its nil attribute
                    XPathNavigator nodeNav = e.Site.SelectSingleNode("../my:Duration",
                      NamespaceManager);
                    if (nodeNav.MoveToAttribute("nil", NamespaceManager.LookupNamespace("xsi")))
                        nodeNav.DeleteSelf();
                    //Retrieve and display the time difference
                    e.Site.SelectSingleNode("../my:Duration", NamespaceManager).SetValue(result.ToString());
                    //((TimeSpan)endTime.Subtract(startTime)).Hours.ToString());
                    //(diff.ToString());
                    //((TimeSpan)endTime.Subtract(startTime)).Hours.ToString());
                }
            }
        }
    }
}
Posted
Updated 18-May-15 4:12am
v3
Comments
Kuthuparakkal 18-May-15 8:20am    
You may try to cut-short your code as :
TimeSpan diff = endTime.Subtract(startTime);
result = ((int)diff.TotalHours).ToString() + ":" + (diff.Minutes).ToString();
BamideleAriwodola 18-May-15 8:41am    
Thank you for this. I finally found the reason why the code wasn't working. The account I was using to upload the template to SP didn't have full administrative rights to the SP server.

1 solution

I finally found the reason why the code wasn't working. The account I was using to upload the template to SP didn't have full administrative rights to the SP server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900