Sunday, November 24, 2013

Remove & Delete Sandbox Solution — will Deactivate and Remove the solution from the sitecollection gallery (Signature : sitecollection url(string),wspname(string))



  public static bool RemoveSolution(string sitecollection, string wspname)
        {
            bool RemoveSuccess = false;
            #region removing Solution

            try
            {

                SPSecurity.RunWithElevatedPrivileges(delegate()
                  {
                      using (SPSite site = new SPSite(sitecollection))
                      {
                          //Code to deactivate solution
                          SPUserSolutionCollection siteSolutions = site.Solutions;

                          foreach (SPUserSolution solution in siteSolutions)
                          {
                              if (solution.Name.Equals(wspname))
                              {
                                  //solution found & remove it
                                  SPUserSolution solutions = site.Solutions.Cast<SPUserSolution>().

                                  Where(s => s.Name == wspname).First();

                                  site.Solutions.Remove(solutions);
                                  break;

                              }


                          }
                          // Code to Delete solution
                          SPList solGallery = site.GetCatalog(SPListTemplateType.SolutionCatalog);
                          foreach (SPListItem item in solGallery.Items)
                          {
                              if (item.File.Name.Equals(wspname))
                              {
                                  solGallery.Items.DeleteItemById(item.ID);
                                  RemoveSuccess = true;
                                  break;
                                 
                              }
                          }

                      }
                  });
            }

            catch (Exception ex)
            {
                RemoveSuccess = false;
               // string exception = ex.Message.ToString();
            }
            return RemoveSuccess;
            #endregion
        }

No comments:

Post a Comment