mardi 10 février 2015

Doc Library folder subfolder permissions assign existing groups using Client Object Model



I have a doc library, inside the library i have folder and subfolders. I need to know how to assign an existing group (I know the name of the group) to a folder and subfolder.


-DocLibBizUnit


--FolderToronto


----Folder1


----Folder2


--FolderCalgary


----Folder10


----Folder20


What I need to do is set folder level permission using Client Object Model. I want to create groups as follows: ContributeToronto, ReadToronto ContributeFolder1, ReadFolder1 ContributeFodler2, REadFolder2 etc.


For FolderToronto, Only two groups will be assigned, all other permissions will be removed. The two groups will be ContributeToronto and ReadToronto. For subfolder Foler1, there will be two groups ContributeFolder1, ReadFolder1 and the parent's groups also. i.e.ContributeToronto, ReadToronto. so in total 4 security groups for each subfolder.


Below is my code.



string siteUrl = "http://mySPsite:20811/sites/docTS";

ClientContext clientContext = new ClientContext(siteUrl);
Site collSite = clientContext.Site;
Web site = clientContext.Web;


//Code for creating one Read Only Group
GroupCreationInformation groupCreationInfo = new GroupCreationInformation();
groupCreationInfo.Title = "ReadToronto";
groupCreationInfo.Description = "Custom Group";

User owner = clientContext.Web.EnsureUser(@"Domain\SSZ");
User member = clientContext.Web.EnsureUser(@"Domain\SSZ");

Group group = clientContext.Web.SiteGroups.Add(groupCreationInfo );
group.Owner = owner;
group.Users.AddUser(member);
group.Update();

RoleDefinition rdRead = collSite.RootWeb.RoleDefinitions.GetByName("Read");
RoleDefinitionBindingCollection rdCollRead = new RoleDefinitionBindingCollection(clientContext);
rdCollRead.Add(rdRead);
clientContext.Web.RoleAssignments.Add(group, rdCollRead);

clientContext.ExecuteQuery();


I need to assign this ReadToronto group to the FolderToronto and Folder1 (subfolder). But can't seem to find a working code. Any ideas?


I have tried this code below (which does not address subfolders anyway) but this does not work.



var list = site.Lists.GetByTitle("DocLibBizUnit");


CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>1000</RowLimit></View>";


ListItemCollection items = list.GetItems(query);
clientContext.Web.Context.Load(items);
clientContext.Web.Context.ExecuteQuery();

foreach (ListItem item in items)
{
if (item.FileSystemObjectType == FileSystemObjectType.Folder)
{
string title;
title = item.FieldValues["FileLeafRef"].ToString();

if (title == "FolderToronto")
{

item.BreakRoleInheritance(false, true);
clientContext.ExecuteQuery();



RoleAssignmentCollection collRoleAssign = item.RoleAssignments;
RoleAssignment rollAssign = collRoleAssign.Add(group, rdCollRead);
clientContext.ExecuteQuery();

}
}
}


Please help, I am new to Sharepoint.








0 commentaires:

Enregistrer un commentaire