Thursday 28 July 2016

Creating and Updating List in SharePoint Online Through CSOM

Friends, here is quick code snippet to create and update lists in SharePoint Online in CSOM from a Console Application.

Reference to Microsoft.SharePoint.Client.dll to be added.
Replace user name, password and site url with actuals from the code.
Code:
 
 
using Microsoft.SharePoint.Client;
using System;
using System.Security;
namespace TestConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "USERNAME";
            string password = "PASSWORD";
            string siteUrl = "SITEURL";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new SharePointOnlineCredentials(userName, GetPassword(password));
            Web web = clientContext.Web;
            ListCreationInformation listCrInfo = new ListCreationInformation();
            listCrInfo.Title = "NewAnnouncements";
            listCrInfo.TemplateType = (int)ListTemplateType.Announcements;
            List list = web.Lists.Add(listCrInfo);
            list.Description = "New Announcements Description";
            list.Update();
            clientContext.ExecuteQuery();
        }
        private static SecureString GetPassword(string password)
        {
            if (password == null)
                throw new ArgumentNullException("password");
            var securePassword = new SecureString();
            foreach (char c in password)
                securePassword.AppendChar(c);
            securePassword.MakeReadOnly();
            return securePassword;
        }
    }
}

No comments:

Post a Comment