无法创建 SPFolder 层次结构。 .Update() 不断抛出异常

Unable to create SPFolder hierachy. .Update() keeps throwing exception

我正在尝试将一个文档库的文件夹结构复制到另一个,

这总是失败,例外情况是:

Update : Exception calling "Update" with "0" argument(s): "Dieser Vorgang kann nur für eine Datei ausgeführt werden. Bei "http://machine/CopyTest2/EUR/Germany" handelt es sich um einen Ordner."

免费翻译:

Update : Exception calling "Update" with "0" argument(s): "This action can only be invoked for a file. "http://machine/CopyTest2/EUR/Germany" is a folder."

这是我目前使用的代码:

foreach ($Folder in $AllFolders) {
    $ParentFolderURL = ""
    $i = 1 #Set 1 so we ignore the first part of the url, which is the library name. we only want the folders
    $FolderURL = $Folder.url.Split("/")

    while ($i -lt ($FolderURL.count - 1)) {
        $ParentFolderURL = "$ParentFolderURL/" + $FolderURL[$i]
        $i++
    }
    # Targetfolder is now http://server/copytest1/EUR
    $targetFolderUrl = "$($DestinationWebURL)/$($dList.Title)$($ParentFolderURL)"
    #check if the folder exists by querying web.GetFolder($targetFolderUrl) and check if it is null or not

    if (!($dWeb.GetFolder($targetFolderUrl)).Exists) {
        $NewFolder = $dlist.Folders.Add($targetFolderUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Name)
        $NewFolder["Title"] = $Folder.Name
        $NewFolder.Update()
    }
    else {
        #If the folder already exists, retrieve the folder where the file will be created
        $NewFolder = $dList.Folders | Where-Object {$_.name -eq $Folder.Name}
    }
    #...
}

我很确定故障线是 $NewFolder = $dlist.Folders.Add($targetFolderUrl, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Name) 但我不知道相对 url 的正确值应该是多少。

我试过的替代方案,其中相对 urls:

的组合
$NewFolder = $dlist.Folders.Add("/$($dList.Title)$($ParentFolderURL)", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Name)
$NewFolder = $dlist.Folders.Add($ParentFolderURL, [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Name)

但是 none 确实有效。

好的。一如既往:PEBCAK

如果 $ParentFolderUrl 为空,

$targetFolderUrl 将指向列表本身。此外,文件夹名称本身也丢失了。错误的逻辑从那里向下层叠。

$targetFolderUrl = "$($DestinationWebURL)/$($dList.Title)$($ParentFolderURL)/$($Folder.Name)"

如我的问题所述,relativeUrl 参数也有误:

$NewFolder = $dlist.Folders.Add("/$($dList.Title)$ParentFolderURL", [Microsoft.SharePoint.SPFileSystemObjectType]::Folder, $Folder.Name)

这 2 项更改纠正了我的错误,现在几乎可以立即创建完整的层次结构。