private775 icon

SP: export list fields

private775 | PRO | 05/17/17 02:31:33 PM UTC | 0 ⭐ | 587 👁️ | Never ⏰ | []
PowerShell |

1.68 KB

|

None

|

0 👍

/

0 👎

# export list fields
$webUrl  = "http://team/workgroups/DEMOSHAREDSERVICECENTRE/comms02"
$listName = "VIPS"
 
$attrs2delete = New-Object "System.Collections.Generic.List[string]"
$("SourceID", "ColName", "RowOrdinal", "Version", "Group")|%{$attrs2delete.Add($_)}
 
$allattrs = New-Object "System.Collections.Generic.List[string]"
 
 
$w = get-spweb $webUrl
$l = $w.Lists.TryGetList($listName)
$flds = $l.Fields |?{-not $_.FromBaseType}
 
$xmlFilePath = "C:\temp\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>"
 
foreach($fld in $flds){
    Add-Content $xmlFilePath $fld.SchemaXml
    Add-Content $xmlFilePath "`n"
}
 
Add-Content $xmlFilePath "</Fields>"
 
$xmlDoc = New-Object System.Xml.XmlDocument
$xmlDoc.Load($xmlFilePath)
 
$nodes = $xmlDoc.SelectNodes("//Fields/Field")
 
$cnt = 1
foreach($node in $nodes){
    "Processing node $($cnt)"
 
    $type = $node.SelectSingleNode("@Type").Value
    if($type -eq "Lookup") {
        $node.ParentNode.RemoveChild($node)
    } else {
        $attrs = $node.Attributes|?{$true}|%{$_}
        $attrs|%{
            $attrName = $_.Name
            if(!$allattrs.Contains($attrName)) { $allattrs.Add($attrName)}
            if($attrs2delete.Contains($attrName)){
                "Removing: $($attrName)"
                $node.Attributes.Remove($_)
            }
            $gr = $xmlDoc.CreateAttribute("Group")
            $gr.Value = "COMMS Request"
            $node.Attributes.Append($gr)
        }
    }
    $cnt++
}
 
$xmlDoc.Save($xmlFilePath)

Comments