[string]$listUrl="Lists/myList",
[string]$contentTypeName,
[boolean]$includeHiddenFields=$true,
[string]$excludeTypes="Computed;Lookup;Guid"
)
clear
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Push-Location
function AddType($typeAsString)
{
$types = $excludeTypes.Split(";");
return $types.Contains($typeAsString) -eq $true;
}
function AddField($field)
{
<##>
if(AddType($field.TypeAsString) -eq $false)
{
return;
}
$fields = $script:exportNode.SelectSingleNode("/List/Fields");
[System.XML.XMLElement]$oXMLField=$global:exportNode.CreateElement("Field");
#InternalName
$urlAt = $global:exportNode.CreateAttribute("InternalName");
$urlAt.Value = $field.InternalName;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Title
$urlAt = $global:exportNode.CreateAttribute("Title");
$urlAt.Value = $field.Title;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#TypeAsString
$urlAt = $global:exportNode.CreateAttribute("TypeAsString");
$urlAt.Value = $field.TypeAsString;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Hidden
$urlAt = $global:exportNode.CreateAttribute("Hidden");
$urlAt.Value = $field.Hidden;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
#Required
$urlAt = $global:exportNode.CreateAttribute("Required");
$urlAt.Value = $field.Required;
$oXMLField.Attributes.Append($urlAt);
#Group
$urlAt = $global:exportNode.CreateAttribute("Group");
$urlAt.Value = $field.Group;
$oXMLField.Attributes.Append($urlAt);
$fields.AppendChild($oXMLField);
<# #>
}
$web = Get-SPWeb $webUrl -ErrorAction:SilentlyContinue;
if($web -eq $null)
{
read-Host "cannot access to $($webUrl) push any key to exit"
exit 0;
}
Write-Host "connected to $($webUrl) Title $($web.Title)"
$list = $web.GetList([Microsoft.Sharepoint.Utilities.SPUtility]::ConcatUrls($web.ServerRelativeUrl, $listUrl));
Write-Host "list $($list.Title) loaded"
#create xml document
$global:exportNode = New-Object System.Xml.XmlDocument
#add list node
[System.XML.XMLElement]$oXMLRoot=$global:exportNode.CreateElement("List")
#web Url attribute
$urlAt = $global:exportNode.CreateAttribute("url");
$urlAt.Value = $webUrl;
$oXMLRoot.Attributes.Append($urlAt);
#web title attribute
$urlAt = $global:exportNode.CreateAttribute("webTitle");
$urlAt.Value = $web.Title;
$oXMLRoot.Attributes.Append($urlAt);
#List Title attribute
$urlAt = $global:exportNode.CreateAttribute("ListTitle");
$urlAt.Value = $list.Title;
$oXMLRoot.Attributes.Append($urlAt);
#List Url attribute
$urlAt = $global:exportNode.CreateAttribute("ListUrl");
$urlAt.Value = $listUrl;
$oXMLRoot.Attributes.Append($urlAt);
#add list note to xmlDocument
$global:exportNode.appendChild($oXMLRoot);
$fields = $null;
if($includeHiddenFields)
{
$fields = $list.Fields |Sort-Object{$_.InternalName} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
else
{
$fields = $list.Fields |Sort-Object{$_.InternalName} | Where-Object {$_.Hidden -eq $false} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
#add fields
[System.XML.XMLElement]$oXMLFields=$global:exportNode.CreateElement("Fields")
$global:exportNode.FirstChild.AppendChild($oXMLFields);
foreach($field in $fields){
AddField($field);
}
#add ContentTypes
[System.XML.XMLElement]$oXMLContentTypes=$global:exportNode.CreateElement("ContentTypes")
$global:exportNode.FirstChild.AppendChild($oXMLContentTypes);
$oXMLContentTypes = $script:exportNode.SelectSingleNode("/List/ContentTypes");
foreach($contentType in $list.ContentTypes)
{
[System.XML.XMLElement]$oXMLContentType=$global:exportNode.CreateElement("ContentType");
$urlAt = $global:exportNode.CreateAttribute("Name");
$urlAt.Value = $contentType.Name;
$oXMLContentType.Attributes.Append($urlAt);
$oXMLContentTypes.AppendChild($oXMLContentType);
[System.XML.XMLElement]$oXMLCTFields=$global:exportNode.CreateElement("Fields");
$oXMLContentType.AppendChild($oXMLCTFields);
<##>
if($includeHiddenFields)
{
$fields = $contentType.Fields |Sort-Object{$_.InternalName} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
else
{
$fields = $contentType.Fields |Sort-Object{$_.InternalName} | Where-Object {$_.Hidden -eq $false} | select Title, InternalName, TypeAsString, Hidden, Required, Group
}
#fields in content type
foreach($field in $fields)
{
[System.XML.XMLElement]$oXMLCTField=$global:exportNode.CreateElement("Field");
#InternalName
$urlAt = $global:exportNode.CreateAttribute("InternalName");
$urlAt.Value = $field.InternalName;
$oXMLCTField.Attributes.Append($urlAt);
#Title
$urlAt = $global:exportNode.CreateAttribute("Title");
$urlAt.Value = $field.Title;
$oXMLCTField.Attributes.Append($urlAt);
#TypeAsString
$urlAt = $global:exportNode.CreateAttribute("TypeAsString");
$urlAt.Value = $field.TypeAsString;
$oXMLCTField.Attributes.Append($urlAt);
#Hidden
$urlAt = $global:exportNode.CreateAttribute("Hidden");
$urlAt.Value = $field.Hidden;
$oXMLCTField.Attributes.Append($urlAt);
#Required
$urlAt = $global:exportNode.CreateAttribute("Required");
$urlAt.Value = $field.Required;
$oXMLCTField.Attributes.Append($urlAt);
#Group
$urlAt = $global:exportNode.CreateAttribute("Group");
$urlAt.Value = $field.Group;
$oXMLCTField.Attributes.Append($urlAt);
$oXMLCTFields.AppendChild($oXMLCTField);
}
}
<# #>
#export to test file
$global:exportNode.OuterXml | Out-File "$($list.Title.Replace(" ", "_")).xml" -Encoding utf8
#dispose the web
$web.Dispose()
download file