InfoPath 2007 File name Validation

June 24, 2011

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);
       }
}

 


Access web.config from SharePoint Timer Job

June 8, 2011

While working on a public facing website using MOSS 2007 I needed to created an XML sitemap to be used by Google and other search engines. I looked around at various different options but eventually found a Blog article by Tim Dobrinski. This covered most of my requirements, however there were a few things I wanted to parameterise such as a list of sites to be excluded from the XML and also the document library the XML file was to be uploaded to.

Talking to a colleague, Gordon Duthie, he mentioned he had done something similar and blogged about it. After reading his blog, Reading from web.config’s AppSettings in a SharePoint timer job, I thought this would be nice and easy. The complication arose from the fact the client had a web application they were using to edit the content for the site and then this was extended and given the public facing site URL.

When I started to use the code it seemed to be working correctly but then I added a setting in the web application web.config and the extended web.config with different values. I found it was only ever hitting the original web.config. Thinking about it this made perfect sense as while there are two sites in IIS there is only one web application. Even though I am accessing the site via the extended application URL the web application name, which is what my colleague was using to get the path to the web.config, was always going to be the actual web application name and not the name of the extended web application.

There may be other workarounds but I decided I would use a custom property to pass information to the timer job, adding properties to a custom SharePoint Timer Job. The only down side of this is if I need to change the details being passed to the timer job I have to deactivate and then reactivate the feature.


%d bloggers like this: