Tuesday, January 20, 2015

Creating a list using CSOM in SharePoint Office 365

Below code can be used to create a list in SharePoint Office 365 using CSOM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;
using System.Security;
namespace GetListCollection
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext clientContext = new ClientContext("https://abc.sharepoint.com");
            Web webUrl=clientContext.Web;
            Console.WriteLine("Enter your user name (ex: abc@microsoft.microsoftonline.com):");
            string userName = Console.ReadLine();
            Console.WriteLine("Enter your password.");
            SecureString password = GetPasswordFromConsoleInput();
            clientContext.Credentials = new SharePointOnlineCredentials(userName, password);
         
            //specifying properties of new custom list
            ListCreationInformation creationinfo=new ListCreationInformation();
            creationinfo.Title="Custom CSOM List";
            creationinfo.Description="Created using VS CSOM";
            creationinfo.TemplateType=(int)ListTemplateType.GenericList;

            //create a new custom list
            List newlist = clientContext.Web.Lists.Add(creationinfo);

            //Retrieve the custom list properties
            clientContext.Load(newlist);

            //executing the query to server
            clientContext.ExecuteQuery();

            //Display the properties of the list
            Console.WriteLine(newlist.Title);
            Console.ReadLine();
         
         
        }
         
     
        private static SecureString GetPasswordFromConsoleInput()
        {
            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