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



6 comments:
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
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.
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.
Looks right - are you getting any errors?
What does the optional featuresafe command do?
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.
Post a Comment