Tag Archives: Feature

Remove SharePoint feature witch name starts with… using powershell

This is how to search for features which display name starts with the given parameter (in this case it would list features which started with MyFeature).

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") }

EDIT: You can also find features using GUID instead with the following script:

Get-SPFeature | where { $_.Id.ToString().StartsWith("MyGUID") }

This will remove all those features which are found using the previous script.

Get-SPFeature | where { $_.DisplayName.StartsWith("MyFeature") } | foreach { Uninstall-SPFeature $_.Id -confirm:0 -force }

Sometimes (especially in SharePoint 2013), retracting wsps aren’t done properly (by SharePoint that is), and you might get errors when you are trying to reinstall a retracted wsp. After you have retracted a wsp, if the features in that wsp can still be found by Get-SPFeature in PowerShell, and/or you get an error message installing the wsp that “A feature with ID <guid> has already been installed in this farm.”, you might have to manually remove some of the resources yourself. These steps often work for me.

  1. Retract and remove wsp from server.
  2. Run powershell script above to check if the features of the wsp are still installed.
  3. If there are still features installet, delete them from the SPFarm manuall with the following command:
    $feats = (Get-SPFarm).FeatureDefinitions
    $f = $feats | where {$_.DisplayName.Contains("Feature name")} 
    $f.Delete()
    

    This only works with single results, so make sure you get only one feature in variable $f Before deleting.

  4. Remove feature folders from the hive (for example C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\FEATURES”
  5. Remove any references in the assembly. In SP2010 (and earlier perhaps), the assembly is located at C:\Windows\assembly, and for SP2013, you can find it at C:\Windows\Microsoft.NET\assembly\GAC_MSIL.
  6. Restart the SharePoint Timer Service (Found in Services)
  7. Restart IIS (cmd “iisreset /noforce”)
Advertisements

Creating SharePoint groups programmatically in feature event receiver

The following snippet shows an example of how to add SharePoint groups to a site and set their permission levels. The code is run by adding it to an event receiver for a feature, inside the overridden FeatureActivated method.

 

 public class MBL_PermissionsEventReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties.Feature.Parent is SPWeb)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite site = new SPSite(((SPWeb)properties.Feature.Parent).Url))
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            web.AllowUnsafeUpdates = true;

                            if (!web.HasUniqueRoleAssignments)
                            {
                                web.BreakRoleInheritance(false);
                            }

                            web.Update();

                            SPUserCollection users = web.AllUsers;
                            SPUser member = users[web.Site.RootWeb.SiteAdministrators[0].LoginName];
                            SPMember owner = web.SiteGroups["Owner of " + web.Title];
                            SPRoleAssignment roleAss = null;
                            SPGroup group = null;
                            SPRoleDefinitionCollection roles = web.RoleDefinitions;
                            SPRoleAssignmentCollection roleAssignments = web.RoleAssignments;
                            SPRoleDefinitionBindingCollection roleDefBindings = null;

                            web.SiteGroups.Add("AdminGroup", owner, member, "");

                            web.Update();

                            SPMember ownerSubGroups = web.SiteGroups["AdminGroup"];
                            
                            web.SiteGroups.Add("group1", ownerSubGroups, member, "");
                            web.SiteGroups.Add("group2", ownerSubGroups, member, "");
                            web.Update();

                            group = web.SiteGroups["Owner of " + web.Title];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Full Control"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["AdminGroup"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Full Control"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["group1"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Contribute"]);
                            roleAssignments.Add(roleAss);

                            web.Update();

                            group = web.SiteGroups["group2"];
                            roleAss = new SPRoleAssignment(group);
                            roleDefBindings = roleAss.RoleDefinitionBindings;
                            roleDefBindings.Add(roles["Read"]);
                            roleAssignments.Add(roleAss);

                            web.Update();
                        }
                    }
                });
            }
        }
    }