Sharepoint - PowerShell and the client object model: "The collection has not been initialized"

I was able to get it working in PowerShell with the CSOM via the following code. It's basically the same as Robert Kaucher's (in PowerShell format) with a minor change towards the end.

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\SharePoint Client\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\SharePoint Client\Microsoft.SharePoint.Client.Runtime.dll"

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext("SharePointSiteURL")

$Web = $Ctx.Web

$Ctx.Load($Web)
$Ctx.ExecuteQuery()
Write-Host $Web.Title

$User = $Web.EnsureUser("domain\UserID")
$Ctx.Load($User)
$Ctx.ExecuteQuery()
Write-Host $User.Email

$RD = $Web.RoleDefinitions.GetByName("Full Control")
$Ctx.Load($RD)
$Ctx.ExecuteQuery()
Write-Host $RD.Description

$RDBC = New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($Ctx)
$Ctx.Load($RDBC)
$Ctx.ExecuteQuery()

$RDBC.Add($RD)
$ctx.Load($Web.RoleAssignments.Add($User, $RDBC))
$Web.Update()
$Ctx.ExecuteQuery()

I have been looking at your problem and it seems that the issue is the Load method on the ClientContext object. You cannot use it because it is an extension method expecting a type. PowerShell does not get along well with generics. See MSDN article here.

public void Load<T>(
    T clientObject,
    params Expression<Func<T, Object>>[] retrievals
)
where T : ClientObject

This article explains how to get the managed client side object model code to work with PowerShell. If you ask me, it is just too much work.

http://blogs.technet.com/b/heyscriptingguy/archive/2011/02/15/using-powershell-to-get-data-from-a-sharepoint-2010-list.aspx

EDIT:

Here is an image showing the $web variable and its properties... They all have that error because $web has not been loaded via $context.Load().

enter image description here

Edit 2:

I tried getting this to work with some inline C# in a PowerShell script but it just wasn't useful. If you don't have a copy of VisualStudio you can download Visual C# Express 2010 for free. Just create a new project, add the SharePoint.Client DLLs and here is the C# code to do what you want. You can make it a console app that takes params if you want.

class Program
    {
        static void Main(string[] args)
        {
            ClientContext ctx = new ClientContext("http://domain-dev/");
            Web web = ctx.Web;
            ctx.Load(web);
            ctx.ExecuteQuery();
            Console.WriteLine(web.Title);

            User user = web.EnsureUser("domain\\bdobbs");
            ctx.Load(user);
            ctx.ExecuteQuery();

            Console.WriteLine(user.Email);

            var rd = web.RoleDefinitions.GetByName("Full Control");
            ctx.Load(rd);
            ctx.ExecuteQuery();

            Console.WriteLine(rd.Description);

            RoleDefinitionBindingCollection rdbc = new RoleDefinitionBindingCollection(ctx);
            ctx.Load(rdbc);
            ctx.ExecuteQuery();

            rdbc.Add(rd);
            web.RoleAssignments.Add(user, rdbc);
            web.Update();
            ctx.ExecuteQuery();

            Console.Read();


        }
    }

Tags:

Powershell