ARTICLE

Client Object Model using C# and PowerShell

Posted by Vijai Anand Articles | Windows PowerShell January 04, 2011
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).
Reader Level:

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"

erver'>
Login to add your contents and source code to this article
post comment
     

Hello, I followed your codes and developed a small win app with c# in VS 2010. I wanted to run this app on a client machine (eg: Win 7, Server 2008 or Vista). So, I tried several things... by using a setup, just running only the *.exe file, etc. But, all failed. Please let me know how exactly to do that. Many Thanks. Nalaka

Posted by Nalaka Malalagama Jan 09, 2011
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter