site permission programatically
-
domingo, 27 de mayo de 2012 14:21
Hello,
for some reason we needed to give read and contribute permissions to users for dynamically created sites using C# code. we want the site have specialized permissions so we break parent site permissions, create groups and set their permission level for the site.
although a specific user has read permission to this site, he gets access denied. When I go to parent site from sharepoint menu, and give the user read permission, then he could see the subsite.
how can i handle this programmatically?
thank you
lose yourself
Todas las respuestas
-
domingo, 27 de mayo de 2012 17:02Kindly share the code you have written so that we can help you out where things have gone wrong
Best Regards, Ashutosh | SharePoint World
-
domingo, 27 de mayo de 2012 17:29
Attaching the code below. It starts with WebProvisioned() method.
private void createGroup(SPWebEventProperties properties, String groupName, String groupDescription, String permissionLevel) { SPWeb web = properties.Web; SPUserCollection users = web.AllUsers; SPUser owner = web.SiteAdministrators[0]; SPMember member = web.SiteAdministrators[0]; SPGroupCollection groups = web.SiteGroups; groups.Add(groupName, member, owner, groupDescription); SPGroup newSPGroup = groups[groupName]; SPRoleDefinition role = web.RoleDefinitions[permissionLevel]; SPRoleAssignment roleAssignment = new SPRoleAssignment(newSPGroup); roleAssignment.RoleDefinitionBindings.Add(role); web.RoleAssignments.Add(roleAssignment); web.Update(); } // method private void AddUserToGroup(SPWebEventProperties properties, String groupName, String userName) { SPWeb web = properties.Web; web.Groups[groupName].AddUser(userName, null, null, null); } public override void WebProvisioned(SPWebEventProperties properties) { properties.Web.Title += String.Format("[Created By: {0}]", properties.UserDisplayName); properties.Web.AllowUnsafeUpdates = true; properties.Web.Update(); if (properties.Web.WebTemplateId == 2) { using (SPWeb web = properties.Web) { try { web.RoleDefinitions.BreakInheritance(false, false); web.Update(); createGroup(properties, "grpMoMSiteReaders_" + web.Name, "Readers group", "Read"); createGroup(properties, "grpMoMSiteAuthors_" + web.Name, "Authors group", "Contribute"); AddUserToGroup(properties, "grpMoMSiteReaders_" + web.Name, "WIN-TMSIGIC3N2P\\asilter"); } catch (Exception exp) { CimtasLoggingService.LogError(CimtasLoggingService.CIMTAS_ERROR, exp.Message); } } } }lose yourself
-
lunes, 28 de mayo de 2012 6:58Moderador
Hi asilter,
If we want the site to not inherit the permissions from the parent site, we need to provide the parameter "UniquePermissions" while creating the site. Otherwise, we will drop into the Access Denied scenario you mentioned.
Here is a PowelShell script to create a site, which provides the UniquePermissions. The WebProvisioned works in this scenario:
New-SPWeb -name "site name" -URL "site URL" -template "template" -UniquePermissionsThanks,
Jinchun ChenJinchun Chen
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)- Marcado como respuesta asilter lunes, 28 de mayo de 2012 11:24
-
lunes, 28 de mayo de 2012 7:57
Here is a PowelShell script to create a site, which provides the UniquePermissions. The WebProvisioned works in this scenario:
New-SPWeb -name "site name" -URL "site URL" -template "template" -UniquePermissions
Is it possible to make the site have unique permissions with c# code. Could you revise my code?
lose yourself
-
lunes, 28 de mayo de 2012 8:00Moderador
Hi asilter,
The code you posted is fine. Could you please post the code you are using to create site?
If you are using SPWebCollection.Add method to create the site, please set the parameter "useUniquePermissions" to be True.
Thanks,
Jinchun ChenJinchun Chen
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @) -
lunes, 28 de mayo de 2012 8:15
I added
// obselete
web.HasUniquePerm = true;above
web.RoleDefinitions.BreakInheritance(false, false);
line. It worked for the site. is obselete line going to be a problem?
lose yourself
- Editado asilter lunes, 28 de mayo de 2012 8:16
-
lunes, 28 de mayo de 2012 8:42Moderador
Hi asilter,
Based on my testing, we should provide the uniquepermission while creating the site.
Thanks,
Jinchun ChenJinchun Chen
Forum Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @) -
lunes, 28 de mayo de 2012 11:25Thanks Jinchun, I marked your answer as the answer.
lose yourself

