All About SharePoint is a blog about SharePoint, InfoPath and related technologies. It was started in 2005 and has good readership in many countries. It covers SharePoint 2003, 2007, 2010, 2013, 2016 and Office 365. You can read articles, tips, tutorials and news. It has code samples and complete applications as well.
Saturday, August 23, 2008
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);
{
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;
}
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.
Friday, June 6, 2008
InfoPath: Reading from a repeating table
I will start with the code first and then explain what's happening.
Code:
XPathNavigator DOM = this.MainDataSource.CreateNavigator();
XPathNodeIterator nodes = DOM.Select("/my:myFields/my:RepeatingGroup", this.NamespaceManager);
XPathNavigator nodesNavigator = nodes.Current;
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Element, false);
string FirstName = "";
string LastName = "";
while (nodesText.MoveNext())
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
{
if (nodesText.Current.Name == "my:FirstName")
{
FirstName = nodesText.Current.Value.ToString();
}
nodesText.MoveNext();
if (nodesText.Current.Name == "my:LastName")
{
LastName= nodesText.Current.Value.ToString();
}
{
LastName= nodesText.Current.Value.ToString();
}
}
nodesText = null;
nodesNavigator = null;
nodes = null;
DOM = null;
nodesNavigator = null;
nodes = null;
DOM = null;
Explanation:
It's a very commong scenario. Developers want to read from the repeating table programmatically. The above code just does that. You take the XPath for the repeating group and create a nodes iterator object. This will iterate through all the descendant nodes. If the node type (XPathNodeType) is "Element", the value will be read into a string variable. That's it. Use MoveNext() to move to the next node in the hierarchy. It's as simple as that!
Subscribe to:
Posts (Atom)