Skip to main content

SharePoint site upgrade utility using C#

Hi people,
    Recently in my spare time i have developed an windows form application which does SharePoint site up gradation without using either stsadm command or powershell script..

 My windows form application looks as follows


















The functional over view:


  1. On click on Load button (1) present at the top , it should load all the Solutions present in the solution repository in the SharePoint central administration and add all the solution names to the dropdown present below the load button(1).




private void loadSolBtn_Click(object sender, EventArgs e)
              {
                     foreach (SPSolution sol in SPFarm.Local.Solutions)
                     {
                           if (!cmbDeploy.Items.Contains(sol.Name))
                           {
                                  cmbRetract.Items.Add(sol.Name);
                           }
                     }
              }



Note:In the above code, the cmbRetractis the Name of the ComboBox.For more information, how we can get the name of FolderBrowseDialog as shown in the step 4.scroll down.

    2.Select the Solution name which is to be retracted from all the web applications on which it is deployed and finally remove the solution from the solution repository.


private void retractBtn_Click(object sender, EventArgs e)
{
string solutionName = cmbRetract.SelectedItem.ToString();
SPSolution solution = GetSolutionByName(solutionName);
if (solution != null)
{
if (solution.DeployedWebApplications != null)
{
solution.RetractLocal(solution.DeployedWebApplications);
}
ExecuteTimerJobDefinitions();
SPFarm.Local.Solutions.Remove(solutionName);
MessageBox.Show("Retracted and removed the solution  "+solutionName+" successfully");
}
}

-----------------------------------------------------------------------------------------------

private SPSolution GetSolutionByName(string solutionName)
{
SPSolutionCollection solCollection = SPFarm.Local.Solutions;
foreach (SPSolution sol in solCollection)
{
if (sol.Name.ToLower() == solutionName.ToLower())
return sol;
}

return null;
}
--------------------------------------------------------------------------------------------------
private void ExecuteTimerJobDefinitions()
{
if (SPFarm.Local.TimerService.JobDefinitions != null)
{
foreach (SPJobDefinition job in
SPFarm.Local.TimerService.JobDefinitions)
{
try
{
job.Execute(SPServer.Local.Id);
}
catch { }
}

}
}

In the above retraction code , i have used ExecuteJobDefinitions ,because retraction needs timer jobs to be run.


3.Now add the newly built .dll files of your project to the Assembly folder. Technically it is being called as Installing dll to the global assembly cache (GAC) folder.

4.Now browse the .wsp file of the solution . Now click on the button 'Browse' and browse the .wsp file  and click on 'Add Solution' button.

-Here I have attached the code for both step3 and step4


                string wspPath = string.Empty;
private void browseBtn_Click(object sender, EventArgs e)
{
BrowseWsp.ShowDialog();
wspPath = BrowseWsp.FileName;
}
SPSolution solution = null;
private void addBtn_Click(object sender, EventArgs e)
{
solution= SPFarm.Local.Solutions.Add(wspPath);
MessageBox.Show("solution " + solution.Name + " is added successfully!!");
}


In the above code, the BrowseWsp os the Name of  FolderBrowseDialog from the tool









5.Now click on "Load" button(2), to load all solutions present in the central administration solution repository. and add to the dropdown present below the loda button(2)


               private void loadSol_Click(object sender, EventArgs e)
{
foreach (SPSolution sol in SPFarm.Local.Solutions)
{
if (!CmbLoadSol.Items.Contains(sol.Name))
{
CmbLoadSol.Items.Add(sol.Name);
}
}
}

In the above code, the CmbLoadSol is the Name of the ComboBox.For more information, how we can get the name of FolderBrowseDialog as shown above.


6. Now click  on the Load button(3), to load all web  applications on which you can deploy the solution and add to the dropdown.



                private void loadBtn_Click(object sender, EventArgs e)
{
SPWebServiceCollection webservices=new SPWebServiceCollection(SPFarm.Local);
foreach(SPWebService service in webservices)
{
foreach(SPWebApplication webApp in service.WebApplications)
{
string spUrl = Utils.GetWebAppURL(webApp, SPUrlZone.Default);
cmbDeploy.Items.Add(spUrl);
}
}
}
In the above code, the cmbDeploy is the Name of the ComboBox.For more information, how we can get the name of FolderBrowseDialog as shown in step 4 above.
---------------------------------------------------------------------------------------------


                 public string GetWebAppURL(SPWebApplication oWebApp, SPUrlZone urlZone)
{
string retVal = string.Empty;
try
{
foreach (SPAlternateUrl altUrl in oWebApp.AlternateUrls)
{
if (altUrl.UrlZone == urlZone)
{
retVal = altUrl.Uri.ToString();
break;
}
}
}
catch (Exception ex)
{
throw ex;
}
return retVal;
}



7. Now select the web application url on which you want to deploy the solution and click on  deploy button

.

               private void deployBtn_Click(object sender, EventArgs e)
{
string selectedWebApp = cmbDeploy.SelectedItem.ToString();
SPSolution solName = SPFarm.Local.Solutions[CmbLoadSol.SelectedItem.ToString()];
Collection<SPWebApplication> webapps = new Collection<SPWebApplication>();
SPWebApplication webapp = SPWebApplication.Lookup(new Uri(selectedWebApp));
webapps.Add(webapp);
solName.Deploy(DateTime.Now, false, webapps, false);
ExecuteTimeJob;
MessageBox.Show("deployed successfully!!");
}




So,..Finally an utility for SharePoint Site upgradation is done!!!..


Hope this may help some one

Cheers
-Pradeepa Achar

Comments

Popular posts from this blog

How to get the SharePoint central admin url programmaticaly via C#?

I had come across a situation where i had to get the sharepoint central admin's url.. You can follow the following snippet of the code. At the begining add the namespace  using Microsoft.SharePoint.Administration; SPAdministrationWebApplication centralAdminFarmUrl =  Microsoft.SharePoint.Administration.SPAdministrationWebApplication.Local ; String  centralAdminUrl = centralAdminFarmUrl.Sites[0].Url;  This may help someone -Cheers Pradeepa Achar

Programmatically read the value from web.config file

Here is a situation where you need to keep a key value pair element in the web.config file and read them during the run time. I suggest, better keep key /value pair under <appSettings> element and read it programmatically . <appSettings>   <add key=" LicenseKey " value=" 987jKouterhsk " /> </appSettings> Assume , you have added a license key as above in the web.config file. You need the license key during run time. You can access the license key class named "ConfigurationManager". write a property  called getLicenseKey and return the corresponding license key. public string GetLicenseKey {    get{   return  ConfigurationManager.AppSettings[" LicenseKey "]; } } Now the property GetLicenseKey will have the value 987jKouterhsk. Use this value as per your requirement. -Cheers Pradeepa Achar