In my last post I wrote about a command that I created to export site columns to an xml file because I needed the data for a Feature I was working on. But what if you just want to create a column using the exported results and for whatever reason you don’t want to create a Feature? I figured it would take me about 2 minutes to throw together an import command that would take in the results of the export so may as well add it to the collection. So now you can use the gl-importsitecolumns command to import what you exported using gl-exportsitecolumns (useful if you’re just trying to move some fields around though I’d still recommend you do this via a Feature). Note that this command will blow up if a column already exists with the same name. The code for this is really simple – just loop through the XML and call the SPFieldCollection’s AddFieldAsXml method:

 1string url = Params["url"].Value;
 2 
 3XmlDocument xmlDoc = new XmlDocument();
 4xmlDoc.Load(Params["inputfile"].Value);
 5 
 6 
 7// Get the source content type and fields.
 8using (SPSite site = new SPSite(url))
 9using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
10{
11    foreach (XmlElement fieldNode in xmlDoc.SelectNodes("//Field"))
12    {
13        web.Fields.AddFieldAsXml(fieldNode.OuterXml);
14    }
15}

The syntax of the command can be seen below:

C:\>stsadm -help gl-importsitecolumns

stsadm -o gl-importsitecolumns

Imports one or more site fields (columns) to a site.

Parameters:
        -url <url>
        -inputfile <file to import fields from>

Here’s an example of how to import site columns that were exported using the exportsitecolumns command:

  stsadm -o gl-importsitecolumns -url "http://intranet" -inputfile c:\fields.xml