What else I need to add to following code to make sure the folder is hidden or read only?
SPListItem createFolder = myDocLib.Folders.Add(myDocLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "Folder444");
folder.Update();
What else I need to add to following code to make sure the folder is hidden or read only?
SPListItem createFolder = myDocLib.Folders.Add(myDocLib.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, "Folder444");
folder.Update();
Unlike actual lists, a Folder cannot be made "hidden" in the normal sense. That is, you cannot make it invisible to the UI, but still have all the normal access levels associated with it (for example, the traditional Workflow History list is hidden in this fashion). In a way, your goals of hiding and making it read-only coincide when it comes to folders, as both are accomplished by Permissions. You can technically reduce the visibility of a particular item through a complicated view filter, but that would only change things on the view level.
The visibility of a list item, which a Folder is, is based on whether you have permissions on that item. The only way to truly "hide" a list item is to remove all permissions of a person from that list item. In order to make a folder read-only, you would remove all Add, Edit, and Delete permissions from the folder. If you still have the default SharePoint permission levels (Full Control, Design, Contribute, and Read), then simply reassigning everything to Read will accomplish this goal.
Here is an example of code that I use to make a discussion (which is a folder) hidden from all users except those in the group "Engineers", to whom it is read-only.
// I excluded these two variable declarations because they were specific to
// my situation.
// Giving you my variable declarations would be misleading, since this is
// meant to be a generic code sample.
// SPListItem folder is the folder I am working on.
// SPWeb web is the SPWeb for the site I am on.
if (!folder.HasUniqueRoleAssignments)
{
folder.BreakRoleInheritance(false); //Setting false erases all permissions, so now the folder is hidden from everyone.
folder.SystemUpdate(false);
}
SPRoleDefinition role = web.RoleDefinitions["Read"];
SPPrincipal smith = web.SiteGroups["Engineers"]; //You would replace "Engineers" with the name of whatever group you want to have read permissions. Or, use web.SiteUsers[] to get a specific user.
SPRoleAssignment roleAssignment = new SPRoleAssignment(smith);
roleAssignment.RoleDefinitionBindings.Add(role);
folder.RoleAssignments.Remove(smith); //If I have already assigned a different permission, this will clear it so that I ensure that the only permission is Read.
folder.RoleAssignments.Add(roleAssignment); //This sets it to Read-Only for these people.
folder.SystemUpdate(false);
EDIT
When assigning multiple instances, it depends on whether you are doing all of the groups or just a subset. I'll list the code for all groups since that is what you are directly asking.
if (!folder.HasUniqueRoleAssignments)
{
folder.BreakRoleInheritance(false); //Setting false erases all permissions, so now the folder is hidden from everyone.
folder.SystemUpdate(false);
}
SPRoleDefinition role = web.RoleDefinitions["Read"];
SPRoleAssignment roleAssignment;
foreach (SPPrincipal smith = web.SiteGroups)
{
roleAssignment = new SPRoleAssignment(smith);
roleAssignment.RoleDefinitionBindings.Add(role);
folder.RoleAssignments.Remove(smith);
folder.RoleAssignments.Add(roleAssignment);
}
folder.SystemUpdate(false);
If you wanted to do it only for a subset of groups, you would define a list of the names of all of those groups, do a foreach (string groupName in groupList)
, and as the first line of the loop, specify SPPrincipal smith = web.SiteGroups[groupName];
.
Thank you Cornet. Here is the method that makes a folder read only. THANKS AGAIN FOR YOUR HELP...
private void MakeFolderReadOnly(SPWeb web,SPListItem folder)
{
if (!folder.HasUniqueRoleAssignments)
{
//Setting false erases all permissions, so now the folder is hidden from everyone.
folder.BreakRoleInheritance(false);
folder.SystemUpdate(false);
}
SPRoleDefinition role = web.RoleDefinitions["Read"];
SPGroupCollection collGroups = web.SiteGroups;
foreach(SPGroup group in collGroups)
{
SPPrincipal principal = group;
SPRoleAssignment roleAssign = new SPRoleAssignment(principal);
roleAssign.RoleDefinitionBindings.Add(role);
folder.RoleAssignments.Remove(principal);
folder.RoleAssignments.Add(roleAssign);
folder.SystemUpdate(false);
}
}