Export Site Columns

Posted on Posted in SharePoint 2007, STSADM Commands

As I mentioned in my previous post about the gl-exportcontenttypes command I need to be able to quickly and easily get the CAML necessary to recreate site columns in a Feature. To do this I created a quick and dirty command called gl-exportsitecolumns. The code for this is extremely simple – I just get the SPField objects of interest based on the parameters passed in and dump out the SchemaXml property – that’s it:

   1:  string url = Params["url"].Value;
   2:  string fieldTitle = Params["fielddisplayname"].Value;
   3:  string fieldName = Params["fieldinternalname"].Value;
   4:  string groupName = Params["group"].Value;
   5:  bool useTitle = Params["fielddisplayname"].UserTypedIn;
   6:  bool useName = Params["fieldinternalname"].UserTypedIn;
   7:  bool useGroup = Params["group"].UserTypedIn;
   8:  bool all = !(useTitle || useName || useGroup);
   9:   
  10:  StringBuilder sb = new StringBuilder();
  11:  XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(sb));
  12:  xmlWriter.Formatting = Formatting.Indented;
  13:   
  14:  try
  15:  {
  16:   xmlWriter.WriteStartElement("Elements");
  17:   
  18:   // Get the source content type and fields.
  19:   using (SPSite site = new SPSite(url))
  20:   {
  21:    using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
  22:    {
  23:     SPFieldCollection fields = web.Fields;
  24:     foreach (SPField field in fields)
  25:     {
  26:      if (all ||
  27:       (useGroup && groupName.ToLowerInvariant() == field.Group.ToLowerInvariant()) ||
  28:       (useName && fieldName.ToLowerInvariant() == field.InternalName.ToLowerInvariant()) ||
  29:       (useTitle && fieldTitle.ToLowerInvariant() == field.Title.ToLowerInvariant()))
  30:      {
  31:       xmlWriter.WriteString("\r\n");
  32:       xmlWriter.WriteRaw(field.SchemaXml);
  33:      }
  34:     }
  35:    }
  36:   }
  37:   
  38:   xmlWriter.WriteString("\r\n");
  39:   xmlWriter.WriteEndElement(); // Elements
  40:  }
  41:  finally
  42:  {
  43:   xmlWriter.Flush();
  44:   xmlWriter.Close();
  45:  }
  46:  File.WriteAllText(Params["outputfile"].Value, sb.ToString());

The syntax of the command can be seen below:

C:\>stsadm -help gl-exportsitecolumns

stsadm -o gl-exportsitecolumns


Exports one or more site fields (columns) to a file.

Parameters:
        -url <url>
        {[-fielddisplayname <field display name> / -fieldinternalname <field internal name>]
         [-group <site column group name to filter results by>]}
        -outputfile <file to output field schema to>

Here’s an example of how to dump the XML for all the site columns in a particular group:

stsadm -o gl-exportsitecolumns -url "http://intranet" -outputfile c:\fields.xml -group "Custom Site Columns"

9 thoughts on “Export Site Columns

  1. Hi Gary,

    I have successfully exported a site column using your method 🙂 thanks.

    I just need to know how to import this into another site. It’s actually that I want to ‘back port’ a site column from a Production system into the Development/Test system that it originally was published from, but the site column has subsequently been deleted.

    Any tips would be most appreciated.

    I would prefer to just “import” it in rather than to create a feature to add it (if the former is even possible).

    Thanks,

    Tod

  2. Tod – thanks for the feedback – it’s actually really simple to do the import – you just use the SPFieldCollection’s AddFieldAsXml method:

    using (SPSite site = new SPSite(url))
    using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
    {
    foreach (XmlElement fieldNode in xmlDoc.SelectNodes(“//Field”))
    {
    web.Fields.AddFieldAsXml(fieldNode.OuterXml);
    }
    }

    To save you some effort I went ahead and just created a new command to do this – if you download the latest you’ll see a new command called importsitecolumns – just run that passing the file you exported using the exportsitecolumns command.

  3. hi Gary,
    thank you for the stsadm extension you wrote.
    i am trying to run this command to export some site columns. can you please tell me if i am doing something wrong? thanks.

    D:\MOSS Projects>stsadm -o gl-exportsitecolumns “http://spdev.company.com:23456/site1” -outputfile C:\GLRSiteColumn.xml -group “GL Reconciliation”

    site1 is a subsite of a site collection and GL Reconciliation is the group name i used for the coulumns.

    thank you.

  4. It removes the following attributes which are not valid in a Feature: Version, Aggregation, and Customization. It also fixes the UserSelectionMode attribute which exports as a string but must be a numeric value.

  5. What if I want to move a site column from a site to the parent site, and there is already content using the column – and I always want the content to use the “new” site column on the parent site instead?
    Can this be accomplished easily or do I have to manually edit this on all the items in question?

  6. Hi Gary,

    Fantastic set of tools here, thank you! I am wondering about the gl-exportsitecolumns (and gl-importsitecolumns) stsadm commands. They do not appear to be available in the 2010 download, but I do see them for 2007. Is there a reason they aren’t included for 2010 (perhaps there is a way to copy content types and site columns between web applications now in 2010)?

    Thanks!
    Chanda

    1. Simply a matter of time – I haven’t had any while I was working on the book – now that I’m done working on the book I’ll be slowly revisiting these commands and getting them migrated over (I’ve got a backlog of blog postings I need to get working on first though).

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*