Tuesday, March 19, 2013

Client Object Model using C# and PowerShell


In this article we will be seeing how to create, update and delete SharePoint 2010 lists using Client Object Model (through both C# code and PowerShell script).

Assembly: Microsoft.SharePoint.Client.dll

Microsoft.SharePoint.Client.Runtime.dll

Namespace: Microsoft.SharePoint.Client
//Creating the list using Client Object Model:
// C# code:
using System;using Microsoft.SharePoint;using Microsoft.SharePoint.Client;
namespace COM
{
    class Program    {        
        static void Main(string[] args)
        {
            string siteUrl= "http://serverName:31829/sites/Home/";
            ClientContext clientContext=new ClientContext (siteUrl);
            Web site = clientContext.Web;
            ListCreationInformation listCreationInfo = new ListCreationInformation();
            listCreationInfo.Title = "COM List";
            listCreationInfo.Description = "List created using COM";
            listCreationInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
            List list = site.Lists.Add(listCreationInfo);
            clientContext.ExecuteQuery();
            Console.WriteLine("List is created successfully");
            Console.ReadLine();
        }
    }
}
// PowerShell Script:

$siteUrl="http://serverName:31829/sites/Home/"
$clientContext= [Microsoft.SharePoint.Client.ClientContext,Microsoft.SharePoint.Client, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
[Microsoft.SharePoint.Client.Web]$site=$context.Web;
$listCreationInfo=New-Object Microsoft.SharePoint.Client.ListCreationInformation
$listCreationInfo.Title="COM List"
$listCreationInfo.Description="List created using COM"
$listCreationInfo.TemplateType=[int][Microsoft.SharePoint.Client.ListTemplateType]::DocumentLibrary;
[Microsoft.SharePoint.Client.List]$list=$site.Lists.Add($listCreationInfo);
$context.ExecuteQuery();
write-host "List is created successfully"
//Deleting the list using Client Object Model:
// C# code:
using System;using Microsoft.SharePoint;using Microsoft.SharePoint.Client;
namespace COM
{
    class Program    {        
        static void Main(string[] args)
        {
            string siteUrl= "http://serverName:31829/sites/Home/";
            ClientContext clientContext=new ClientContext (siteUrl);
            Web site = clientContext.Web;
            List list = site.Lists.GetByTitle("cl");          
            list.DeleteObject();
            clientContext.ExecuteQuery();
            Console.WriteLine("List is deleted successfully");
            Console.ReadLine();
        }
    }
}
// Powershell script:
$siteUrl="http:// serverName:31829/sites/Home/"
$listName="Updated COM List"
$clientContext= [Microsoft.SharePoint.Client.ClientContext,Microsoft.SharePoint.Client, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
[Microsoft.SharePoint.Client.Web]$site=$context.Web;
[Microsoft.SharePoint.Client.List]$list=$site.Lists.GetByTitle($listName);
$list.DeleteObject();
$context.ExecuteQuery();
//Updating the list using Client Object Model:
//C# code:
using System;using Microsoft.SharePoint;using Microsoft.SharePoint.Client;
namespace COM
{
    class Program    {        
        static void Main(string[] args)
        {
            string siteUrl= "http://serverName:31829/sites/Home/";
            ClientContext clientContext=new ClientContext (siteUrl);
            Web site = clientContext.Web;
            List list = site.Lists.GetByTitle("COM List");
            list.Title = "Updated COM List";
            list.Update();
            clientContext.ExecuteQuery();
            Console.WriteLine("List is updated successfully");
            Console.ReadLine();
        }
    }
}
// PowerShell Script:

$siteUrl="http://serverName:31829/sites/Home/"
$clientContext= [Microsoft.SharePoint.Client.ClientContext,Microsoft.SharePoint.Client, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c]
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
[Microsoft.SharePoint.Client.Web]$site=$context.Web;
[Microsoft.SharePoint.Client.List]$list=$site.Lists.GetByTitle("COM List");
$list.Title="Updated COM List"
$list.Update();
$context.ExecuteQuery();
write-host "List is updated successfully"