InfoPath 2007 File name Validation

I am relatively new to InfoPath but realise it has a lot of uses so when a support call for an Issue with an InfoPath form was escalated to the development team I offered to have a look.

It had already been established that the issue was down to the fact that the file name generated in the form occasionally contained Illegal characters, see list below, thus couldn’t be saved into the SharePoint document library. My first reaction was this is easy in C# but how do I manage this in InfoPath and the answer, well the one I choose, was using C# πŸ™‚

Looking at the existing form I could see there was a few Rules and Actions set up on the submit button one of which was to generate the file name using the formula below

Existing formula for generating the file name
concat(“PREFIX-“, FIELD_NAME, “-“, now())

My first thought was I might be able to change this formula somehow but decided against this as I wasn’t sure if it was even possible and if it was then it would make the formula, potentially, too complex. Instead I opted to create a hidden field and use the Changed Event on the existing field to get the value entered by the user, strip out any characters I didn’t want and then set the value of my hidden field. I could then change my formula on the submit button to use my new formatted field instead of the original and bingo it works.

I have included the code I used to strip out the Illegal characters below.

New formula for generating the file name
concat(“PREFIX-“, NEW_FIELD_NAME, “-“, now())

Illegal characters
[ ~ # % & * { } \ : < > ? /

public void FIELD_NAME_Changed(object sender, XmlEventArgs e)
{
      //characters not allowed
      string[] invlaidChars = new string[] { β€œ-”, β€œβ€˜β€, β€œ[β€œ, β€œ~”, β€œ#”, β€œ%”, β€œ&”, β€œ*”, β€œ{β€œ, β€œ}”, β€œ\\”, β€œ:β€œ, β€œ<β€œ, β€œ>β€œ, β€œ?β€œ, β€œ/”}; 

      //check the value has changed 
      if (e.OldValue.Equals(e.NewValue))
      {
           return;
      } 

      string newFieldValue = e.NewValue; 

      //loop through invlaid characters and remove them
      foreach (string invalidChar in invlaidChars)
      {
          if (formattedValue.Contains(invalidChar))
          {
               newFieldValue = newFieldValue.Replace(invalidChar, β€œβ€œ);
           }
       } 

      XPathNavigator nav = MainDataSource.CreateNavigator();

      //find formatted field 
       XPathNavigator node = nav.SelectSingleNode(β€œ/my:myFields/my:FIELD_NAME”, NamespaceManager);

      //check we have found it
      if (node != null)
      {
           //set the value
           node.SetValue(newFieldValue);
       }
}

 

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

%d bloggers like this: