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.

Saturday, July 12, 2008

Populating InfoPath drop down with SharePoint list data programmatically

Learn how to use SPQuery object to fetch data from a SharePoint list.  In one of the previous articles, you saw how one can populate a drop down list box without writing code but now you will see how to do it programmatically. Programming means more power in your hands!

Wednesday, July 9, 2008

InfoPath: Raising an error on validation

InfoPath provides excellent method to validate controls, for example, you can use data validation alone or in conjuction with Rules to validate data in InfoPath forms. You can also do it  programmatically and raise errors which will be shown in the form when there is a validation error. Assume there is a control called FirstName. Here is how you will raise an error on FirstName:
public void FirstName_Changed(object sender, XmlEventArgs e)
{

}
Right click the control and select Programming > Changed Event. Note, Validating Event is also available but we will use Changed Event. Raise any previously raised errors:
this.Errors.Delete("FirstNameError");
After validating, if there is an error, raise it:
 this.Errors.Add(Nav, "FirstNameError", "Please add correct value in First Name");
The first parameter is the XPathNavigator object that points to the control.
XPathNavigator nav = this.CreateNavigator().SelectSingleNode(XPath of the firstname field, this.NamespaceManager);
XPath will be like "/my:myFields/my:FirstName".
The second parameter is the name of the error and third parameter is the description of the error.
Happy programming!

Wednesday, June 18, 2008

Populating InfoPath Drop down with SharePoint List Data


Hard coding values in InfoPath drop down list box is easy but you can also populate it with dynamic data. Data can be from an XML file, a database or even a SharePoint list. This article shows how you can populate the list box with data from a SharePoint list with out writing a line of code.