My career in technology

Of course, by far the easiest way to populate a list of states in an InfoPath form would be using an existing SharePoint list or a web service (see addendum at the end of this post).

But suppose you need to create a list of states and such a list or service doesn’t exist internally, or an external service cannot be used because of compliance issues, or there are potential access issues with a stand-alone form.  Not a very broad use-case, but it could happen.  (I know it can, because I ran into it…)

There are a couple of ways to generate the list.

First, you could use the InfoPath user interface for the drop-down list and input the state abbreviations. 

One.

At.

A.

Time.

(Oh joy!)

Or you could do what I did, and create a little Excel spreadsheet formula that merges the XML format for drop-down list box options and the list of states.

Or, even easier, you could just use the list I generated and paste it into the XML.

For your gratification and ease of use, here is the code:

											<option value="AL">
												<xsl:if test="my:LocationState=&quot;AL&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>AL</option>
											<option value="AK">
												<xsl:if test="my:LocationState=&quot;AK&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>AK</option>
											<option value="AS">
												<xsl:if test="my:LocationState=&quot;AS&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>AS</option>
											<option value="AZ">
												<xsl:if test="my:LocationState=&quot;AZ&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>AZ</option>
											<option value="AR">
												<xsl:if test="my:LocationState=&quot;AR&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>AR</option>
											<option value="CA">
												<xsl:if test="my:LocationState=&quot;CA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>CA</option>
											<option value="CO">
												<xsl:if test="my:LocationState=&quot;CO&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>CO</option>
											<option value="CT">
												<xsl:if test="my:LocationState=&quot;CT&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>CT</option>
											<option value="DE">
												<xsl:if test="my:LocationState=&quot;DE&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>DE</option>
											<option value="DC">
												<xsl:if test="my:LocationState=&quot;DC&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>DC</option>
											<option value="FM">
												<xsl:if test="my:LocationState=&quot;FM&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>FM</option>
											<option value="FL">
												<xsl:if test="my:LocationState=&quot;FL&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>FL</option>
											<option value="GA">
												<xsl:if test="my:LocationState=&quot;GA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>GA</option>
											<option value="GU">
												<xsl:if test="my:LocationState=&quot;GU&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>GU</option>
											<option value="HI">
												<xsl:if test="my:LocationState=&quot;HI&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>HI</option>
											<option value="ID">
												<xsl:if test="my:LocationState=&quot;ID&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>ID</option>
											<option value="IL">
												<xsl:if test="my:LocationState=&quot;IL&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>IL</option>
											<option value="IN">
												<xsl:if test="my:LocationState=&quot;IN&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>IN</option>
											<option value="IA">
												<xsl:if test="my:LocationState=&quot;IA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>IA</option>
											<option value="KS">
												<xsl:if test="my:LocationState=&quot;KS&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>KS</option>
											<option value="KY">
												<xsl:if test="my:LocationState=&quot;KY&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>KY</option>
											<option value="LA">
												<xsl:if test="my:LocationState=&quot;LA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>LA</option>
											<option value="ME">
												<xsl:if test="my:LocationState=&quot;ME&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>ME</option>
											<option value="MH">
												<xsl:if test="my:LocationState=&quot;MH&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MH</option>
											<option value="MD">
												<xsl:if test="my:LocationState=&quot;MD&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MD</option>
											<option value="MA">
												<xsl:if test="my:LocationState=&quot;MA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MA</option>
											<option value="MI">
												<xsl:if test="my:LocationState=&quot;MI&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MI</option>
											<option value="MN">
												<xsl:if test="my:LocationState=&quot;MN&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MN</option>
											<option value="MS">
												<xsl:if test="my:LocationState=&quot;MS&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MS</option>
											<option value="MO">
												<xsl:if test="my:LocationState=&quot;MO&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MO</option>
											<option value="MT">
												<xsl:if test="my:LocationState=&quot;MT&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MT</option>
											<option value="NE">
												<xsl:if test="my:LocationState=&quot;NE&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NE</option>
											<option value="NV">
												<xsl:if test="my:LocationState=&quot;NV&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NV</option>
											<option value="NH">
												<xsl:if test="my:LocationState=&quot;NH&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NH</option>
											<option value="NJ">
												<xsl:if test="my:LocationState=&quot;NJ&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NJ</option>
											<option value="NM">
												<xsl:if test="my:LocationState=&quot;NM&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NM</option>
											<option value="NY">
												<xsl:if test="my:LocationState=&quot;NY&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NY</option>
											<option value="NC">
												<xsl:if test="my:LocationState=&quot;NC&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>NC</option>
											<option value="ND">
												<xsl:if test="my:LocationState=&quot;ND&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>ND</option>
											<option value="MP">
												<xsl:if test="my:LocationState=&quot;MP&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>MP</option>
											<option value="OH">
												<xsl:if test="my:LocationState=&quot;OH&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>OH</option>
											<option value="OK">
												<xsl:if test="my:LocationState=&quot;OK&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>OK</option>
											<option value="OR">
												<xsl:if test="my:LocationState=&quot;OR&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>OR</option>
											<option value="PW">
												<xsl:if test="my:LocationState=&quot;PW&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>PW</option>
											<option value="PA">
												<xsl:if test="my:LocationState=&quot;PA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>PA</option>
											<option value="PR">
												<xsl:if test="my:LocationState=&quot;PR&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>PR</option>
											<option value="RI">
												<xsl:if test="my:LocationState=&quot;RI&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>RI</option>
											<option value="SC">
												<xsl:if test="my:LocationState=&quot;SC&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>SC</option>
											<option value="SD">
												<xsl:if test="my:LocationState=&quot;SD&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>SD</option>
											<option value="TN">
												<xsl:if test="my:LocationState=&quot;TN&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>TN</option>
											<option value="TX">
												<xsl:if test="my:LocationState=&quot;TX&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>TX</option>
											<option value="UT">
												<xsl:if test="my:LocationState=&quot;UT&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>UT</option>
											<option value="VT">
												<xsl:if test="my:LocationState=&quot;VT&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>VT</option>
											<option value="VI">
												<xsl:if test="my:LocationState=&quot;VI&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>VI</option>
											<option value="VA">
												<xsl:if test="my:LocationState=&quot;VA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>VA</option>
											<option value="WA">
												<xsl:if test="my:LocationState=&quot;WA&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>WA</option>
											<option value="WV">
												<xsl:if test="my:LocationState=&quot;WV&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>WV</option>
											<option value="WI">
												<xsl:if test="my:LocationState=&quot;WI&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>WI</option>
											<option value="WY">
												<xsl:if test="my:LocationState=&quot;WY&quot;">
													<xsl:attribute name="selected">selected</xsl:attribute>
												</xsl:if>WY</option>

So what do you do with it?

  1. In InfoPath, set up your form as you want it.  (It is easiest if you take the next step before you create additional views, but it is not that much more work to do it afterward.)
  2. For your State Drop-Down Listbox control, configure a single entry with a value of something distinctive, like “$$$$”
  3. Save the files as Source Files.
  4. Open the ViewName.xsl file(s) you wish to edit.
  5. Search for “$$$$” (or whatever your value is).
  6. You will find code that looks like this:
    											<xsl:attribute name="value">
    												<xsl:value-of select="my:LocationState"/>
    											</xsl:attribute>
    											<option>
    												<xsl:if test="my:LocationState=&quot;   Select...&quot;">
    													<xsl:attribute name="selected">selected</xsl:attribute>
    												</xsl:if>
    											</option>
    											<option value="$$$$">
    												<xsl:if test="my:LocationState=&quot;$$$$&quot;">
    													<xsl:attribute name="selected">selected</xsl:attribute>
    												</xsl:if>$$$$</option>
    
  7. Replace it with the code above.
  8. Do a search and replace and change “my:LocationState” to “my:YourFieldName
  9. Save your xml file(s). 
  10. You may also want to replace the default initial value of  ”   Select…” if you are making your control the width of the abbreviations.  JMHO.

I got the list of abbreviations from the US Postal Service.  It includes some lesser-used abbreviations, which I kept, but I did not include the military abbreviations.

Express permission is given for any user to utilize the code in this post in any project they need it for, with the only stipulation that the user post a comment below.  No warranty is expressed or implied, except that it should save you approximately 10 minues of drudgery.

And apparently when I edited a typo, WordPress reformatted my code. Sigh. I fixed it. The code display feature is great, but there are conflicts with the WordPress editing interface.

Addendum: For available web services, check here.

Advertisements

Comments on: "List of US States for use in an InfoPath Form" (6)

  1. Am I the only one who used this? 🙂 THANK YOU for saving me a ton of typing!

    • Given that you are the 198th person the view the post (assuming they were all unique visitors), I don’t imagine you are the first to use it, but you are the first to show your appreciation! Thanks!

  2. Joe Johnson said:

    Hi Jim,
    Your site is beautiful and helpful.
    I am not going to use this idea because I am not allowed to publish forms with code on them. I think this is called “Sandbox”?
    But I will be returning to your site for other tips. I am just starting to develop InfoPath forms and Sharepoint form libraries.
    Thanks,
    Joe

    • FYI – Technically this is a “no code” solution, so if you are allowed to publish forms, you should have no problem implementing this. This solution calls for modifying the XSL (text) in the form rather than using the interface to add the states

      one.

      at.

      a.

      time.

      As you aren’t adding compiled code (like C#) to the form, it is “no code” by Microsoft’s definition, so feel free to try this solution.

      Also, as I mentioned at the beginning of the post, the most efficient way to add a list of states would be using some external source, like a webservice, presuming that webservice was always accessible, trustworthy, and that connecting to it doesn’t violate any rules for your environment. The second-most efficient solution is to connect to a standard list on your site that contains a list of the states. My solution is for that rare use case in which form users may not always have connectivity to the server with the list, and an external source is not feasable or acceptable.

      And you don’t want to add all fifty states

      one.

      at.

      a.

      time.

  3. I had just started the “one at a time method” and am soooo glad I saw your post. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: