1

I am getting ServerException when running the program. Here is my code:

 ClientContext context = new ClientContext("http://myRUL");
 Principal user = context.Web.SiteUsers.GetByLoginName(@"myRealAccoutLoginIn");
 RoleDefinition readDef = context.Web.RoleDefinitions.GetByName("Approve");//"Approve" is the permission I want to give to the user

 RoleDefinitionBindingCollection roleDefCollection = new RoleDefinitionBindingCollection(context);
            roleDefCollection.Add(readDef);
 RoleAssignment newRoleAssignment = context.Web.RoleAssignments.Add(user, roleDefCollection);

 context.ExecuteQuery(); 

Here is enter image description herethe detail exception:

And I can assure you the SiteUsers exist from the website, the red circle from the below image is my sharepoint accout: enter image description here

OPK
  • 4,120
  • 6
  • 36
  • 66

1 Answers1

4

Web.SiteUsers doesn`t exist in SharePoint 2010 and is not recomended in SharePoint 2013.

Try using Web.EnsureUser("<username>") (documentation) to get the Principal.

Michael A
  • 9,480
  • 22
  • 70
  • 114
  • It worked and I accepted your answer. Another question, is it possible to give certain user permission only to certain library and folders? – OPK Apr 20 '15 at 16:28
  • It's possible. You can assign exclusive permissions to users and roles in Libraries, Folders and ListItems/Documents. – JGabrielBastos Apr 20 '15 at 16:38
  • ok. How about using `Client Object Model` and write a program to do it? – OPK Apr 20 '15 at 16:41
  • Just changing context.Web.RoleAssignments.Add to context.Web.Lists[ – JGabrielBastos Apr 20 '15 at 17:55
  • I asked a new question, please have a look and provide answers so that I can accept answer and give you reputation. http://stackoverflow.com/questions/29754692/how-to-grant-user-permission-to-certain-folders-using-client-object-model – OPK Apr 20 '15 at 17:59