iklan banner
MASIGNCLEAN104

Creating SharePoint Folders and Items with PowerShell

iklan banner
I was working on a customer problem and needed a very large list to try to reproduce a problem.  I typically fire up Visual Studio and create Yet Another Console App.  You know, one of the hundred or so “ConsoleApplication1” projects that litter your My Documents/Visual Studio/Projects folder?  I decided to force myself to do this with PowerShell instead, and the result was not only quick, but also something I could do on a machine that doesn’t have Visual Studio installed.

Not only did I need many items in the list, I also needed a weird structure… 10 folders, each having 10 folders, each having 10 folders, each having 50 items.


Creating a folder in SharePoint is easy, but not straightforward.  You have to use the SPFileSystemObjectType.Folder enumeration value when creating a list item.  You can see the syntax for that below.  Since we want to create nested folders, you can see how we use the relative URL to accomplish that as well.

Here is the script I came up with.  Note!  Before you run this in your environment, understand that it creates 50,000 items!  You might want to change the number of loops to reduce the number of items being created, or better yet, just read the code and adapt it for your situation.

$spAssignment = Start-SPAssignment
$mylist = (Get-SPWeb -identity http://portal.sharepoint.com -AssignmentCollection $spAssignment).Lists["LargeList"]

for($i=1; $i -le 10; $i++)
{
    $folder = $mylist.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
    $folder["Title"] = "folder$i"
    $folder.Update();
    for($j=1; $j -le 10; $j++)
    {
        $s1folder = $mylist.AddItem($folder.Folder.ServerRelativeUrl, 
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
        $s1folder["Title"] = "subfolder$j"
        $s1folder.Update();
         for($k=1; $k -le 10; $k++)
        {
            $s2folder = $mylist.AddItem($s1folder.Folder.ServerRelativeUrl, 
[Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
            $s2folder["Title"] = "subsubfolder$k"
            $s2folder.Update();
                        
            for($l=1; $l -le 50; $l++)
            {
                
                #Create item
                
                $newItem = $mylist.AddItem($s2folder.Folder.ServerRelativeUrl,
[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
                $newItem["Title"] = "Item $i $j $k $l"
                $newItem["FirstName"] = "FirstName $i $j $k $l"
                $newItem["LastName"] = "LastName $i $j $k $l"
                $newItem["Company"] = "Company $i $j $k $l"
                $newItem.Update()
            }
        }   
    }
}
Stop-SPAssignment $spAssignment


The code above is sourced from here, this blog is only a reminder of my needs as a newbie developer for cases in the world of my work, below is the implementation of the code that I made from the implementation of the code above. only just make one level folder for this

-------------------Code---------------------------------------------------------------

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force

## Get Web
$Web = Get-SPWeb "http://sharepointsite/Apps"
$Listsource= $Web.Lists["Meeting Room Book"]
$Listdestination= $Web.Lists["meetingroomtry"]

$urlsource = $Listsource.RootFolder.Url
$itemssource = $Listsource.items

$urldestination = $Listdestination.RootFolder.Url
$itemsdestination = $Listdestination.items

#Write-host -foregroundcolor red "Total number of items found in the Root: "$url
#Write-host -foregroundcolor red "Total number of items found in the Root: "$items.count

 foreach($item in $itemssource)
 {
$BookingDate = $item['Booking_x0020_Date'];
$year = ([System.DateTime] $BookingDate).Year
#$month = ([System.DateTime] $BookingDate).Month
$month = ([System.DateTime] $BookingDate).ToString('MM')
$ListPath = $Web.ServerRelativeUrl + "/" + $urldestination + "/";
$FolderPath = $ListPath + $year + $month;
$Folder = $Web.GetFolder($FolderPath);
$foldername = "$($year)$($month)";

# Create the Year folder if it does not already exist
if (-not $Folder.Exists)
{
$Folder = $Listdestination.Folders.Add("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
$Folder["Title"] = $foldername
$Folder.Update();
}

$TargetFolder = $Listdestination.ParentWeb.GetFolder($Listdestination.RootFolder.Url + "/" +$foldername);

#Write-host -foregroundcolor yellow "TargetFolder: " $FolderPath
#Write-host -foregroundcolor red "TargetFolder: " $Folder.Folder.ServerRelativeUrl

$newitem= $Listdestination.Items.Add($FolderPath,[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)
$newitem["Title"] = $item["Title"]
$newitem["Booking_x0020_Date"] = $item["Booking_x0020_Date"]
$newitem["End_x0020_Time"] = $item["End_x0020_Time"]
$newitem["Booked_x0020_By"] = $item["Booked_x0020_By"]
$newitem["Display"] = $item["Display"]
$newitem["Category"] = $item["Category"]
$newitem["Status"] = $item["Status"]
$newitem["Recurrence"] = $item["Recurrence"]
$newitem["Meeting_x0020_Title"] = $item["Meeting_x0020_Title"]
$newitem.update()
#break;
 }


-----------------------------------end code -------------------------------------------------



Share This :