With the help of answer by @Ankit Kumar, I tried the code given here. But his code is for SharePoint 2013. And the question is for SharePoint 2010.
In SharePoint 2010 var GRoup1 = cpx.Web.SiteGroups.GetByName(groupName);
will always gives error
Method "GetByName" does not exist.
So I modified the code to get the group name as shown below:
Group grp = null;
GroupCollection groupColl = clientContext.Web.SiteGroups;
clientContext.Load(groupColl,
groups => groups.Include(
group => group.Title,
group => group.Id));
clientContext.ExecuteQuery();
foreach (Group siteGroup in groupColl)
{
if (siteGroup.Title.Equals("TestGroup"))
{
grp = siteGroup;
break;
}
}
And after that using following code I was able to implement this on my SharePoint 2010 Environment.
var roletypes = clientContext.Web.RoleDefinitions.GetByType(RoleType.Contributor);
clientContext.ExecuteQuery();
listItemObject.BreakRoleInheritance(true, false);
RoleDefinitionBindingCollection collRoleDefinitionBinding = new RoleDefinitionBindingCollection(clientContext);
collRoleDefinitionBinding.Add(roletypes);
listItemObject.RoleAssignments.Add(grp, collRoleDefinitionBinding);
listItemObject.Update();
clientContext.ExecuteQuery();
Where listItemObject
is your folder context. This code worked for me at my 2010 Environment.
Let me know if this solved your problem or any further help needed.