I've moved my blog to!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Sunday, February 17, 2008

Export Site Columns

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);
  10:  StringBuilder sb = new StringBuilder();
  11:  XmlTextWriter xmlWriter = new XmlTextWriter(new StringWriter(sb));
  12:  xmlWriter.Formatting = Formatting.Indented;
  14:  try
  15:  {
  16:   xmlWriter.WriteStartElement("Elements");
  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:   }
  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.

        -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"


Anonymous said...

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).



Gary Lapointe said...

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"))

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.

ignazioc said...

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 "" -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.

Gary Lapointe said...

Looks right - are you getting any errors?

Anonymous said...

What does the optional featuresafe command do?

Gary Lapointe said...

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.