2

I have found examples on how to add users to SharePoint Groups using Client Object Model. Can anyone help provide an example on how to grant permissions to items in a List using an existing SharePoint group? I dont need to create the group in code, nor add users to the group. I also do not need to add roles to the SharePoint Group. I simply need to grant permissions to the List Item using the existing SharePoint Group.

Thanks for any tips provided.

5

You should create RoleDefinitionBinding object set the type of permissions to it and add it as parameter to RoleAssignments of list item. There is example of how to add group with read permissions to list item:

using System;
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;

namespace Microsoft.SDK.SharePointServices.Samples
{
  class BreakSecurityInheritanceAddUser
  {
    static void Main()
    {
      string siteUrl = "Your site url";
      ClientContext clientContext = new ClientContext(siteUrl);
      SP.List oList = clientContext.Web.Lists.GetByTitle("Your List Name");

      int itemId = 3;
      ListItem oListItem = oList.Items.GetById(itemId); //Get List Item by id

      oListItem.BreakRoleInheritance(false);



RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);

  collRoleDefinitionBinding.Add(clientContext.Web.RoleDefinitions.GetByType(RoleType.Reader)); //Set permission type

  oListItem.RoleAssignments.Add(oGroup, collRoleDefinitionBinding); //oGroup - your SPGroup

  clientContext.ExecuteQuery();
}
  }
}
  • Thank you. What would I pass in place of the "oGroup" parameter? Let's say my SharePoint Group name is "TestGroup". The group already has permissions assigned (for example, CONTRIBUTE). Do I still need to set the permission type? – obautista Dec 22 '13 at 18:06
  • 1
    "oGroup" is an object type "Group": GroupCollection groups = web.SiteGroups; clientContext.Load(groups, groupitems => groupitems.Include(groupitem => groupitem.Title, groupitem => groupitem.Description).Where(groupitem=> groupitem.Title == "TestGroup")); Permission is for list item. What did you mean by that group has contribute permission? Your group has permission for site? For list? For item? – Andrey Ungur Dec 22 '13 at 18:59
  • Sorry, that was a bad example. "TestGroup" has READ permissions. And it would be granting permissions to the list item, so granting "TestGroup" to the list item in the list. – obautista Dec 22 '13 at 23:08
0

check this blog, he is using the visual studio via COM http://model-view-controller.net/2012/09/20/set-permission-to-group-in-sharepoint-2010-programmatically-using-vs-2010-via-com/

also check this MSDN for complete How to http://msdn.microsoft.com/en-us/library/ee537013(v=office.14).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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