Lately i'm working to a feasibility study for a intranet migration from SharePoint 2010 to SharePoint 2013.
During this project, i needed to know what are content types and fields were involved in the 2010 custom solution, in order to map them in SharePoint 2013 ones.
My boss asked me to do this A.S.A.P., so i developed a powershell script capable of retrieving these data and put them in CSV files, so i can import them with excel and do further analysis.
The script output is a set of three CSV files:
- Site content types
- Site columns
- Mapping between content types and fields
Here is the code for PowersShell 3.0:
##
## 20130214, from Varro with love
##
Add-PSSnapin Microsoft.SharePoint.PowerShell
## Variables
$WEB_APPLICATION_URL = "http://teamsite"
$CTS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_ContentTypes.csv"
$FIELDS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_Fields.csv"
$CT_FIELDS_FILE_PATH = "c:\Users\Administrator\Desktop\2013_TeamSite_CTs_And_Fields.csv"
## Initialization
echo "Initializing variables"
'"CT Name","CT Description","CT ID","CT Group","CT Parent ID"' | Out-File $CTS_FILE_PATH;
'"Field Title","Field Description","Field Internal Name","Field ID","Field Group","Field Type"' | Out-File $FIELDS_FILE_PATH;
'"CT Name","Field Title","CT ID","Field ID"' | Out-File $CT_FIELDS_FILE_PATH;
$AllContentTypes = @();
$AllFields = @();
## Go with the code
echo "Entering web application $WEB_APPLICATION_URL"
$webApplication = Get-SPWebApplication -identity $WEB_APPLICATION_URL
ForEach ($siteCollection in $webApplication.Sites)
{
ForEach($subSite in $siteCollection.AllWebs)
{
$subsiteUrl = $subsite.Url;
echo "Extracting info from $subSiteUrl";
$contentTypes = $subSite.ContentTypes;
ForEach ($contentType in $contentTypes)
{
If (!$AllContentTypes.Contains($contentType.Id))
{
$AllContentTypes += $contentType.Id;
# Let's write on Content types CSV
'"' + $contentType.Name + '","' + $contentType.Description + '","' + $contentType.Id + '","' + $contentType.Group + '","' + $contentType.Parent.Id + '"' | Out-File $CTS_FILE_PATH -Append
ForEach ($field in $contentType.Fields)
{
If (!$AllFields.Contains($field.Id))
{
$AllFields += $field.Id;
# Let's write on Fields CSV
'"' + $field.Title + '","' + $field.Description + '","' + $field.Id + '","' + $field.Group + '","' + $field.Type + '","' | Out-File $FIELDS_FILE_PATH -Append;
}
'"' + $contentType.Name + '","' + $field.Title + '","' + $contentType.Id + '","' + $field.Id + '","' | Out-File $CT_FIELDS_FILE_PATH -Append;
}
}
}
}
}
echo "See you space cowboy";
If you need to run this script in PowerShell 2.0, you just need to change these rows
If (!$AllContentTypes.Contains($contentType.Id))
...
If (!$AllFields.Contains($field.Id))
with these ones:
$alreadyInCT = $AllContentTypes -contains $contentType.Id;
If (!$alreadyInCT)
...
$alreadyInFields = $AllFields -contains $field.Id;
If (!$alreadyInFields)
Please feel free to reuse, comment and share.