another technical blog...technically

Tuesday, March 5, 2013

Get site content types and site columns using PowerShell

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:
  1. Site content types
  2. Site columns
  3. 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.
Share:
written in: Milano, Italia

12 comments:

  1. Hello,

    I'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";

    ReplyDelete
    Replies
    1. Hi Anonymous one,
      i 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";

      Delete
  2. 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.

    ReplyDelete
    Replies
    1. Sorry for the late reply, it'a pleasure to help. Stay tuned.

      Delete
  3. Amazing! Its really amazing piece of writing, I have got much clear idea concerning from this piece of writing.

    ReplyDelete
  4. Hi mates, how is all, and what you want to say about this article, in my view
    its actually remarkable in favor of me.

    ReplyDelete
  5. Hi, Neat post. There is a problem along with your
    web 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.

    ReplyDelete
    Replies
    1. Hi, thank you for your comment. The new theme is designed also for IE >= 9 ;)

      Delete
  6. Greetings from Carolina! I'm bored at work so I decided to browse your site on my iphone
    during 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!

    ReplyDelete
  7. Hi there to every single one, it's really a nice for me to go to see this website, it includes
    priceless Information.

    ReplyDelete
  8. I'm not surre why bbut this weblog is loading incredibly slow for me.
    Is 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.

    ReplyDelete
  9. 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
    Tx! Just wanted to say keep upp the excellent work!

    ReplyDelete

Because of a lot of SPAM about courses, I need to moderate all comments here.
I ensure you that I will answer whenever possible (if you are not a spammer).

Me, myself and I

My Photo
I'm just another IT guy sharing his knowledge with all of you out there.
Wanna know more?