Archive for August, 2012

How do I – Programmatically select a field inside InfoPath XML using C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

As we know, InfoPath forms are actually XML documents behind the scenes… a special kind of XML, but XML nonetheless. As such we should be able to figure out how to change the value of a field pretty easily, right? You’d think so, but unless you live inside XML every day (so few of us do these days) it might now be as easy as you’d have thought. After getting the InfoPath document loaded to an XmlDocument object, one would expect a statement thus:

doc.SelectSingleNode("//my:Family", nsm).InnerText = "New Value";
to have the desired effect. Alas, it’s not that simple. It never is, is it? Smile Instead, this is what you’re greeted with: image_thumb_38C22EAC What we need to do is ensure that your XmlNamespaceManager is properly assigned with the content from the given InfoPath form in order to translate the “my:” part of our xPath statement. If we take a look at the InfoPath document’s OuterXml value we see this: Untitled-1 With so many xmlns attributes in the InfoPath document, we need some automation to prepare our XmlNamespaceManager so we can interact with the data properly. Adding this little code snippet to our existing code, should resolve that nasty error:
foreach (XmlAttribute att in doc.DocumentElement.Attributes)
{
  if (att.Prefix == "xmlns")
  {
    nsm.AddNamespace(att.LocalName, att.Value);
  }
}
Remember to create the XmlNamespaceManager object with a reference to the base XmlDocument’s NameTable thus:
nsm = new XmlNamespaceManager(doc.NameTable);
Throw all that together and we are now able to manipulate the InfoPath field programmatically to our heart’s content.

Cheers
C




image

SharePoint Fundamentals – How do I programmatically get a reference to a SharePoint site following Best Practices using C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

This is one of the most fundamental building blocks for all SharePoint developers.  We get references to SPWeb and SPSite objects all the time without ever giving a second thought to how it’s done.  I’ve decided to document the recommended way to do this, combining guidance I frequently provide to SharePoint admins with regard to dealing with developer code via the Developer Dashboard.  This little snippet is thus intended to start guiding developers towards making it a habit to wrap sections of code in the SPMonitoredScope class.  I have provided the complete namespace of objects for completeness sake. 

A reference to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.SharePoint.dll is required.

Here’s the code:

using (new Microsoft.SharePoint.Utilities.SPMonitoredScope("ScopeName"))
{
  using (Microsoft.SharePoint.SPSite sps = new Microsoft.SharePoint.SPSite("SiteURL"))
  {
    using (Microsoft.SharePoint.SPWeb spw = sps.OpenWeb())
    {
      //Do something
    }
  }
}


Cheers
C




image