Saturday, January 24, 2015

Creating new user SharePoint group using CSOM for SharePoint 2013 Office 365

We can create SharePoint user group using CSOM for SharePoint 2013 in Visual Studio by using below code.

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);

            GroupCollection grpcoll = webUrl.SiteGroups;
            GroupCreationInformation gpcreate = new GroupCreationInformation();
            gpcreate.Title = "TestUserGroup";
            gpcreate.Description = "TestGroup";
            Group newGroup = grpcoll.Add(gpcreate);
            clientContext.Load(newGroup);
            clientContext.ExecuteQuery();
            Console.WriteLine("New Group name is:" + gpcreate.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;
        }
    }
}
Creating a List View using Client Side Object Model for SharePoint 2013 Office 365

We can create a view using below code in visual studio by using CSOM while you execute the code it will ask for user id and password for your Office 365.

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);
            List list = webUrl.Lists.GetByTitle("doctest");
            ViewCollection viewcoll = list.Views;

            string[] viewFields = { "Title" };

            ViewCreationInformation creationInfo = new ViewCreationInformation();
            creationInfo.Title = "Public View";
            creationInfo.ViewFields = viewFields;
            creationInfo.RowLimit = 5;
            viewcoll.Add(creationInfo);
            clientContext.ExecuteQuery();
        }
        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;
        }
    }
}

Tuesday, January 20, 2015

Accessing first 5 items in a list using CSOM for SharePoint 2013 Online

Below code snippet shows retrieving first 5 items in a list using CSOM for SharePoint 2013 Online by usning Visual Studio.

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);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            // view fields
            string[] viewFields = { "Title" };

            //Camlquery to retrieve the items from the custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery(5, viewFields);

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();

            foreach (ListItem item in itemcoll)
            {
                Console.WriteLine(item["Title"].ToString());
            }
            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;
        }
    }
}

Creating new item in the list using CSOM for SharePoint 2013 Online and retrieving the list items

If incase we happend to create a list item using CSOM for Sharepoint 2013 Online below code can be used.

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);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            //create a new item
            ListItemCreationInformation creationinfo = new ListItemCreationInformation();
            ListItem item = list.AddItem(creationinfo);

            //Set the title value for the new item
            item["Title"] = "New Item";

            //Update the item
            item.Update();
            clientContext.Load(item);

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

            //Camlquery to retrieve the item from the custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery();

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

            clientContext.ExecuteQuery();

            //Loop through all the items
            foreach (ListItem items in itemcoll)
            {
                //Display the item title field value
                Console.Write(items["Title"].ToString() + " \n ");

            }
            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;
        }
    }
}


Retrieving list items using SharePoint 2013 CSOM Model

Below code can be used to retrieve the list items for a specific list. Below code should be compiled in visual studio.

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);

            //Get the list by Title
            List list = webUrl.Lists.GetByTitle("doctest");

            //Caml query to retrieve the items from custom list
            CamlQuery query = CamlQuery.CreateAllItemsQuery();

            //Get all the items from the list
            ListItemCollection itemcoll = list.GetItems(query);
            clientContext.Load(itemcoll);

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

            //Loop through all the items
            foreach (ListItem item in itemcoll)
            {
                //Display the item title field value
                Console.WriteLine(item["Title"].ToString());
            }
            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;
        }
    }
}
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;
        }
    }
}
Connecting SharePoint 2013 Office 365 through CSOM

In order to connect to SharePoint Office 365 through CSOM you need to have following Dlls that need to be added to visual studio project. These dlls are:

Microsoft.SharePoint.Client
Microsoft.SharePoint.Client.Runtime.dll
Microsoft.SharePoint.Client. Taxonomy.dll
Microsoft.SharePoint.Client.UserProfiles.dll

Below code can be used to create a connection to SharePoint 2013 Office 365

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)
        {           
                string webUrl = "https://abc.sharepoint.com";
                Console.WriteLine("Enter your user name (ex: john@mytenant.microsoftonline.com):");
                string userName = Console.ReadLine();
                Console.WriteLine("Enter your password.");               
                SecureString password = GetPasswordFromConsoleInput();
                using (var context = new ClientContext(webUrl))
                {
                    context.Credentials = new SharePointOnlineCredentials(userName, password);
                    context.Load(context.Web, w => w.Title);
                    context.ExecuteQuery();
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.WriteLine("Your site title is: " + context.Web.Title);
                    Console.ForegroundColor = defaultForeground;
                  
                }
           
        }
        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;
        }
    }

}