question

sns1 avatar image
0 Votes"
sns1 asked sns1 commented

script to get list of contant type from SC

is there any script to generate content type GUID along with list name, content type name and sub site name for the entire site collection?
If yes please share , output should be exported to excel

office-sharepoint-online
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

ElsieLu-MSFT avatar image
0 Votes"
ElsieLu-MSFT answered sns1 commented

Hi @sns1 ,

To loop through all the subsites:

 #Load SharePoint Online Assemblies
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
       
 ##Variables for Processing
 $SiteUrl = "https://crescent.sharepoint.com/sites/Sales/"
 $UserName="Salaudeen@crescent.com"
 $Password ="Password goes here"
      
 #Setup Credentials to connect
 $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
      
 #sharepoint online powershell get subsites
 Function Get-SPOWeb() {
 param(
     $WebURL = $(throw "Please Enter the Site Collection URL")
 )
     
     #Get Web information and subsites
     $Context = New-Object Microsoft.SharePoint.Client.ClientContext($webURL)
     $Context.Credentials = $credentials
     $Web = $context.Web
     $Context.Load($web)
     
     #powershell cmdlet to retrieve sharepoint online subsites
     $Context.Load($web.Webs)
     $Context.executeQuery()
     
     #Do something with the current sub-site
     Write-host $Web.URL
     
     #Iterate through each subsite in the current web
     foreach ($Subweb in $web.Webs)
     {
         #Call the function recursively to process all subsites underneath the current web
         Get-SPOWeb($Subweb.url)
     }
 }
     
 #Call the function
 Get-SPOWeb -WebURL $SiteUrl

Test result:
129823-56.jpg
Reference:
SharePoint Online: PowerShell to Get All Subsites in a Site Collection
===========================
To loop through all the lists:

 Try{
 Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
 Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
 }
 catch {
 }
 $siteUrl = "https://xxx.sharepoint.com/sites/TeamMisTest2"
 $username = "sion@xxx.onmicrosoft.com"
 $password=ConvertTo-SecureString "xxxx" -AsPlainText -Force
 $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
 $ctx.Credentials = $credentials
 $web = $ctx.Web
 $lists = $web.Lists
 $ctx.Load($lists)
 $ctx.ExecuteQuery()
 $lists| select -Property Title, ID | export-csv -Path C:\lists.csv -NoTypeInformation

Reference:
Retrieve all list names and list guids from SharePoint Online site using PowerShell
===========================
To get content types name and id from a list:

 #Load SharePoint CSOM Assemblies
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
 Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
      
 #Set parameter values
 $SiteURL="https://crescenttech.sharepoint.com/"
 $ListName="Projects"
     
 #Get Credentials to connect
 $Cred= Get-Credential
       
 #Setup the context
 $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
 $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
      
 #Get the List
 $List = $Ctx.Web.lists.GetByTitle($ListName)
     
 #Get All content types of the list
 $ContentTypes = $List.ContentTypes
 $Ctx.Load($ContentTypes)
 $Ctx.ExecuteQuery()
     
 #Get Content Type Details
 $ContentTypes | Select Name,Description, ID

Reference:
SharePoint Online: Get List Content Types using PowerShell
Note: Microsoft is providing this information as a convenience to you. The sites are not controlled by Microsoft. Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please make sure that you completely understand the risk before retrieving any suggestions from the above link.


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


56.jpg (24.3 KiB)
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

HI Elsie Lu,

Do we need to execute both of the above at same time?
PFA, where highlighted with red circled I am not finding in the path in my system.

FYI: I can use pnp powershell command to connect the site.

Please help further.

0 Votes 0 ·

Hi Elsie Lu,

Did you have a chance to look in to my above thread?

0 Votes 0 ·