Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to grant a user permission to a SharePoint site using PowerShell and the client object model. The code I'm using is as follows:

# Setup context
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web=$context.Web

# New user object
$user=$web.EnsureUser("domain\username");

# Role definition
$rd = $web.RoleDefinitions.GetByName("Full Control")

# Create role definition binding collection
$rdbc=New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)
$context.ExecuteQuery();
$rdbc.Add($rd)

$context.Web.RoleAssignments.Add($user, $rdbc)

$context.ExecuteQuery();

This fails on the $context.Web.RoleAssignments.Add($user, $rdbc) line with the following exception:

The collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. At :line:0 char:0

I've tried placing additional ExecuteQuery statements around the RoleAssignments.Add call but the result is the same.

Any ideas?

share|improve this question
    
You actually get that at every line that creates a SharePoint object. All the properties of $web, etc are that way... –  Robert Kaucher Aug 23 '12 at 17:44

3 Answers 3

up vote 5 down vote accepted

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


        }
    }
share|improve this answer

You will need to ClientContext.Load() the objects which you will be modifying. Namely, I will suggest you try this following code:

# Setup context
$context=New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$web=$context.Web

//Load the $web object here.

# New user object
$user=$web.EnsureUser("domain\username");

//load the $user object here.

# Role definition
$rd = $web.RoleDefinitions.GetByName("Full Control")

# Create role definition binding collection
$rdbc=New-Object Microsoft.SharePoint.Client.RoleDefinitionBindingCollection($context)

//load the $rdbc object here.

$context.ExecuteQuery();

$rdbc.Add($rd)

$context.Web.RoleAssignments.Add($user, $rdbc)

$context.ExecuteQuery();

Here is a link which is pure client object model code to achieve what you are trying to do. Please modify the syntax for powershell as necessary. http://blogs.technet.com/b/speschka/archive/2009/11/01/using-the-sharepoint-2010-client-object-model-part-5.aspx

share|improve this answer
1  
this cannot be done in pure PowerShell. See my answer above. The load method just doesn't work in PowerShell. –  Robert Kaucher Aug 23 '12 at 19:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.