Skip to content

September 10, 2011

File to SharePoint

Rounding out the set of solutions I’ve been playing with to get data from the File Classification Infrastructure into SharePoint – in a more flexible and supportable way – is a tool for importing files into SharePoint. This solution will read files from the file system and try to pickup optional metadata from a ~~metadata.xml file in the current directory (it follows the same format as the FCI2XML tool outputs.) The tool preserves the creator, creation date/time, last editor, and last edit time when uploading the file. It doesn’t worry about security or versions – but it’s a simple way to import files into SharePoint in a hierarchy.

Changing an Attribute for an XML file with PowerShell

In my little FCI program I output an attribute called author from Word documents – and I wanted to import the FCI data into SharePoint but Author is the internal field name for the creator of a file. SharePoint expects this to be a login name – but out of Word I got a friendly name. So I needed to convert this property to a different name. The first step was getting some XSLT to do this so I came up with:

<?xml version=’1.0′ ?>
<xsl:stylesheet version=”1.0″ xmlns:xsl=””>
<xsl:output method=”xml” version=”1.0″ encoding=”UTF-8″ indent=”yes”/>
<xsl:template match=”/” >
<xsl:apply-templates />
<xsl:template match=”/Files/File/Properties/Property/@Name[.=’Author’]” >
<xsl:attribute name=”Name”>DocumentAuthor</xsl:attribute>
<xsl:template match=”@*|node()”>
<xsl:apply-templates select=”@*|node()”/>

This worked pretty well but I wanted to run it from PowerShell so how could I do that? Well first I’m lazy so I wanted the Convert-Xml commandlet from the PowerShell Community Extensions. With that I wasn’t quite done. I still needed to process all of my metadata files – and I wanted to refer to my XSLT file relative to the script. So I came up with the following which processes all ~~metadata.xml files, creates ~~spmetadata.xml files, deletes the ~~metadata.xml and then renames all of the ~~spmetadata.xml to ~~metadata.xml.

Import-Module pscx
function Get-ScriptDirectory
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
$scriptLocation = Get-ScriptDirectory
$xsltLocation = Join-Path $scriptLocation “authortransform.xsl”
$files = dir -Recurse -Include “~~metadata.xml”
foreach ($file in $files)
$outputPath = Join-Path $file.Directory.FullName “~~spmetadata.xml”
Convert-Xml -path $file.FullName -XsltPath $xsltLocation > $outputPath
dir -include “~~metadata.xml” -recurse | remove-item
dir -include “~~spmetadata.xml” -recurse | %{ $newName = join-path $_.Directory.FullName “~~metadata.xml”; move-item $_.FullName $newName }

It took me way to long to put these pieces together but once I did they seem to work pretty well.

File Classification Infrastructure to XML

In Windows 2008 R2 a new feature was added – File Classification Infrastructure (FCI). What FCI does is it allows you to set and keep metadata for files on the file system. Despite the promise of the technology there aren’t a ton of tools which can read or use it so it’s hard to get too excited about the technology. I did, however, think that the ability to apply properties via rules could come in handy from time-to-time. So I wrote a little program that will extract FCI properties and write them to an XML file in the current directory. You can then use this metadata in other programs.

Recent Posts

Public Speaking