none
Exporting Site Columns RRS feed

  • Question

  • I would like to use Powershell to export columns from a site collection so that I can create/import them into a new clean site collection. Can I use the SchemaXml?

    Basically, I want to go through each field in specific groups in the source site collection, and write a row containing the Title, Group and SchemaXml in a csv file. The first two columns are basically for my ability to read the file. On the import side, I want to retrieve the row, and then basically go row by row and feed the SchemaXml into:

    $web.Fields.AddFieldAsXml(variable with the SchemaXml)

    ...in the new destination site collection. If I do this, should I remove the ID="<GUID>" and SourceID="<GUID>" bits from the xml prior to using the AddFieldAsXml method? or is that unnecessary?

    Tuesday, March 7, 2017 8:15 PM

All replies

  • Hi,

    Here is the solution for your reference, you could check below link for details.

    http://get-spscripts.com/2011/01/export-and-importcreate-site-columns-in.html

     

    $sourceWeb = Get-SPWeb http://portal
    $xmlFilePath = "C:\Install\Script-SiteColumns.xml"
    
    #Create Export Files
    New-Item $xmlFilePath -type file -force
    
    #Export Site Columns to XML file
    Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
    Add-Content $xmlFilePath "`n<Fields>"
    $sourceWeb.Fields | ForEach-Object {
        if ($_.Group -eq "Custom Columns") {
            Add-Content $xmlFilePath $_.SchemaXml
        }
    }
    Add-Content $xmlFilePath "</Fields>"
    
    $sourceWeb.Dispose()
    

    $destWeb = Get-SPWeb http://portal/sites/migrationtest
    $installPath = "C:\Install"
    
    #Get exported XML file
    $fieldsXML = [xml](Get-Content($installPath + "\Script-SiteColumns.xml"))
    
    $fieldsXML.Fields.Field | ForEach-Object {
        
        #Configure core properties belonging to all column types
        $fieldXML = '<Field Type="' + $_.Type + '"
        Name="' + $_.Name + '"
        ID="' + $_.ID + '"
        Description="' + $_.Description + '"
        DisplayName="' + $_.DisplayName + '"
        StaticName="' + $_.StaticName + '"
        Group="' + $_.Group + '"
        Hidden="' + $_.Hidden + '"
        Required="' + $_.Required + '"
        Sealed="' + $_.Sealed + '"'
        
        #Configure optional properties belonging to specific column types – you may need to add some extra properties here if present in your XML file
        if ($_.ShowInDisplayForm) { $fieldXML = $fieldXML + "`n" + 'ShowInDisplayForm="' + $_.ShowInDisplayForm + '"'}
        if ($_.ShowInEditForm) { $fieldXML = $fieldXML + "`n" + 'ShowInEditForm="' + $_.ShowInEditForm + '"'}
        if ($_.ShowInListSettings) { $fieldXML = $fieldXML + "`n" + 'ShowInListSettings="' + $_.ShowInListSettings + '"'}
        if ($_.ShowInNewForm) { $fieldXML = $fieldXML + "`n" + 'ShowInNewForm="' + $_.ShowInNewForm + '"'}
            
        if ($_.EnforceUniqueValues) { $fieldXML = $fieldXML + "`n" + 'EnforceUniqueValues="' + $_.EnforceUniqueValues + '"'}
        if ($_.Indexed) { $fieldXML = $fieldXML + "`n" + 'Indexed="' + $_.Indexed + '"'}
        if ($_.Format) { $fieldXML = $fieldXML + "`n" + 'Format="' + $_.Format + '"'}
        if ($_.MaxLength) { $fieldXML = $fieldXML + "`n" + 'MaxLength="' + $_.MaxLength + '"' }
        if ($_.FillInChoice) { $fieldXML = $fieldXML + "`n" + 'FillInChoice="' + $_.FillInChoice + '"' }
        if ($_.NumLines) { $fieldXML = $fieldXML + "`n" + 'NumLines="' + $_.NumLines + '"' }
        if ($_.RichText) { $fieldXML = $fieldXML + "`n" + 'RichText="' + $_.RichText + '"' }
        if ($_.RichTextMode) { $fieldXML = $fieldXML + "`n" + 'RichTextMode="' + $_.RichTextMode + '"' }
        if ($_.IsolateStyles) { $fieldXML = $fieldXML + "`n" + 'IsolateStyles="' + $_.IsolateStyles + '"' }
        if ($_.AppendOnly) { $fieldXML = $fieldXML + "`n" + 'AppendOnly="' + $_.AppendOnly + '"' }
        if ($_.Sortable) { $fieldXML = $fieldXML + "`n" + 'Sortable="' + $_.Sortable + '"' }
        if ($_.RestrictedMode) { $fieldXML = $fieldXML + "`n" + 'RestrictedMode="' + $_.RestrictedMode + '"' }
        if ($_.UnlimitedLengthInDocumentLibrary) { $fieldXML = $fieldXML + "`n" + 'UnlimitedLengthInDocumentLibrary="' + $_.UnlimitedLengthInDocumentLibrary + '"' }
        if ($_.CanToggleHidden) { $fieldXML = $fieldXML + "`n" + 'CanToggleHidden="' + $_.CanToggleHidden + '"' }
        if ($_.List) { $fieldXML = $fieldXML + "`n" + 'List="' + $_.List + '"' }
        if ($_.ShowField) { $fieldXML = $fieldXML + "`n" + 'ShowField="' + $_.ShowField + '"' }
        if ($_.UserSelectionMode) { $fieldXML = $fieldXML + "`n" + 'UserSelectionMode="' + $_.UserSelectionMode + '"' }
        if ($_.UserSelectionScope) { $fieldXML = $fieldXML + "`n" + 'UserSelectionScope="' + $_.UserSelectionScope + '"' }
        if ($_.BaseType) { $fieldXML = $fieldXML + "`n" + 'BaseType="' + $_.BaseType + '"' }
        if ($_.Mult) { $fieldXML = $fieldXML + "`n" + 'Mult="' + $_.Mult + '"' }
        if ($_.ReadOnly) { $fieldXML = $fieldXML + "`n" + 'ReadOnly="' + $_.ReadOnly + '"' }
        if ($_.FieldRef) { $fieldXML = $fieldXML + "`n" + 'FieldRef="' + $_.FieldRef + '"' }    
    
        $fieldXML = $fieldXML + ">"
        
        #Create choices if choice column
        if ($_.Type -eq "Choice") {
            $fieldXML = $fieldXML + "`n<CHOICES>"
            $_.Choices.Choice | ForEach-Object {
               $fieldXML = $fieldXML + "`n<CHOICE>" + $_ + "</CHOICE>"
            }
            $fieldXML = $fieldXML + "`n</CHOICES>"
        }
        
        #Set Default value, if specified  
        if ($_.Default) { $fieldXML = $fieldXML + "`n<Default>" + $_.Default + "</Default>" }
        
        #End XML tag specified for this field
        $fieldXML = $fieldXML + "</Field>"
        
        #Create column on the site
        $destWeb.Fields.AddFieldAsXml($fieldXML.Replace("&","&amp;"))
        write-host "Created site column" $_.DisplayName "on" $destWeb.Url
        
        $destWeb.Dispose()
    }
    

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Wednesday, March 8, 2017 4:52 AM
  • Thanks. This appears to be a tweak to something I had found. A nice one at that.

    However, I am wondering if I can simply use the SchemaXml directly with the AddFieldAsXml method. No real need to do anything else if that works.

    Wednesday, March 8, 2017 7:35 PM