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
## ## 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.
Hello,
ReplyDeleteI'm running into problems getting this to run in Powershell 2.0 after replacing the lines you mentioned. My "Field Type" is blank in the csv. How does this look:
##
## 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)
{
$alreadyInCT = $AllContentTypes -contains $contentType.Id;
If (!$alreadyInCT)
{
$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)
{
$alreadyInFields = $AllFields -contains $field.Id;
If (!$alreadyInFields)
{
$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";
Hi Anonymous one,
Deletei have done some tests in order to help you. The code you will find below will produce only field csv file.
The post code works but there is an alignment issue. I will fix this ASAP, in the meanwhile take this (it's dirty and made in 2 minutes during a boring deploy evening, but it does the job).
##
## 20130214, from Varro with love
##
Add-PSSnapin Microsoft.SharePoint.PowerShell
## Variables
$WEB_APPLICATION_URL = "http://teamsite"
$FIELDS_FILE_PATH = "C:\Users\Administrator\Desktop\2013_TeamSite_Fields.csv"
## Initialization
echo "Initializing variables"
'"Field Title","Field Description","Field ID","Field Group","Field Type"' | Out-File $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)
{
$alreadyInCT = $AllContentTypes -contains $contentType.Id;
If (!$alreadyInCT)
{
$AllContentTypes += $contentType.Id;
# Let's write on Content types CSV
ForEach ($field in $contentType.Fields)
{
$alreadyInFields = $AllFields -contains $field.Id;
If (!$alreadyInFields)
{
$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;
}
}
}
}
}
}
echo "See you space cowboy";
Thanks for this post! I was able to use this solution just as you provided it to find what I needed to know about two different environments.
ReplyDeleteSorry for the late reply, it'a pleasure to help. Stay tuned.
DeleteAmazing! Its really amazing piece of writing, I have got much clear idea concerning from this piece of writing.
ReplyDeleteHi mates, how is all, and what you want to say about this article, in my view
ReplyDeleteits actually remarkable in favor of me.
Hi, Neat post. There is a problem along with your
ReplyDeleteweb site in internet explorer, would test this?
IE nonetheless is the market leader and a huge element of other folks will miss
your magnificent writing due to this problem.
Hi, thank you for your comment. The new theme is designed also for IE >= 9 ;)
DeleteGreetings from Carolina! I'm bored at work so I decided to browse your site on my iphone
ReplyDeleteduring lunch break. I really like the info you provide here and can't wait to take
a look when I get home. I'm amazed at how fast your blog loaded on my mobile ..
I'm not even using WIFI, just 3G .. Anyhow, very good
site!
Hi there to every single one, it's really a nice for me to go to see this website, it includes
ReplyDeletepriceless Information.
I'm not surre why bbut this weblog is loading incredibly slow for me.
ReplyDeleteIs anyone else having this issue or iss it a issue on my end?
I'll checck back later and see if the problem still exists.
Hey there! I've bee reading your weblog for some time now and finally got the courage to go ahead and give you a shout out from Dallas
ReplyDeleteTx! Just wanted to say keep upp the excellent work!