question

HeKa-1767 avatar image
0 Votes"
HeKa-1767 asked HeKa-1767 answered

Import CSV with a folder structure to sharepoint online

Hello everybody,

I'm currently trying to import a library and folder structure to sharepoint online from a CSV.
My csv looks like this:

 Position1;Position2;Position3
 0 Basics;;
 1 LibraryA;;
  ;1.1 FolderA_in_LibA;
  ;1.2 FolderB_in_LibA;
  ;;1.2.1 SubfolderA_in_FolderB
 2 LibraryB;;

First column is a document library (added with "new-pnplist -Title -Template DocumentLibrary"), second column is a first folder, third column is a subfolder of second.

How can I manage that when creating e.g. the folder 1.2 the last used Position 1 (document library) is used? Same issue when creating the the subfolder 1.2.1.

$importcsvfile | foreach {
add-pnpfolder -Name $importcsvfile.Position3 -Folder LASTUSED_$importcsvfile.Position2
}

I hope you understand what I mean...not too familiar with this kind of stuff :)

Thanks in advance

windows-server-powershell
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.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered RichMatheisen-8856 edited

Using your original CSV example:

 $csv = @"
 Position1;Position2;Position3
 0 Basics;;
 1 LibraryA;;
  ;1.1 FolderA_in_LibA;
  ;1.2 FolderB_in_LibA;
  ;;1.2.1 SubfolderA_in_FolderB
 2 LibraryB;;
 "@
 $Pos1 = ""
 $Pos2 = ""
 $Pos3 = ""
 foreach ($row in ($csv | ConvertFrom-Csv -Delimiter ';')) {
         if ($row.Position1.Trim().Length -gt 0) {
             $Pos1 = $row.Position1.Trim()
             # create folder using $Pos1
             # or just skip it if you've already created the top-level folder
             Write-Host "Name: $Pos1  Folder: ???"
             Continue
         }
         if ($row.Position2.Trim().Length -gt 0) {
             $Pos2 = $row.Position2.Trim()
             # create folder using $Pos1 and $Pos2
             Write-Host "Name: $Pos2  Folder: $Pos1"
             Continue
         }
         if ($row.Position3.Trim().Length -gt 0) {
             $Pos3 = $row.Position3.Trim()
             # create folder using $Pos1, $Pos2, and $Pos3
             Write-Host "Name: $Pos3  Folder $Pos1_$Pos2"
         }
 }
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.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Why make the CSV into a complicated structure? Wouldn't this be a much easier data set to deal with?

 Position1;Position2;Position3
 0 Basics;;
 1 LibraryA;;
 1 LibraryA;1.1 FolderA_in_LibA;
 1 LibraryA;1.2 FolderB_in_LibA;
 1 LibraryA;1.2 FolderB_in_LibA;1.2.1 SubfolderA_in_FolderB
 2 LibraryB;;
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.

RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

Using a simpler CSV content:

 $csv = @"
 Position1;Position2;Position3
 0 Basics;;
 1 LibraryA;;
 1 LibraryA;1.1 FolderA_in_LibA;
 1 LibraryA;1.2 FolderB_in_LibA;
 1 LibraryA;1.2 FolderB_in_LibA;1.2.1 SubfolderA_in_FolderB
 2 LibraryB;;
 "@
    
 foreach ($row -in ($csv | ConvertFrom-Csv -Delimiter ';') {
     if ($row.Position1.Trim().Length -gt 0){
         if ($row.Position2.Trim().Length -gt 0){
             if ($row.Position3.Trim().Length -gt 0){
                 # create folder using all three names
             }
             else {
                 # create folder using 1st two names
             }
         }
         else{
             # create folder using only 1st name
             # or just skip it if you've already crated the top-level folder
         }
     }
 }
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.

HeKa-1767 avatar image
0 Votes"
HeKa-1767 answered

Hey Rich,
thanks for your ideas.
The thing is, I'm not making a complicated CSV, it is a converted excel list and to create it the way I posted first is the easiest and quickest way of making a CSV out of it (without having to edit/do much in the xlsx or csv).

In the sample CSV file I was given it is even a little bit more complicated. There is at least one folder named "name/name" which will not work.

I'm now trying to create a correct CSV first;

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.

HeKa-1767 avatar image
0 Votes"
HeKa-1767 answered

Thanks a lot, seems to work like a charm :)

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.