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.
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.
Wednesday, June 18, 2008
Populating InfoPath Drop down with SharePoint List Data
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!
Wednesday, May 14, 2008
XPath, XQuery, and XSLT Functions
Some XML functions can be very useful in InfoPath, especially if you are an InfoPath developer. For example, suppose you are using data validation in your InfoPath form and you want the users to enter a value in one of the text boxes on your form. Of course, you can use the built-in data validation functions, for example, "If field is blank then show error alert" but users can dodge you by entering a space in the text box. In this case, InfoPath won't generate any error and will accept the value entered by the user. How can handle this situation? The answer is XML functions. You can use normalize-space() function. Right click the control and select "Data Validation". In the drop down, select "The expression" and in the text box enter normalize-space(.)="". In the screen tip, enter "Please enter valid value" and click Ok. InfoPath will now show an error if users try to enter an invalid value like a whitespace. Similarly, you can use other XML functions in your code or to validate data using the built-in data validation. More functions can be found on the following page. The page has the list of functions and examples to show how each function works:
-SSA
Tuesday, May 6, 2008
How to grant permissions to a custom assembly that is referenced in a report in Reporting Services
It seems straight forward. Isn't it? but it may not be as simple and straight forward for the newbies as it seems at first. A colleague and a friend of mine recently was struggling with the same issue and he learnt the lesson after spending considerable amount of time experimenting with different solutions. You get a security exception when you write your own component to be used in a report in reporting services. My friend is working on a SharePoint project that involves reporting services. He created a report that used a custom built component. The code used SharePoint object model. He tried to use Elevated privileges but even that didn't work because the error is thrown on the line that has the elevated priviliges code. He even tried the code access security, that didn't work. The following KB article has information about this problem:
http://support.microsoft.com/kb/842419/ (How to grant permissions to a custom assembly that is referenced in a report in Reporting Services)
Use following code to get rid of the security exception:
SharePointPermission perm = new SharePointPermission(PermissionState.Unrestricted);
perm.Assert();
perm.Assert();
//Code using SharePoint object model here!!
perm.Deny();
Include following DLL in the references before you use the above code:
Microsoft.SharePoint.Security.DLL
This DLL will be found in the 12 hive. Exact location is as following:
System Drive:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions12ISAPI
Add following namespaces at the top:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;
using System.Security.Permissions;
That's it. Your component will now work fine.
Monday, May 5, 2008
Creating SharePoint List Views Programmatically
Learn how to create SharePoint list views programmatically.
Validating contact selector control programmatically
How to know if contact selector is empty?
XpathNavigator selector = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector", this.NamespaceManager);
if (selector.SelectChildren(XPathNodeType.Element).Count == 0)
{
this.Errors.Add(selector, "Error","Selector is empty");
{
this.Errors.Add(selector, "Error","Selector is empty");
}
How to raise error if user has selected more than one person in the selector?
if (selector.SelectChildren(XPathNodeType.Element).Count > 1)
{
this.Errors.Add(selector, "Error","Please select one user");
{
this.Errors.Add(selector, "Error","Please select one user");
}
How to tell if user hasn't selected a valid person?
selector = selector.SelectSingleNode("my:Person", this.NamespaceManager);
XPathNavigator displayname = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:DisplayName", this.NamespaceManager);
string strDisplayName = displayname.value.toString();
string strDisplayName = displayname.value.toString();
XPathNavigator accountid = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:AccountId", this.NamespaceManager);
string strAccountid = accountid.value.toString();
string strAccountid = accountid.value.toString();
XPathNavigator accounttype = this.CreateNavigator().SelectSingleNode("/my:myFields/my:contactselector/my:Person/my:AccountType", this.NamespaceManager);
string strAccountType = accounttype.value.toString();
string strAccountType = accounttype.value.toString();
if (string.IsNullOrEmpty(strDisplayName)) ||
string.IsNullOrEmpty(strAccountId)) ||
string.IsNullOrEmpty(strAccountType))
{
this.Errors.Add(selector,"Error","Please select valid user");
}
Note: This was off the top of my head, you may find minor mistakes in XPaths or spellings.
Sunday, April 20, 2008
MVP Summit experience
Returned from the summit on friday night but my body was so sour that I spent whole saturday sleeping, relaxing, and watching TV. This was my first summit experience and undoubtedly it was the best experience I have had in recent times. This was the first time I met SharePoint MVPs in person. I have uploaded some pictures that I took during the summit, here are the URLs:
http://picasaweb.google.com/saifullah.shafiq/Paintball2008
http://picasaweb.google.com/saifullah.shafiq/PartyWithPalermo
http://picasaweb.google.com/saifullah.shafiq/MvpAttendeeParty
http://picasaweb.google.com/saifullah.shafiq/ProductGroupDinner
http://picasaweb.google.com/saifullah.shafiq/PaintballLunch
http://picasaweb.google.com/saifullah.shafiq/PartyWithPalermo
http://picasaweb.google.com/saifullah.shafiq/MvpAttendeeParty
http://picasaweb.google.com/saifullah.shafiq/ProductGroupDinner
http://picasaweb.google.com/saifullah.shafiq/PaintballLunch
The sessions were very informative and I got chance to spend some quality time with the other MVPs during the events like Paintball, Attendee party, PG dinner, etc. I also got chance to meet with MVPs from around the world. All in all, it was an excellent experience and I really enjoyed the visit and I hope to visit the summit again next year which is expected to start on March 1st, 2009. Those of you interested in finding out how we SharePoint MVPs had fun at the summit may want to read the following blog post:
-Saif
Subscribe to:
Posts (Atom)