Ensure_List_Folder

Language:
PowerShell
Keywords:
SharePoint
Code Snippet

## Ensures that the SPList has a folder

Function Ensure_List_Folder( [Microsoft.SharePoint.SPList]$tList

                           , $folderName

                           ) {

    if ($tList.ParentWeb.GetFolder( ( [System.String]::Format("{0}/{1}", $tList.RootFolder.Url , $folderName) ) ) -ne $null) {

        # Calculate parent folder's server relative path... $tList.RootFolder + folderName.RemoveAfterLast("/")

        $folder_Parent_ServerRelativePath = ( [System.String]::Format("{0}/{1}", $tList.RootFolder.ServerRelativeUrl , $folderName.Replace("\","/") ) )

        $folder_Parent_ServerRelativePath = $folder_Parent_ServerRelativePath.SubString(0 , $folder_Parent_ServerRelativePath.LastIndexOf("/") )

 

        $tFolder = $tList.Items.Add( $folder_Parent_ServerRelativePath , [Microsoft.SharePoint.SPFileSystemObjectType]::Folder , $folderName.SubString($folderName.Replace("\","/").LastIndexOf("/") + 1) )

        Write-Output([System.String]::Format("List [{0}] : Creating Folder [{1}]", $tList.Title, $folderName))

        $tFolder.Update()

    }

} # Function Ensure_List_Folder(...)


Created 2012-10-03
comments powered by Disqus
Login