A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Thursday, August 28, 2008

Comparing two different date formats

If one date control is using a format different than the other date control, how will you compare the dates? Why will this happen? Why will the two dates be using different formats in the same form? well, you can't argue with the client. can you? This was a real scenario that I faced some time ago. So, convert the dates to one format and compare them. For example,
   string shortDate1 = Microsoft.VisualBasic.Strings.FormatDateTime(Convert.ToDateTime(Date1), Microsoft.VisualBasic.DateFormat.ShortDate);
   string shortDate2 = Microsoft.VisualBasic.Strings.FormatDateTime(Convert.ToDateTime(Date2), Microsoft.VisualBasic.DateFormat.ShortDate);
   if (DateTime.Parse(shortDate1) < DateTime.Parse(shortDate2))
   {
         //Display message
   }
In the code above, I am comparing two dates, Date1 and Date2. These dates are stored in string format in the form. So we convert them to date format first and then to short date format and then compare them.

Monday, August 18, 2008

InfoPath: Deleting all rows except the first one in repeating table

Code:
XPathNavigator Node = this.CreateNavigator().SelectSingleNode("/my:myFields/my:RepeatingGroup", this.NamespaceManager);
XPathNodeIterator node_2b_deleted = this.CreateNavigator().Select("/my:myFields/my:RepeatingGroup", NamespaceManager);
                                    if (node_2b_deleted.Count > 1)
                                    {
                                        string group = "/my:myFields/my:RepeatingGroup";
                                        XPathNavigator firstItem = Node.SelectSingleNode(groupResults + "[2]", NamespaceManager);
                                        XPathNavigator lastItem = Node.SelectSingleNode(groupResults + "[position()=last()]", NamespaceManager);
                                        firstItem.DeleteRange(lastItem);
                                        lastItem = null;
                                        firstItem = null;
                                    }
                                    node_2b_deleted = null;
Explanation:

We start with second row and delete all rows till the last row. Why will we leave the first row? It depends. There are scenarios where one required to re-populate the repeating table after deleting all rows. In that case, if there is no first row, it will not be possible to create a clone of the first row. We first create a clone of the first row and then populate the fields of the repeating row. This continues until all rows are populated with data and then we delete the master row to avoid creating a duplicate entry of the first row. So, first row is always required. You can blank out all fields in the first row and if the repeating table is in a section, hide the section which will give users the feeling that whole repeating table has been deleted.

Saturday, August 16, 2008

Populating InfoPath drop down with SharePoint list data programmatically - Part 2 (Using SPQuery)

Learn how to use SPQuery object to fetch data from a SharePoint list.  SPuery is fast and efficient. Populate InfoPath drop down with SharePoint list data.