How do I – Identify the SharePoint site template type and ID of a given sub web?

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

This one always comes up at some point in time, especially after migrations. I’ve seen issues with migrated sites many times when custom site templates were used for sites, since SharePoint doesn’t know how to upgrade these sites 100%. A good example is the Fab40 templates that people loved to install in SharePoint 2007. When migrating to 2010, the templates are not supported and if the templates were not installed on the target server, issues will arise with the sites. The most common of these are that the home pages will simply throw a 404 error. You can navigate through the site settings pages just fine, but the home pages just doesn’t work. For this reason, I always start my trouble shooting by asking what type of site I’m dealing with. Since it’s most unlikely that your site admin would know in most cases, it becomes necessary to be able to identify that. Here’s a quick Powershell script for doing so.

$web = Get-SPWeb "http://sharepoint.crayveon.com/Research/Encryption Algorithms"
write-host "Template Name: " $web.WebTemplate
write-host "Template ID: " $web.WebTemplateId
$web.Dispose()
NOTE:  The URL supplied to Get-SPWeb should be in quotes and should NOT contain unescaped characters i.e. http://sharepoint.crayveon.com/Research/Encryption%20Algorithms will NOT yield a valid SPWeb object, but “http://sharepoint.crayveon.com/Research/Encryption Algorithms” will.

Cheers
C




image

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