Sunday, November 24, 2013

AddActivateSolutionbytes -“Upload and Activate the sandbox solution of a site collection gallery” (Signature : sitecollection url(string),wspname(string),bytes(byte[]))


Add and ActivateSoltution:


  public static bool  AddActivateSolutionbytes(string sitecollection, string WSPfilename, byte[] bytes)
        {
            bool Success = false;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    using (SPSite site = new SPSite(sitecollection))
                    {


                        //Get the Solution Gallery for the SPSite
                        SPDocumentLibrary gallery = (SPDocumentLibrary)site.GetCatalog(SPListTemplateType.SolutionCatalog);



                        //Add the WSP File. I've used a source that is an SPFile, but really it's a string and byte array
                        SPFile file = gallery.RootFolder.Files.Add(WSPfilename, bytes);
                        // Activate Solution 
                        SPUserSolution solution = site.Solutions.Add(file.Item.ID);

                        Success = true;



                    }

                });
               
            }
            catch (Exception ex)
            {

                Success = false;
                //add logger
            }
          
             return Success;
        }

No comments:

Post a Comment