Wednesday, September 23, 2020

Console Application to Create new Subsite under site collection using CSOM + SharePoint Online

 using System;

using System.Security;

using Microsoft.SharePoint.Client;


namespace ConsoleAppCreateSubsite

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter Your Username Please  ------- ");

            string userName = Console.ReadLine();

            //string userName = "girish.kumar@happiestbarik.onmicrosoft.com";

            Console.WriteLine("Enter Your Password Please  ------- ");

            SecureString password = GetPasswordOfYourSite();

            Console.WriteLine("Enter Your Site collection URL  ------- ");

            string SiteCollURL = Console.ReadLine();

            Console.WriteLine("Enter Your New Subsite URL name   ------- ");

            string newSubsiteName = Console.ReadLine();

            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();

            using (var clientContext = new

            ClientContext(SiteCollURL))

            {

                // SharePoint Online Credentials  

                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

                // Get the SharePoint web  

                WebCreationInformation creation = new WebCreationInformation();

                creation.Url = newSubsiteName; //"MyConsoleWeb";

                creation.Title = "WelcomeToConsoleWeb";

                Web newWeb = clientContext.Web.Webs.Add(creation);


                // Retrieve the new web information. 

                clientContext.Load(newWeb, w => w.Title);

                clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

            }


        }


        private static SecureString GetPasswordOfYourSite()

        {

            ConsoleKeyInfo info;

            //Get the user's password as a SecureString  

            SecureString securePassword = new SecureString();

            do

            {

                info = Console.ReadKey(true);

                if (info.Key != ConsoleKey.Enter)

                {

                    securePassword.AppendChar(info.KeyChar);

                }

            }

            while (info.Key != ConsoleKey.Enter);

            return securePassword;

        }

    }

}

No comments:

Post a Comment

Building Secure Azure AI Foundry Agents with Managed Identity

  The credential problem nobody wants to own Every team that connects an Azure AI Foundry agent to Microsoft Graph, SharePoint, Outlook, or...