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();
        }
    }
}

 

.

13 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

Anonymous said...

Hello,

Is there a way to get a source/ location/ URL of a content type in a site using a PowerShell script? I am looking for a specific content type in my web application which has lots of site collection so I want to know in which specific site the content type is located.

Please help.

Mike Smith said...

Anonymous,

Are you looking for a Content Type and where it is defined, or all of the documents that use that Content Type?

Mike

Anonymous said...

Hello Mike,

What I am looking for is, the site URL where that specific content is located or created originally/where it is defined. Because my main goal is to get the content type and rename it.

Appreciate your help!

Mike Smith said...

See if this is what you need: (Searching for a content type named "Memos".)

Get-SPSite -Limit All |
where {-not ($_.rootweb.url -like "*sitemaster-*") } |
foreach { $_.rootweb } |
select -ExpandProperty ContentTypes |
where {$_.name -eq "Memos"} |
select name, id, {$_.ParentWeb.Url} | ft -AutoSize

The above is for PowerShell running on a SP server. (i.e. not SP Online)
The "sitemaster" line is only needed for SP 2016.

Mike

Anonymous said...

Thanks for the script but “$_.ParentWeb.Url” field is giving me the URL of the web application but what I want is a specific URL of a sub site under the web application where the content type is defined.
Sorry for bothering you

Mike Smith said...

> but “$_.ParentWeb.Url” field is giving me the URL of the web application

Content Types exist at the Site Collection level, and $_.ParentWeb.Url should be listing the URL of the site collection.

Are you looking for the URLs of documents that use a Content Type?

Mike

Anonymous said...

Mike,

Thanks a lot for all your help. I got another workaround to get the content types.

Thanks again!

Mike Smith said...

Please share! Post a link to the solution...

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.