0

My goal is to upload approximately 10,000 folders to a SharePoint library (call it T2) and create a list in SharePoint (say T1) that lists these folders along with other details and their corresponding links for access.

Users can access this T1 list, filter, search, and sort. If they want to view a specific case, they can click on the link and access the folder (in T2). I’ve managed to upload a portion of the folders for testing, and I’ve also figured out how to create the list with all the relevant fields.

However, I’m having difficulty understanding how to obtain the links to the folders. Moreover, it appears that by default, such links are not present. Let me explain further: I manually copied the link to one of the folders (in T2) for testing purposes and added it to the list (in T1). Later, I found an online script and tried it out. Interestingly, only one folder from the library appeared when I ran the script. I manually copied a second folder link and ran the script again, and both folders showed up. It seems that I need to authorize sharing first.

For creating folders and files, I use this piece of code:

#Function to upload all files from a local folder to SharePoint Online Folder
Function Upload-PnPFolder($LocalFolderPath, $TargetFolderURL)
{
    Write-host "Processing Folder:"$LocalFolderPath -f Yellow
    #Get All files and SubFolders from the local disk
    $Files = Get-ChildItem -Path $LocalFolderPath -File
 
    #Ensure the target folder
    Resolve-PnPFolder -SiteRelativePath $TargetFolderURL | Out-Null
 
    #Upload All files from the local folder to SharePoint Online Folder
    ForEach ($File in $Files)
    {
        Add-PnPFile -Path "$($File.Directory)\$($File.Name)" -Folder $TargetFolderURL -Values @{"Title" = $($File.Name)} | Out-Null
        Write-host "`tUploaded File:"$File.FullName -f Green
    }
}

The code I'm using to get the link is this one: How to get a list of shared links in a SharePoint Online document library? Any PowerShell or other way?

How do I get the list of ALL the uploaded folders?

Thanks for all the help.

1
  • Would it not be better to create the actual list item after running resolve-pnpfolder? Are the folders in T2 not shared with everyone from start? Commented Mar 1, 2024 at 7:44

1 Answer 1

0

I managed to find a solution, modified the code i found online a bit and it worked

#Config Variables
$SiteURL = "https://XXXXXX/sites/Mysubsite"
$TargetFolderURL = "MyDocs" #Site Relative Path of the Library

$fileJSON="C:\itemlist_demo.json"
$JSONData = Get-Content -Path $fileJSON -Raw | ConvertFrom-Json

 
#Connect with SharePoint Online
Connect-PnPOnline -Url $SiteURL -Interactive
$Ctx = Get-PnPContext

$ListItems = Get-PnPListItem -List $TargetFolderURL

$FoldersINListItems = $ListItems
 
ForEach($Item in $FoldersINListItems)  
{
    $prid= $Item.FieldValues['FileLeafRef'].Remove(0,2) #take suffix out to get ID

    if ($JSONData.'PRID' -contains $prid)
    {
        $JsonNode = $JSONData | Where {$_.'PRID' -eq $prid}

        $HasUniquePermissions = Get-PnPProperty -ClientObject $Item -Property "HasUniqueRoleAssignments"

        #Get Shared Links
        $SharingInfo = [Microsoft.SharePoint.Client.ObjectSharingInformation]::GetObjectSharingInformation($Ctx, $Item, $false, $false, $false, $true, $true, $true, $true)
        $ctx.Load($SharingInfo)
        $ctx.ExecuteQuery()

        ForEach($ShareLink in $SharingInfo.SharingLinks)
        {
            If($ShareLink.Url)
            {           
                If($ShareLink.IsEditLink)
                {
                    $AccessType="Edit"
                }
                ElseIf($shareLink.IsReviewLink)
                {
                    $AccessType="Review"
                }
                Else
                {
                    $AccessType="ViewOnly"
                    $JsonNode.'SPLink' = $ShareLink.Url

                }
                 
            }
        }

    }
}

$JSONData | ConvertTo-Json | Set-Content  -Path $fileJSON

This way i can store the links in a json file. In the next step i run throught all the nodes in the json file and create the items in my list

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.