Use Find/Replace in PowerShell to fix InfoPath Forms after moving to a new Site Collection

I know I know, that’s a really long title.

I found some pretty good PowerShell how-to’s which described how to use Get-ChildItem to iterate through a folder – checking each item for a string and then using -replace to fix the string.

However, my situation was slightly different as I wanted to walk through a SharePoint 2010 Form Library – fixing InfoPath forms which had been moved into a new site collection and thus did not open in the browser and would not save due to the old reference to the XSN.

I’m sure there may be better methods on moving XSNs and XMLs across site collections where maybe this wouldn’t be necessary, but if you’re reading this I’ll assume you don’t care.

In any case, what I ended up doing (after obviously re-publishing the XSN template to the new site collection so new IP forms wouold work) is:

  1. Copy all of the forms using Windows Explorer (we didn’t care about versions or modified by metadata, just the content type metadata) into the new Form Lib.
  2. Run the following PowerShell code to fix each XML file – replacing the link to the old XSN with a link to the new XSN (Note: I ran this on my desktop, NOT on the server as I didn’t need any SP cmdlets):
$files = Get-ChildItem \\sitecollection\davwwwroot\FormLib -Filter "*.xml"
foreach ($file in $files) {
(Get-Content $file.fullname) | ForEach-Object {$_ -replace "http://sitecollection/library/OLDXSNPATH.xsn", "http://newsitecollection/library/NEWXSNPATH.xsn"} | Set-Content $file.fullname

Voila, all IP forms are now fixed and will not only open in the browser as expected – but I can now update these forms in the new location as they now point to the correct XSN template.

If you’re a frequent reader you’re probably thinking “Wow, normally he gives us these nice functions with parameters, help, etc.”… You’re right, this time it was a quick and dirty chunk of code. However, chances are I’ll need to do this again – so I did actually write a function for future needs:

function Reset-InfoPathTemplateLink {
$files = Get-ChildItem $FilePath -Filter $FileExtension
	foreach ($file in $files) {
		(Get-Content $file.fullname) |
		ForEach-Object {$_ -replace $OldPath,$NewPath} |
		Set-Content $file.fullname
	} #end foreach
} #end function

To run the function, first dot-source it and then use the following syntax:

Reset-InfoPathTemplateLink -FilePath "\\sitecollection\subsite\formlib" -FileExtension "*.xml" -OldPath "http://sitecollection/doclib/template.xsn" -NewPath "http://newsitecollection/newdoclib/newtemplate.xsn"

As always, happy PowerShelling!

Add an Auto-numbering field to an InfoPath Form

Thanks to some Twitter help from Clayton Cobb and Saed Shela and Clayton’s blog post on Auto-Numbering InfoPath Forms, I got a solution that works perfectly and requires nothing but the InfoPath form itself. I previously had a solution using the Do Calculation method of a SharePoint Designer workflow, but I would much rather do it all in InfoPath.
What I needed was to automatically number the InfoPath Form in a SharePoint 2010 Form Library. I could’ve just used the ID field, but my client is replacing a process that was previously manually numbered from 1 all the way up to the 4500+ range (and increasing daily). I’m preparing this process so that we can do a cutover and start numbering where the ‘old’ process left off.
So basically for my testing and development I wanted to add 5000 to the current item ID and put that into another field, we’ll call it Form Number.
In order to pull this off, I did the following:
  1. Create 2 hidden fields in InfoPath, I called them NumID and NumberToAdd. NumID will contain the ID of the item, NumberToAdd is the amount to add to the NumID – my default value is set at 5000.
  2. Set up the Submit button in the form to perform the following steps when clicked:
    1) Submit to the Form library
    2) Query a data connection that pulls the ID from my Form Library
    3) Set the NumID field to the ID of the item
    4) Set the Form Number field to NumID + NumberToAdd
    5) Submit to the Form library
    6) Close the form

The query data connection is simply a receive connection to my form lib, and it pulls the ID field only. Since I have the NumberToAdd field set at 5000, it adds 5000 to the current ID and I end up with the Form Number field showing a value of 5001, 5002, etc.

Pretty simple, but sometimes the simple things give me the most problems! Thanks again to Clayton and Saed for the assistance via Twitter!

Add Collapsible Help to InfoPath Forms

One of my favorite things to do in relation to SharePoint is business process automation using workflow. What I REALLY enjoy is developing InfoPath forms to go with the workflows.

Recently I was working on an InfoPath form that had a need for a significant amount of text-based help within the form. If it is a sentence or so, it’s not a big deal to put the text inline or even as a screentip, but this was more text than a sentence.

After some thinking and playing with rules and picture buttons, I came up with the solution below:

Help collapsed, notice the [?] icon:

Help expanded, with a different [-] icon:

InfoPath Designer 2010 and SharePoint 2010 are a great team!

In case you’re not as fortunate as me and you haven’t had the privilege of experimenting with SharePoint 2010 and the new Form Templates available in InfoPath Designer 2010, here are some cool things you can do without code!

  1. Create a custom branded User Profile Lookup Form to talk to the UserProfileService.asmx web service. Pull in data such as Preferred Name, E-mail address, Profile Picture, Department, Manager, etc. Put this in an InfoPath Form Web Part and give the user a nice UI for querying your Active Directory.
  2. Create a custom Site Provisioning Form to talk to the Sites.asmx web service. Wrap an approval workflow around it so that when the manager approves the site request it gets auto-provisioned. The user doesn’t need permission to create sites because you can run it with elevated privileges. The manager doesn’t need to manually create the site for the user after approving it; everyone wins!

These are just two of the cool things you can do with SharePoint 2010 and InfoPath 2010.