How to rename folder in document library in sharepoint using Csom Script?
lp
How to rename folder in document library in sharepoint using Csom Script?
lp
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2023, 12:06 PM EDT
using System;
using Microsoft.SharePoint.Client;
namespace RenameFolderCSOM
{
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://yoursharepointsiteurl";
string username = "yourusername";
string password = "yourpassword";
ClientContext context = new ClientContext(siteUrl);
context.Credentials = new SharePointOnlineCredentials(username, ConvertToSecureString(password));
Web web = context.Web;
List list = web.Lists.GetByTitle("Your Document Library");
context.Load(list);
Folder folder = list.RootFolder.Folders.GetByUrl("Your Folder URL");
context.Load(folder);
folder.MoveTo("New Folder Name");
context.ExecuteQuery();
Console.WriteLine("Folder renamed successfully.");
}
private static System.Security.SecureString ConvertToSecureString(string password)
{
System.Security.SecureString securePassword = new System.Security.SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
return securePassword;
}
}
}
Nidhi Raj14 reputationPosted Aug 14, 2023, 12:50 PM EDT
Can you please help me with renaming of folder in document library in SharePoint using Csom as there is folder inside folder in that particular document library.In that case how can one able to rename folder? Kindly help me with this
Cr BhargaviRank #420 · 5.9K reputationPosted Aug 14, 2023, 11:28 PM EDT
using Microsoft.SharePoint.Client; using System; class Program { static void Main(string[] args) { string siteUrl = "https://yourtenant.sharepoint.com/sites/yoursite"; string libraryName = "YourDocumentLibrary"; string oldFolderName = "OldFolderName"; string newFolderName = "NewFolderName"; using (ClientContext context = new ClientContext(siteUrl)) { context.Credentials = new SharePointOnlineCredentials("username", GetSecureString("password")); Web web = context.Web; List library = web.Lists.GetByTitle(libraryName); Folder folder = library.RootFolder.Folders.GetByUrl(oldFolderName); folder.MoveTo($"{libraryName}/{newFolderName}"); context.ExecuteQuery(); Console.WriteLine("Folder renamed successfully."); } } private static System.Security.SecureString GetSecureString(string input) { System.Security.SecureString secureString = new System.Security.SecureString(); foreach (char c in input) { secureString.AppendChar(c); } return secureString; } }