none
Need to remove permission for Visitors group in sub site programmatically

    Question

  • We have a requirement where we need to assign/remove permission for SharePoint groups at sub site level based on the case. We are unable remove permission for Site Collection Visitors in subsite. Initially when sub site was created it inherit from site collection, then it will break inheritance. I shouldn't remove permission from Site or Site collection. I just need to remove permission for visitors group from subsite. Is it possible through code.. If yes kindly provide the code sample for the same

    Thanks in Advance

    Friday, July 26, 2013 9:25 AM

Answers

  • Sorry about the confusion, I thought you need to remove permissions. Here is the updated code to remove Visitors group from the site.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                using(SPSite site = new SPSite("http://sharepoint2010"))
                {
                    RemoveGroupFromSharepointSite(site, "HOME Visitors");
                }
            }
    
            public static void RemoveGroupFromSharepointSite(SPSite site, string groupName)
            {
                using (SPWeb rootWeb = site.RootWeb)
                {
                    SPGroupCollection siteGroupColl = rootWeb.SiteGroups;
                    siteGroupColl.Remove(groupName);
                }
            }
        }
    }
    
    HTH

    Thanks, Rakesh
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you. ***Posting is provided "AS IS" with no warranties, and confers no rights.

    Saturday, July 27, 2013 12:46 PM

All replies

  • Hi,

    Please see below sample code snippet for removing specific permission level.

    private static void RemoveSpecificRoleForUser(SPUser spUser, SPRoleType roleType, SPWeb spWeb)
     {
     SPRoleAssignmentCollection roleCollection = spWeb.RoleAssignments;
     for (int i = 0; i < roleAssignmentCollection.Count; i++)
     {
     SPRoleAssignment roleAssignment = roleCollection[i];
     if (roleAssignment.Member.ID == spUser.ID)
     {
     if (roleAssignment.RoleDefinitionBindings != null)
     for (int j = 0; j < roleAssignment.RoleDefinitionBindings.Count; j++)
     {
     SPRoleDefinition roleDefinitionSingle = roleAssignment.RoleDefinitionBindings[j];
     if (roleDefinitionSingle.Type == roleType)
     {
     roleAssignment.RoleDefinitionBindings.Remove(roleDefinitionSingle);
     roleAssignment.Update();
     }
     }
     }
     }
     }
    HTH...

    Thanks, Rakesh
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you. ***Posting is provided "AS IS" with no warranties, and confers no rights.


    Friday, July 26, 2013 10:39 AM
  • I tried your code. but the visitor group is not removed in sub site.. 
    Saturday, July 27, 2013 11:05 AM
  • Sorry about the confusion, I thought you need to remove permissions. Here is the updated code to remove Visitors group from the site.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                using(SPSite site = new SPSite("http://sharepoint2010"))
                {
                    RemoveGroupFromSharepointSite(site, "HOME Visitors");
                }
            }
    
            public static void RemoveGroupFromSharepointSite(SPSite site, string groupName)
            {
                using (SPWeb rootWeb = site.RootWeb)
                {
                    SPGroupCollection siteGroupColl = rootWeb.SiteGroups;
                    siteGroupColl.Remove(groupName);
                }
            }
        }
    }
    
    HTH

    Thanks, Rakesh
    Please Mark As Answer if my post solves your problem or Vote As Helpful if a post has been helpful for you. ***Posting is provided "AS IS" with no warranties, and confers no rights.

    Saturday, July 27, 2013 12:46 PM