Script to fetch the list of CSS and JS files

sns 9,246 Reputation points
2022-03-01T10:47:14.223+00:00

Is there any script to fetch the list of CSS, JS files from SharePoin site, Out put should be in CSV and file should show from which library it is coming up.
If yes could u please share it. Thank you.

Microsoft 365 and Office SharePoint For business Windows
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-03-02T08:09:26.317+00:00

    Hi @sns ,
    First ,you can get all lists in SharePoint site and get all items from lists, then filter out the files of type ‘css‘ and ‘js’ ,finally , all the files that meet the conditions are output in csv format.
    Here is an example of using PowerShell to achieve this effect ,” FileRef” will show the file from which library:

    $siteUrl = "https://xxxx.sharepoint.com/sites/xxx"  
    $filePath = "C:\ New folder\xxx.csv"  
      
    Connect-PnPOnline -Url $siteUrl -Credentials (Get-Credential)  
      
    $site = Get-PnPWeb  
      
    $ExportTable = @()  
      
      
    # Get all libraries in sub webs  
      
        $curLibraries = Get-PnPList -Web $site | Where-Object{$_.BaseTemplate -eq 101}  
      
        foreach ($lib in $curLibraries)  
        {  
      
            $listItems = Get-PnPListItem -List $lib -Fields "FileRef", "FileLeafRef", "File_x0020_Type",  "File_x0020_Size", "Created_x0020_By", "Created_x0020_Date", "Modified_x0020_By", "Modified"   -Web $site  
      
            foreach ($item in $listItems)  
            {  
                if($item.FieldValues.File_x0020_Type -eq 'js' -or $item.FieldValues.File_x0020_Type -eq 'css'){  
                $ExportTable += New-Object psobject -Property @{  
                   'FileRef' = $item.FieldValues.FileRef  
                   'FileLeafRef' = $item.FieldValues.FileLeafRef  
                   'Type' = $item.FieldValues.File_x0020_Type  
                   'File_x0020_Size' = $item.FieldValues.File_x0020_Size  
                   'Created_x0020_By' = $item.FieldValues.Created_x0020_By  
                   'Created_x0020_Date' = $item.FieldValues.Created_x0020_Date  
                   'Modified_x0020_By' = $item.FieldValues.Modified_x0020_By  
                   'Modified' = $item.FieldValues.Modified  
                   'SiteUrl' = $site.ServerRelativeUrl  
                   'SiteName' = $site.Title  
                   'UniqueID' = $item.FieldValues.UniqueId  
                }  
                }  
            }  
        }  
      
      
    # Export results into csv  
    $ExportTable | select FileRef, FileLeafRef, Type, File_x0020_Size, FileSizeDisplay, Author, Created_x0020_By, Created_x0020_Date, Editor, Modified_x0020_By, Modified, SiteUrl, SiteName, UniqueID | Export-Csv $filePath -NoTypeInformation  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.


    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.