Wednesday, May 15, 2024

Script to get specific list data from multiple sites in SharePoint 2013/2016

 Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue


# Import site URLs and titles from CSV file

$siteurls = Import-Csv -Path "D:\Sathyam\IPP\IPP_Subsites.csv"


# Array to hold all results from all sites

$AllListItemCollection = @()


# Iterate through each site in the CSV

foreach($webs in $siteurls) {

    $web = $null

    try {

        # Get the web object for the site

        $web = Get-SPWeb -Identity $webs.SiteURL  #Siteurl is the column name in the excel file. 

        $siteTitle = $webs.SiteTitle

        # Get the target list

        $list = $web.Lists["Listname"]

        Write-Host $web.Url

        

        # Loop through each item in the list

        $list.Items | foreach {

            $ExportItem = New-Object PSObject

            $ExportItem | Add-Member -MemberType NoteProperty -name "SiteURL" -value $web.Url

            $ExportItem | Add-Member -MemberType NoteProperty -name "Decision Type" -value $_["Milestone_x0020_Type1"]  # Milestone_x0020_Type1 is column internal Name. 

            $ExportItem | Add-Member -MemberType NoteProperty -name "Key Deliverable?" -value $_["Key_x0020_Milestone_x003F_"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Title" -value $_["Title"]

            $ExportItem | Add-Member -MemberType NoteProperty -Name "Item Status" -value $_["Item_x0020_Status"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Rejected" -value $_["Rejected"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Keyword(s)" -value $_["Keywords"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Created" -value $_["Created"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Created By" -value $_["Author"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Modified" -value $_["Modified"]

            $ExportItem | Add-Member -MemberType NoteProperty -name "Modified By" -value $_["Editor"]


            # Add the object to the all results array

            $AllListItemCollection += $ExportItem

        }

    } catch {

        Write-Host "Error accessing site or list: $($_.Exception.Message)" -ForegroundColor Red

    } finally {

        # Dispose of the web object if it was created

        if ($web) {

            $web.Dispose()

        }

    }

}


# Export the aggregated result array to a single CSV file

$AllListItemCollection | Export-CSV "D:\Sathyam\IPP\output\AllSitesOutput.csv" -NoTypeInformation


No comments:

Post a Comment