how to update the SP list Url using powershell.

kem104 160 Reputation points
2024-07-25T18:51:45.7766667+00:00

Hi,

My goal is to udpate the sharepoint list using power automate. I can change the list name from out of the box Sharepoint, however it does not allow me to update the URL. I copied my code below and I am getting following error.

Error changing List URL! Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."

When I search for this error its user access error. however I am the admin and owner of the site. then I tried to execute following code without this lines of code and it works very well. that means may be can not move the list to the rootfoler?

#sharepoint online change library url powershell  

    $List.Rootfolder.MoveTo($NewListURL)

    $Ctx.ExecuteQuery()

 

    #Keep the List name as is

    $List.Title=$ListName

    $List.Update()

    $Ctx.ExecuteQuery()

I am not sure please help me to solve this issue so I can able to update the List URL

Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Config Parameters
$SiteURL= "https://domain.sharepoint.com/sites/TestSite"
$ListName="Lists/TestList1"
$NewListURL="Lists/TestList2"
 
#Setup Credentials to connect
$Cred = Get-Credential
   
Try {
    
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

 	#Get the List
    $List=$Ctx.web.Lists.GetByTitle($ListName)
    $Ctx.Load($List)


     #sharepoint online change library url powershell  
    $List.Rootfolder.MoveTo($NewListURL)
    $Ctx.ExecuteQuery()
 
 #Keep the List name as is
    $List.Title=$ListName
    $List.Update()
    $Ctx.ExecuteQuery()

	
    Write-host -f Green "List URL has been changed!"
}
Catch {
    write-host -f Red "Error changing List URL!" $_.Exception.Message
}


SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,837 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,046 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ling Zhou_MSFT 18,100 Reputation points Microsoft Vendor
    2024-07-26T06:06:10.74+00:00

    Hi @kem104,

    Thank you for posting in this community.

    I tested Patch fox's answer and successfully changed the URL and name of the List.

    I'm guessing your last null-value expression in a method problem is due to your $list being null, please double-check the URL and name of your current list before re-running the power shell.

    Attached below is my PowerShell and test results (in my case changed list1 to listA)

    #SharePoint online site URL 
    $siteUrl = "https://tenant.sharepoint.com/sites/24July" 
    #Current display name of SharePoint list 
    $oldListName = "list1" 
    #New list URL 
    $newListUrl = "https://tenant.sharepoint.com/sites/24July/Lists/listA" 
    #New display name for SharePoint list 
    $newListName = "listA" 
    #Connect to SharePoint online site 
    Connect-PnPOnline -Url $siteUrl -Interactive
    #Get the SharePoint list 
    $list = Get-PnPList -Identity $oldListName 
    #Move SharePoint list to the new URL 
    $list.Rootfolder.MoveTo($newListUrl) 
    Invoke-PnPQuery 
    #Rename List Set-PnPList -Identity 
    Set-PNPList -Identity $oldListName -title $newListName
    
    

    Here is my result: User's image

    You'll notice that the name on the side navigation bar hasn't changed, but you can click Edit below and rewrite the name on the quick navigation bar.

    User's image

    User's image

    Hope this information helps.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. Patchfox 3,926 Reputation points
    2024-07-25T20:25:30.5466667+00:00

    Hi kem104, I want to help you to solve your problem.

    As I understand, you get a 403 error when you want to rename a SharePoint URL of a list item via Powershell, right?

    Firstly, why do you use the SDK and not the widely used PNP module of PowerShell?

    Is it possible to try it with the following example code:

    SharePoint online site URL 
    $siteUrl = "https://contoso.sharepoint.com/sites/SPConnect" 
    Current display name of SharePoint 
    list $oldListName = "Images List" 
    New list URL $newListUrl = "Lists/LogoUniverse" 
    #New display name for SharePoint list 
    $newListName = "Logo Universe" 
    Connect to SharePoint online site 
    Connect-PnPOnline -Url $siteUrl -Interactive
    Get the SharePoint list 
    $list = Get-PnPList -Identity $oldListName 
    Move SharePoint list to the new URL 
    $list.Rootfolder.MoveTo($newListUrl) 
    Invoke-PnPQuery 
    Rename List Set-PnPList -Identity 
    $oldListName -Title $newListName 
    
    

    Source: https://ganeshsanapblogs.wordpress.com/2023/03/22/change-sharepoint-online-list-url-using-pnp-powershell/

    If you get the same error, we can check and troubleshoot again what's going on there and what could cause the problem.


    If the reply was helpful, please don’t forget to upvote or accept it as an answer, thank you!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.