Advertisement
private775

SP: export list fields

May 17th, 2017
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # export list fields
  2. $webUrl  = "http://team/workgroups/DEMOSHAREDSERVICECENTRE/comms02"
  3. $listName = "VIPS"
  4.  
  5. $attrs2delete = New-Object "System.Collections.Generic.List[string]"
  6. $("SourceID", "ColName", "RowOrdinal", "Version", "Group")|%{$attrs2delete.Add($_)}
  7.  
  8. $allattrs = New-Object "System.Collections.Generic.List[string]"
  9.  
  10.  
  11. $w = get-spweb $webUrl
  12. $l = $w.Lists.TryGetList($listName)
  13. $flds = $l.Fields |?{-not $_.FromBaseType}
  14.  
  15. $xmlFilePath = "C:\temp\Script-SiteColumns.xml"
  16.  
  17. #Create Export Files
  18. New-Item $xmlFilePath -type file -force
  19.  
  20. #Export Site Columns to XML file
  21. Add-Content $xmlFilePath "<?xml version=`"1.0`" encoding=`"utf-8`"?>"
  22. Add-Content $xmlFilePath "`n<Fields>"
  23.  
  24. foreach($fld in $flds){
  25.     Add-Content $xmlFilePath $fld.SchemaXml
  26.     Add-Content $xmlFilePath "`n"
  27. }
  28.  
  29. Add-Content $xmlFilePath "</Fields>"
  30.  
  31. $xmlDoc = New-Object System.Xml.XmlDocument
  32. $xmlDoc.Load($xmlFilePath)
  33.  
  34. $nodes = $xmlDoc.SelectNodes("//Fields/Field")
  35.  
  36. $cnt = 1
  37. foreach($node in $nodes){
  38.     "Processing node $($cnt)"
  39.  
  40.     $type = $node.SelectSingleNode("@Type").Value
  41.     if($type -eq "Lookup") {
  42.         $node.ParentNode.RemoveChild($node)
  43.     } else {
  44.         $attrs = $node.Attributes|?{$true}|%{$_}
  45.         $attrs|%{
  46.             $attrName = $_.Name
  47.             if(!$allattrs.Contains($attrName)) { $allattrs.Add($attrName)}
  48.             if($attrs2delete.Contains($attrName)){
  49.                 "Removing: $($attrName)"
  50.                 $node.Attributes.Remove($_)
  51.             }
  52.             $gr = $xmlDoc.CreateAttribute("Group")
  53.             $gr.Value = "COMMS Request"
  54.             $node.Attributes.Append($gr)
  55.         }
  56.     }
  57.     $cnt++
  58. }
  59.  
  60. $xmlDoc.Save($xmlFilePath)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement