Skip to content

Instantly share code, notes, and snippets.

@phillipharding
Last active August 29, 2015 14:04
Embed
What would you like to do?
Create Folder Tree's for SharePoint Lists and Libraries
function EnsureListFolderTree($web, $list, $folderName) {
$curl = $list.RootFolder.ServerRelativeUrl
$folders = $folderName.Replace("^/+","").Split("/")
ForEach($fn in $folders) {
$furl = $curl + "/" + $fn
$f = $web.GetFolder($furl)
if ($f.Exists -eq $false) {
$f = $list.AddItem($curl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $fn)
$f.Update()
$f = $web.GetFolder($f.Folder.ServerRelativeUrl)
}
$curl = $f.ServerRelativeUrl
}
$f
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment