2
votes

I have created a library programmatically within a content type my URL is :

/sites/test/_layouts/15/start.aspx#/1235/Forms/AllItems.aspx

I would like to update this URL and change the

12345

by something else on my C# code (a string value that i get somewhere else).

something like :

/sites/test/_layouts/15/start.aspx#/TEST/Forms/AllItems.aspx

I success to change the Title of the library easily but not the URL.

SPList lst = web.Lists[oldVal];
                        lst.Title = properties.AfterProperties["CorporateName"].ToString();
                        lst.Update();

I searched and read many way to do it within Sharepoint Designer, but i didn't find the perfect solution to do it on Visual Studio.

3

3 Answers

2
votes

The SPList object does not have a supported method for being moved to a new URL. But each list has a corresponding folder that does have a method for MoveTo() which will change the Url of the associated list.

SPWeb web = site.OpenWeb()
SPFolder folder = web.Folders["ListURL"];
folder.MoveTo("NewURL");
folder.Update();

This isn't necessarily supported but it does work and I believe it uses the same backend logic as the SharePoint Designer workaround.

A little discussed but related method for dealing with this manually is to rename a list folder while viewing a SharePoint site from Explorer via WebDAV.

1
votes

Thanks Arcan.NET i solved my problem with your help !

SPList lst = web.Lists[properties.ListItem["Name"].ToString()];
SPFolder folder = web.Folders[lst.RootFolder.Url];
folder.MoveTo(properties.AfterProperties["Nickname"].ToString());
0
votes

https://sharepoint.stackexchange.com/questions/168269/change-url-of-a-sharepoint-list-programmatically

Add-PSSnapin Microsoft.SharePoint.PowerShell –erroraction SilentlyContinue

$libOriginalUrl = "/Lists/YourLibName1";
$libNewUrl = "/YourLibName2";
$web = Get-SPWeb -Identity http://....

$lib = $web.GetList($web.Url + $libOriginalUrl)
$rootFolder = $lib.RootFolder;
$rootFolder.MoveTo($web.Url + $libNewUrl)