0

I was able to find the following code for SharePoint Online CSOM (Powershell) and it's working great, the issue is that I want to move the child nodes out of "Récents" because otherwise they are deleted as well (we are using French sites, "Récents" is the French term for "Recent"):

 $username = "censored"
 $password = "censored"
 $url = "censored"
 $securePassword = ConvertTo-SecureString $password -AsPlainText -Force

 Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
 Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
 Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Taxonomy.dll")
 Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Publishing.dll")

 $clientCtx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
 $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$securePassword)
 $clientCtx.Credentials = $credentials

 $QuickLaunchNodes = $clientCtx.Web.Navigation.QuickLaunch
 $clientCtx.Load($QuickLaunchNodes)
 $clientCtx.ExecuteQuery()
 $clientCtx.Dispose()
 for($i=$QuickLaunchNodes.Count-1;$i -ge 0; $i--)
 {
      if($QuickLaunchNodes[$i].Title -eq "Récents")
      {
           $QuickLaunchNodes[$i].DeleteObject()
      }
 }
 $clientCtx.ExecuteQuery()
 $clientCtx.Dispose()

Is there a way to get all the childs and move them out to the parent menu before deleting the node?

Thank you for your time and help.

1 Answer 1

0

Try with this code:

if($QuickLaunchNodes[$i].Title -eq "Récents")
{
    $clientCtx.Load($QuickLaunchNodes[$i].Children)
    $clientCtx.ExecuteQuery()

    for($j=$QuickLaunchNodes[$i].Children.Count-1;$j -ge 0; $j--)
    {
        $c = $QuickLaunchNodes[$i].Children[$j]
        $nav = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
        $nav.Url = $c.Url
        $nav.Title = $c.Title
        $result = $clientCtx.Web.Navigation.QuickLaunch.Add($nav)
    }
   $QuickLaunchNodes[$i].DeleteObject()
   break;
}

It will go through the children of the Recent node, copy them to the root, and after that delete the Recent node.

EDIT: It looks that powershell has some strange behavior regarding the collections. It throws an error when you try to add an element to collection saying that the collection has not been initialized (although it has!). I was able to solve the problem with help from this thread.

The key thing was to get the result of adding the element to the collection and save it in variable.

$result = $clientCtx.Web.Navigation.QuickLaunch.Add($nav)
3
  • I had to change "NavigationNodeCreationInformation()" to "NavigationNodeCreationInformation" (without parantheses) for it to work, also it does copy the childs correctly, but whenever "Récents" got a child, I get an error and it is not deleted: format-default : The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. If "Récents" is empty, I receive no error. Commented Feb 10, 2017 at 16:57
  • Please see my updated answer and code. I've just tested it and it works now. Commented Feb 10, 2017 at 18:45
  • I confirm that it's working. Thanks a lot for your time help! This is greatly appreciated! I accepted this answer as the solution. Commented Feb 10, 2017 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.