use below code to check provided list existed or not
SPList list = currWeb.Lists.TryGetList("SampleList");
if(list!=null)
{
//some code
}
SPList list = currWeb.Lists.TryGetList("SampleList");
if(list!=null)
{
//some code
}
//Main
FileStream stream = null;
string UploadFileName = @"C:\sample.wsp";
FileInfo fileInfo = new FileInfo(UploadFileName);
//Verifying the condition whether that file is existed (or) not
if (!fileInfo.Exists) { throw new InvalidOperationException("The file does not exist."); }
byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.Name);
//If file existed converting it in to the stream to transfer
stream = File.OpenRead(UploadFileName);
stream.Position = 0;
//usage
AddSolutionWorker("sample.wsp",bytes)
//Method
public void AddSolutionWorker(string wspName, byte[] wspcontents) { string tdir = Path.GetDirectoryName(Path.GetTempFileName()); string wsppath = string.Format(@"{0}\{1}", tdir, wspName); if (File.Exists(wsppath)) File.Delete(wsppath); File.WriteAllBytes(wsppath, wspcontents); SPFarm.Local.Solutions.Add(wsppath); File.Delete(wsppath); }
public List<Solution> GetSolutions() { List<Solution> solutions = new List<Solution>(); foreach (SPSolution sol in SPFarm.Local.Solutions) { Solution s = new Solution(); s.Name = sol.Name; s.Deployed = sol.Deployed; solutions.Add(s); } return solutions ; }
FileStream stream = null;
string
sitecollectionUrl = "http://samplesitecollection:port";
string
UploadFileName = @"C:\HelloWorldSolution.wsp";
FileInfo
fileInfo = new FileInfo(UploadFileName);
//Verifying the condition whether that
file is existed (or) not
if (!fileInfo.Exists) { throw new InvalidOperationException("The file does not exist."); }
byte[] bytes = System.IO.File.ReadAllBytes(fileInfo.Name);
//If file existed converting it in to
the stream to transfer
stream
= File.OpenRead(UploadFileName);
stream.Position = 0;
//checking if the solution is uploaded
in sitecollection solution gallery
bool
SolutionExits = CheckSolutionExits(sitecollectionUrl, fileInfo.Name);
if
(SolutionExits != true)
{
//add solution & activate
bool SuccessDeployed =
AddActivateSolutionbytes(sitecollectionUrl, fileInfo.Name, bytes);
}
if
(SolutionExits == true)
{
//remove solution
bool
RemoveSuccess = RemoveSolution(sitecollectionUrl, fileInfo.Name);
}
|
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
}
|
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;
}
|
public
static
bool
CheckSolutionExits(string sitecollection, string wspname)
{
#region Solution exits
bool Found
= false;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using
(SPSite
site = new SPSite(sitecollection))
{
SPList
solGallery = site.GetCatalog(SPListTemplateType.SolutionCatalog);
foreach
(SPListItem item in solGallery.Items)
{
if
(item.File.Name.Equals(wspname))
{
Found = true;
break;
}
}
}
});
}
catch
(Exception
ex)
{
throw ex;
}
#endregion
return
Found;
}
|