SharePoint 2013 Update Access Requests Associated Owner Group

I had a requirement to programmatically change the group that handles the access requests.  Below is the code that I used to do it.  By default the access request can only be accepted/handled by the OOTB site owners group.  To change it, you need to


  • give the SharePoint group permission to "Access Requests" list
  • update "vti-associatedownergroup" property in the root web's AllProperties.


using (SPSite site = new SPSite(siteId))
{
    using (SPWeb web = site.OpenWeb())
    {
        try
        {
            SPList list = web.Lists.TryGetList("Access Requests");
            if (list != null && !list.Permissions.Inherited)
            {
                SPGroup oGroup = web.SiteGroups.GetByName("groupName");
                if (oGroup != null)
                {
                    SPRoleDefinition role = web.RoleDefinitions["Full Control"];
                    SPRoleAssignment roleAssignment = new SPRoleAssignment(oGroup);
                    roleAssignment.RoleDefinitionBindings.Add(role);
                    list.RoleAssignments.Add(roleAssignment);
                    list.Update();
                    web.AllowUnsafeUpdates = true;

                    if (web.AllProperties.ContainsKey("vti_associateownergroup"))
                        web.AllProperties[("vti_associateownergroup"] = oGroup.ID.ToString();
                    else
                        web.AllProperties.Add(("vti_associateownergroup", oGroup.ID.ToString());

                    web.Update();
                }                            
            }
        }
        catch (Exception ex)
        {
            Logger.LogError("EventError", "ListAdded: " + ex.ToString());
        }
        finally
        {
            web.AllowUnsafeUpdates = false;
        }
    }
}

Comments

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

Add spell check dictionary in SharePoint 2010, SharePoint 2013, and Office 365