środa, 12 września 2012

Wyzerowanie (czyszczenie) witryny sharepoint

Czyli... coś czego... niemal nigdy nie powinno się robić, jednak moja wyobraźnia jest na tyle pojęta, że... jednak jestem w stanie sobie wyobrazić syt. w której trzeba programistycznie wyczyścić całą witrynę sharepoint. Można to zrobić za pomocą przedstawionej poniżej metody, którą można podpiąć pod kod deaktywujący feature.

Uwaga: ta metoda jest niczym "broń atomowa" dla witryny sharepoint, więc jej ewentualne użycie powinno być wcześniej 10-krotnie przemyślane.

     private void ClearAll(SPFeatureReceiverProperties properties)
        {

            SPWeb baseWeb = (SPWeb)properties.Feature.Parent;
            Guid siteId = baseWeb.Site.ID;
            Guid webId = baseWeb.ID;
            SPSecurity.RunWithElevatedPrivileges(
                delegate()
                {
                    using (SPSite adminSite = new SPSite(siteId))
                    {
                        try
                        {
                            SPWeb web = adminSite.AllWebs[webId];
                            if (web.Exists)
                            {
                                int listIndex = 0;
                                while (web.Lists.Count > listIndex)
                                {
                                    try
                                    {
                                        web.Lists[listIndex].Delete();
                                    }
                                    catch (Exception ex) { listIndex++; };
                                }

                                while (web.ContentTypes.Count > 0)
                                {
                                    int errorCount = 0;

                                    while (web.ContentTypes[0].FieldLinks.Count > errorCount)
                                    {
                                        try
                                        {
                                            web.ContentTypes[0].FieldLinks
                                                .Delete(web.ContentTypes[0].FieldLinks[errorCount].Id);
                                        }
                                        catch (Exception ex)
                                        {
                                            errorCount++;
                                        }
                                    }
                                    web.ContentTypes[0].Delete();
                                }
                                while (web.Fields.Count > 0)
                                {
                                    int fieldIndex = 0;
                                    while (fieldIndex < web.Fields.Count)
                                    {
                                        try
                                        {
                                            web.Fields.Delete(web.Fields[fieldIndex].InternalName);
                                        }
                                        catch (Exception ex) {
                                            fieldIndex++;
                                        };
                                    }
                                }
                                web.Update();
                                web.ResetRoleInheritance();
                                if (Helpers.checkGroup(SolutionResourcesAccessor.RMUAHRGroupName, web.SiteGroups))
                                    web.SiteGroups.Remove(SolutionResourcesAccessor.RMUAHRGroupName);
                                if (Helpers.checkGroup(SolutionResourcesAccessor.RMUAUsersGroupName, web.SiteGroups))
                                    web.SiteGroups.Remove(SolutionResourcesAccessor.RMUAUsersGroupName);
                                if (Helpers.checkGroup(SolutionResourcesAccessor.RMUAAdminGroupName, web.SiteGroups))
                                    web.SiteGroups.Remove(SolutionResourcesAccessor.RMUAAdminGroupName);
                                while (web.Navigation.QuickLaunch.Count > 0)
                                {
                                    web.Navigation.QuickLaunch.Delete(web.Navigation.QuickLaunch[0]);
                                    web.Update();
                                }
                                while (web.Navigation.TopNavigationBar.Count > 0)
                                {
                                    web.Navigation.TopNavigationBar.Delete(web.Navigation.TopNavigationBar[0]);
                                    web.Update();
                                }
                            }

                            SPLimitedWebPartManager mainLWPM =
                                web.GetLimitedWebPartManager(SPUrlUtility.CombineUrl(web.Url, "default.aspx")
                                   , System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
                            while (mainLWPM.WebParts.Count != 0)
                                mainLWPM.DeleteWebPart(mainLWPM.WebParts[0]);
                            web.ResetRoleInheritance();
                        }
                        catch (Exception ex)
                        {

                        }
                    }
                }
            );

        }

Brak komentarzy:

Prześlij komentarz