1/11/2011

PowerShell to find SharePoint Content Types

 

A few quick PowerShell scripts…

 

Find all lists that support a particular content type:

 

$webs = get-spsite http://yourserver/sites/yoursite | get-spweb 

foreach ($web in $webs) 
{
foreach ($lst in $web.lists)
{
foreach ($ctype in $lst.ContentTypes)
{
if ($ctype.Name -eq "Document")
{ $lst.DefaultViewUrl }
}
}
$web.Dispose()
}

(Can be typed as all one line.)

 

For SharePoint 2007 replace the first line with these three:

    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

    $site = New-Object Microsoft.SharePoint.SPSite(http://yourserver/sites/yoursite)

    $webs = $site.AllWebs

 

 

Find all list items of a particular content type:

 

$webs = get-spsite http://yourserver/sites/yoursite | get-spweb 

foreach ($web in $webs)
{
foreach ($lst in $web.lists)
{
foreach ($item in $lst.Items)
{
if ($item.ContentType.Name -eq "Document")
{ $item.Url}
}
}
$web.Dispose()
}

(Can be typed as all one line.)

 

Get a list of all Content Types in a web:

$site = Get-SPSite http://yourserver/sites/yoursite

$web = $site.RootWeb

foreach ($ctype in $web.ContentTypes) {$ctype.Name}

These examples used the content type’s name property. You could also use the ID property.

 

 

C#

And for completeness, here’s a C# version to find all lists that support a particular content type:

using System;
using Microsoft.SharePoint;

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            SPSite site = new SPSite(http://yourserver/sites/yoursite);
            
            SPWebCollection webs = site.AllWebs;
            foreach (SPWeb web in webs) { 
                foreach (SPList lst in web.Lists) { 
                    foreach (SPContentType ctype in lst.ContentTypes) { 
                        if (ctype.Name == "Document") {
                            Console.WriteLine(lst.DefaultViewUrl);
                        } 
                    }
                }
                web.Dispose();
            }
            site.Dispose();
        }
    }
}

 

.

5 comments:

Mike said...

Does this work for SharePoint Online with Office 365?

Mike Smith said...

Mike,

No. PowerShell requires server access and we don't have that for 365. PowerShell is used with 365, but only for user and general admin.

Mike

bhanua said...

Mike,
I am trying to extract values from each content type, we want consolidate those data for meta data. So what i am trying to do, get all the content types and data entered by the users for that particular content type item.
If you have any idea about that, please give some clue.

Thanks
Bhanu

Sharepoint apprentice said...

Could you please help me with PowerShell commands that would list all the content types for a workflow and then list the fields for each of those content types.

My next step would be to update a field it is incorrect.

Thanks for any help you can give in advance.

Mike Smith said...

Sharepoint apprentice,

SharePoint Designer workflows or Visual Studio workflows (i.e. did SPD create the content types or did you or the developer)?

Update fields? Change the content type itself, or change documents/list items using those content types?

Mike

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.