Thursday, 26 April 2012

Argh! I Locked Myself Out!

But I didn't get back in by being given a key - more like kicking the door down, but anyway...

A few days ago I was testing something on the dev machine and removed a few user groups from the site permissions list on a site collection. They were still associated with the site, just had no site permissions. I was logged in as site admin. Went to do something on one of those sites and what do you know, I can't get in! Checked the site collection admin, where I was queen of everything and all looked well. Checked Central Admin and the Sharepoint:80 web application, found my errant site collection, and yes I was site admin. Still couldn't get in.

Here is how I got back in. I do not recommend using this as a method to get in. It is a horrible hack and will consign me to SharePoint Hell. I only did it because it was a dev machine and the outcome was not important. However - it is an example of how to dynamically add an existing SharePoint user group onto a Site Permissions list if it isn't on it already:

using (SPSite site = new SPSite("http://notmyemployerssitecoll/sc/sample/"))
     using (SPWeb web = site.OpenWeb())
         foreach (SPGroup group in web.AssociatedGroups)
             if (group.Name.Contains("My Little Lost Group"))
                 //assign it permissions to control everything
                 //obviously when we do this for real, we want it to grab the
                 //existing role assignment
                 SPRoleAssignment roleAssignment = newSPRoleAssignment(web.SiteGroups[group.Name]);
                 SPRoleDefinitionBindingCollection roleDefinition = roleAssignment.RoleDefinitionBindings;
                 roleDefinition.Add(web.RoleDefinitions["Full Control"]);
                 web.Properties[group.Name] = "Full Control";

No comments:

Post a Comment