I would like to use c# to remotely deploy my sharepoint site collections and sites, I can easily write the code to perform most operations.

However I can't seem to find a way of deploying the code to sitecollection level rather than farm level.

Leaving my laptop at work I don't have the exact code but it goes like this so far ->

Create sitecollection add a sitecollection (http://server/managed-wildcard-path/sitecollection-name to it open up the solution DeployLocal to sitecollection.

The solution is found and DeployLocal runs without errors, however the solution isn't deployed not even on the route.

Does anyone know how to go about this? If I have the right idea but possibly the wrong idea of how it works, please also inform me.


Here is the code:

    // Attempt to add the solution to the solution store
    SPSolution solution = new SPSolution();
    solution = SPFarm.Local.Solutions.Add(wspUrl);
catch (Exception ex)
    OutPutMessage("--------------------\r\n" + wspUrl + ": This solution already exists in the solution store");
    // Create a new sitecollection collection and add the target site too it
    Collection<SPWebApplication> webapps = new Collection<SPWebApplication>();
    SPWebApplication webapp = SPWebApplication.Lookup(new Uri(siteUrl));

    // Open the target wsp
    SPSolution solutionToDeploy = new SPSolution();
    txtConsole.Text += "Solution name: \"" + SPFarm.Local.Solutions[strSolutionName].DisplayName + "\"\r\n";
    solutionToDeploy = SPFarm.Local.Solutions[strSolutionName];

    // and deploy
    solutionToDeploy.DeployLocal(true, webapps, true);
    OutPutMessage("----------------\r\nSolution Deployed\r\n----------------";

I believe that this line:


is actually trying to add the solution to the farm. You want to add it the SPSite's solution gallery, and a think your use of DeployLocal is an error - I think it's for the actual deployment of the files.

If site is your site collection try:

SPSite site = ...
SPFile sourceSolutionFile = ...
//Get the Solution Gallery for the SPSite
SPDocumentLibrary solutionGallery = (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 solutionFile = solutionGallery.RootFolder.Files.Add(sourceSolutionFile.File.Name, sourceSolutionFile.File.OpenBinary());
// Activate Solution     
SPUserSolution newUserSolution = newSite.Solutions.Add(solutionFile.Item.ID);

Certainly, that works for deploying Site Templates into the solution gallery by code for me.

  • Thank you Andy I get most of the differences and as to why they would work, however I am not sure how to get the wsp solution into the SPFile object. I haven't used this part before and it is a totally new ball park for me so sorry for the slowness. Can you give me an example of how to open up a local file ie. c:\windows\temp\tempwsp.wsp up with SPFile to be the filesource?
    – Hugh Wood
    Aug 9 '12 at 11:50
  • I have got this working, not seeing the part about a string and a byte array since I was at work, I just merely inputted the file name and used File.ReadAllBytes.. Great it found it started to add it and apparently it tries to add it as a sandbox solution, my solution isn't a sandbox and cannot be, so I need a method that works on non sandbox solutions.
    – Hugh Wood
    Aug 9 '12 at 21:08

It appears that this method only works on Sandbox solutions.

So I went back to basics and ran STSADM from inside a form application that also had the other functionality to set up the site collections built in.

So in a similar fashion to wait_pid() in c++ I looked it up for c# and used the solution presented here: https://stackoverflow.com/questions/611094/async-process-start-and-wait-for-it-to-finish then read the output using a similar method to the one found here https://stackoverflow.com/questions/6597800/capture-output-of-process-synchronously-i-e-when-it-happens, then checking the Gallery to see if it was in correctly, then deployed etc etc, basically following the exact method from the output in visual studio.

Not a perfect solution but it will do for now, if anyone has any ideas please feel free to shout.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.