A Practical Guide to SharePoint 2013

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

Friday, September 11, 2009

Validating contact selector control

Question (asked by a forum user): I have a Contact Selector control in a repeating table in a browser form. How do I validate that there's something in the Contact Selector control? Suggested Solution: First, how can you validate contact selector control? Here is the solution: Private int Validate() { XPathNavigator ContactSelectorField = this.CreateNavigator().SelectSingleNode(XPATH of Contact Selector, this.NamespaceManager); if (ContactSelectorField.SelectChildren(XPathNodeType.Element).Count == 0) { returnValue = 0; } else if (ContactSelectorField.SelectChildren(XPathNodeType.Element).Count > 0) { ContactSelectorField = ContactSelectorField.SelectSingleNode("my:Person", this.NamespaceManager); if (string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:DisplayName").Value.Trim()) || string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:AccountId").Value.Trim()) || string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:AccountType").Value.Trim())) { returnValue = 0; } } } In a repeating table, you can iterate through all items and validate the control. I mean it depends on your actual requirements. This is just a hint. Please use your own judgement. XPathNavigator docNavigator = this.CreateNavigator(); XPathNodeIterator nodeIterator = docNavigator.Select("/my:myFields/my:group/my:repeatingGroup", this.NamespaceManager); bool Status = true; for (int i = 0; i < nodeIterator.Count; i++) { string strItemXPath = "/my:myFields/my:group/my:repeatingGroup[" + (i + 1) + "]" + "/my:" Status = Validate(strItemXPath); //See modified Validate function below } Private int Validate(string path) { XPathNavigator ContactSelectorField = this.CreateNavigator().SelectSingleNode(path + "contactSelector", this.NamespaceManager); //contactSelector is the name of the contact selector control. if (ContactSelectorField.SelectChildren(XPathNodeType.Element).Count == 0) { returnValue = 0; } else if (ContactSelectorField.SelectChildren(XPathNodeType.Element).Count > 0) { ContactSelectorField = ContactSelectorField.SelectSingleNode("my:Person", this.NamespaceManager); if (string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:DisplayName").Value.Trim()) || string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:AccountId").Value.Trim()) || string.IsNullOrEmpty(ContactSelectorField.SelectSingleNode("my:AccountType").Value.Trim())) { returnValue = 0; } } } This is off the top of my head. You may have to tweak it before trying it.