Search This Blog

Monday, September 16, 2019

SharePoint Online- Rename document Library filename - using client context c#


Dowload code 

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading.Tasks;

namespace RenameSPFileDoc
{
    class Program
    {      
        /// <summary>
        /// SharePoint online site connect in c#
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine("SharePoint Online site URL:");
            string webSPOUrl = Console.ReadLine();
            Console.WriteLine("User Name:");
            string userName = Console.ReadLine();
            Console.WriteLine("Password:");
            SecureString password = FetchPasswordFromConsole();
            try
            {
                using (var context = new ClientContext(webSPOUrl))
                {
                    context.Credentials = new SharePointOnlineCredentials(userName, password);
                    Web web = context.Web;
                    Console.WriteLine("Enter Document Library Name : ");
                    string DocumentName = Convert.ToString(Console.ReadLine());
                    Console.WriteLine("Enter your Folder Name : ");
                    string folederName = Convert.ToString(Console.ReadLine());
                    Console.WriteLine("Enter Prfefix Name : ");
                    string prefixName = Convert.ToString(Console.ReadLine());

                    List list = context.Web.Lists.GetByTitle(DocumentName);
                    context.Load(list);
                    context.Load(list.RootFolder);
                    context.Load(list.RootFolder.Folders);
                    context.Load(list.RootFolder.Files);
                    context.ExecuteQuery();
                    FolderCollection fcol = list.RootFolder.Folders;
                    var folderExists = fcol.Any(x => x.Name == folederName);
                    if (folderExists)
                    {
                        foreach (Folder f in fcol)
                        {
                            if (f.Name == folederName)
                            {
                                context.Load(f.Files);
                                context.ExecuteQuery();
                                FileCollection fileCol = f.Files;
                                foreach (File file in fileCol)
                                {
                                    context.Load(file.ListItemAllFields);
                                    context.ExecuteQuery();
                                    file.MoveTo(file.ListItemAllFields["FileDirRef"] + "/" + prefixName + file.ListItemAllFields["FileLeafRef"].ToString(), MoveOperations.Overwrite);
                                    context.ExecuteQuery();
                                    Console.ForegroundColor = ConsoleColor.White;
                                    Console.WriteLine("Rename : " + file.ListItemAllFields["FileDirRef"].ToString() + "=>" + prefixName + file.ListItemAllFields["FileLeafRef"].ToString());
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("There is no folder name inside doc !!!");
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error is: " + ex.Message + "Trace is : " + ex.StackTrace);
            }         
            Console.WriteLine("Press any key to continuee...");
            Console.Read();
        }
        private static SecureString FetchPasswordFromConsole()
        {
            string password = "";
            ConsoleKeyInfo info = Console.ReadKey(true);
            while (info.Key != ConsoleKey.Enter)
            {
                if (info.Key != ConsoleKey.Backspace)
                {
                    Console.Write("*");
                    password += info.KeyChar;
                }
                else if (info.Key == ConsoleKey.Backspace)
                {
                    if (!string.IsNullOrEmpty(password))
                    {
                        password = password.Substring(0, password.Length - 1);
                        int pos = Console.CursorLeft;
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                        Console.Write(" ");
                        Console.SetCursorPosition(pos - 1, Console.CursorTop);
                    }
                }
                info = Console.ReadKey(true);
            }
            Console.WriteLine();
            var securePassword = new SecureString();
            //Convert string to secure string 
            foreach (char c in password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }


}




No comments:

Post a Comment