Monday, July 15, 2013

SharePoint 2010 - Adding Folders and Items inside Folder using PowerShell

I came across situation where I need to add the Folders and items within that folders using PowerShell to check threshold problem. I thought adding the items more than the threshold value, PowerShell will be ideal to run on SharePoint System.

Here is the PowerShell Script which creates 30 folders inside the Custom List and 200 items in each Folder.
Only requirement to run this Script is custom list named as “ProjectPhones”.

Note:
  • You can have your own list. Just change the name in script (Highlighted)
  • While running script add your Site Url as parameter where the list is situated
PowerShell Script:

Add-PsSnapin Microsoft.SharePoint.PowerShell


# read site url
$siteUrl=$args[0]

write-host $siteUrl
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
write-host $site
$web = $site.OpenWeb()

write-host $web
$list = $web.Lists["ProjectPhones"]
write-host $list

$F=1
$i=1


for($F=1; $F -le 30; $F++)
{
                write-host "Creating folder - Folder " + $F
                $folder = $list.AddItem("", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder)
                $folder["Title"] = "Folder" + $F
                $folder.Update();


                for($i=1; $i -le 200; $i++)
                {
                                write-host "Adding " + $i  + " Item in Folder[" + $F + "]"

                                $ListItem = $list.AddItem($folder.Folder.ServerRelativeUrl,[Microsoft.SharePoint.SPFileSystemObjectType]::File, $null)

                                $ListItem["Title"] = "PhoneNumber Folder-" + $F + "-" + $i
                               

                                $ListItem.Update()
                }
}
$web.Dispose()
$site.Dispose()


Remove-PsSnapin Microsoft.SharePoint.PowerShell

Hope this helps anyone in need.

No comments: