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"

Tuesday, January 22, 2013

Authentication in SharePoint 2010 Classic Mode Vs Claims Based


Classic Mode:

This is nothing but Windows Authentication. If any web application is created with Classic Mode Authentication then it cannot applicable to configure forms based authentication. But, to configure this application to use Forms Based Authentication then you can convert from Classic Mode to Claims Based. 

But, there is no UI exist for doing this conversion. The only way around is through PowerShell.

$app = get-spwebapplication "URL"
$app.useclaimsauthentication = "True"
$app.Update()
But, if an application is created using Claims based and if you want to convert from Claims based to Classic Mode then it is not possible either through UI or through PowerShell. So, remember this.

Claims Based:

In SharePoint 2010 for a web application we can enable both windows, forms authentication. In earlier implementation to do this, we have to create two web applications which has different zones and different authentication. But, with the new claims based authentication a single application can have the capability to configure both windows and forms under single URL. All this is possible because of the authentication framework is built on Microsoft Identify Foundation. And it uses “Geneva” framework to handle this authentication.