0

I am trying to convert folders to document sets. As there are a lot of folders and many libraries spread around many subsites, I am querying items based on a specific content type and then converting them to a Document Set content type. Conversion is done, but when the provision() method is called as shown in the code below:

      $documentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::GetDocumentSet($item.Folder)
        $documentSet.Provision()

I get the following error:

Exception calling "Provision" with "0" argument(s): "Object reference not set to an instance of an object."
At C:\Dev\copyfields.ps1:35 char:13
+             $documentSet.Provision()
+             ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : NullReferenceException

However, if I do a foreach loop as follows: foreach($folder in $list.Folders) then provision does not throw an error. But this method takes a lot of time more then a day to finish everything.

How can I improve my query and make the conversion to document set while using caml query? Any ideas or how can I improve my code below?


Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

$web = Get-SPWeb "http://sp2016"

$newCT = $web.ParentWeb.ContentTypes["TestDocSet"]
if($null -ne $newCT){
   $list = $web.Lists["2021"]

   if ($list) 
   { 
         $query = New-Object Microsoft.SharePoint.SPQuery; 
         $query.Query = 
         "<Where>
            <Eq>
               <FieldRef Name='ContentType' />
               <Value Type='Computed'>Folder</Value>
            </Eq>
         </Where>";
         $query.ViewAttributes = "Scope='RecursiveAll'"; 
         $items = $list.GetItems($query); 
         
         for($i=$items.Count-1;$i -ge 0;$i--){
            $item = $items[$i]
            Write-host $item["ContentType"] -ForegroundColor Green
            $item["ContentTypeId"] = $newCT.Id
            $item.ProgId = “SharePoint.DocumentSet”
            $item.SystemUpdate($false)

            $documentSet = [Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet]::GetDocumentSet($item.Folder)
            $documentSet.Provision()
         }
   }
   
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.