getting Create Error when using Powershell
-
22-10-2019 - |
Question
I am receiving this error on following line
Exception calling "Create" with "4" argument(s): "Value cannot be null. Parameter name: parentFolder" At x:\Dev\PowerShell\MoveFiles2DocSet.ps1:29 char:91 + $newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create <<<< ($docLib.RootFo lder,$DocSet,$cType.Id,$docsetProperties) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($docLib.RootFolder,$DocSet,$cType.Id,$docsetProperties)
$siteURL="http://intranet.colgate.com/sites/blazingWhite"
$docLib = "NewProduct"
$site=Get-SPSite $siteURL
$web=$site.RootWeb
$collFiles=$web.GetFolder($docLib).Files
$count=$collFiles.Count
while($count -ne 0)
{
$item = $collFiles[$count-1].Item
## Create document set if it does not exists
$DocSet = $item["CounterParty"]
$DocSetURL = $siteURL + "/" + $docLib + "/" + $DocSet
$docLibURL = $siteURL + "/" + $docLib
[Microsoft.SharePoint.SPFolder]$targetFolder = $web.GetFolder($DocSetURL)
[Microsoft.SharePoint.SPFolder]$doclibFolder = $web.GetFolder($docLibURL)
Write-Host ""
Write-Host $DocSet
Write-Host $targetFolder
Write-Host $doclibFolder
if (-not $targetFolder.Exists)
{ # Create Doc Set
# Get Document Set Content Type from list
$cType = $web.ContentTypes["Document Set"]
# Create Document Set Properties Hashtable
[Hashtable]$docsetProperties = @{"Title"=$DocSet}
$docsetProperties = @{"Description"=$DocSet}
$docsetProperties = @{"CounterParty"=$DocSet}
Write-Host $cType.Id
Write-Host $docsetProperties
Write-Host ""
# Create new Document Set
$newDocumentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::Create($doclibFolder,$DocSet,$cType.Id,$docsetProperties)
Write-Host "Document set created " $newDocumentSet
if($newDocumentSet.Exists)
{
# changing folder type to Document Set type
$newDocumentSet.Item["ContentTypeId"] = $cType.Id
$newDocumentSet.Item["HTML File Type"] = "SharePoint.DocumentSet"
$newDocumentSet.Item.Update()
}
}
# move file(s) to the document set
Write-Host "$DocSet is the doc set. $collFiles[$count-1].Name is name"
$collFiles[$count-1].MoveTo($siteURL + "/" + $docLib + "/" + $DocSet + "/" + $collFiles[$count-1].Name, $true)
$count--
}
#dispose:
$web.Dispose();
$site.Dispose();
Edit:
The above code is working except document set looks like Folder instead of blue doc set icon. Can anyone shed some light on this please.
Solution
Just by looking at your code.
Create($docLib.RootFolder,$DocSet,$cType.Id,$docsetProperties)
and $docLib
is string?
$docLib = "NewProduct"
maybe this would work
Create($targetFolder,$DocSet,$cType.Id,$docsetProperties)
since
[Microsoft.SharePoint.SPFolder]$targetFolder = $web.GetFolder($DocSetURL)
OTHER TIPS
I'm kind of late to this particular party, but while researching my own issue with document sets appearing as simple folders I came across this blog entry ( http://aknauer.blogspot.com/2011/06/document-sets-und-powershell.html ) which contained this valuable nugget:
"It is also important that you select the content type $cType from the collection of the list and not from the collection of the Web. Otherwise, rather than just a document set a new folder is created."
I switched my content type assignment from the web collection (as Mala had done) to the list collection (as Andreas suggests) and my problem was resolved.