Skip to content

Programmatically waiting on SharePoint Solutions to deploy

September 2, 2011

I recently had the need to deploy multiple solutions via the object model, but of course had to wait for each solution to successfully deploy before moving to the next, etc.  Another requirement is that I needed a common code base that would work on either SharePoint 2007 or SharePoint 2010.  And of course, needed to work for a single server installation or a farm.

There are multiple posts out there about interfacing with and examining the running jobs on all servers via the timer service etc., but none of them seemed to work perfectly for me.

One would immediately think just to check SPSolution.Deployed and wait for it to be true before moving on to the next.

In my testing, this works great on SharePoint 2010 in my test environments, but not so well on SharePoint 2007.

For me, I found that not only did I need to check SPSolution.Deployed, but also check SPSolution.JobExists immediately afterwards.  I couldn’t just rely on SPSolution.JobExists, because at times it would be false right after adding the solution (Perhaps because the Job had yet to actually start).

So for me, it was best to first wait until SPSolution.Deployed was true, then wait for SPSolution.JobExists to be false.

This seemed like the simplest method for ensuring the solution was deployed and complete before moving on.

So here’s a bit of a code snippet to show you basically what I’m doing.  The following code assumes you have no specific web application you’re deploying to

SPSolution solution = 
  SPFarm.Local.Solutions.Add("pathtomysolution.wsp");

solution.Deploy(DateTime.Now, true, true);
bool deployed = solution.Deployed;

while (!deployed)
{
  Thread.Sleep(1000);
  deployed = solution.Deployed;
}

bool jobexists = solution.JobExists;

while (jobexists)
{
  Thread.Sleep(1000);
  jobexists = solution.JobExists;
}

 

With this, I don’t have to muck around with poking at the timer job service on every single server in the farm etc.

I’m curious how this might work in your environments and hope this helps!

– Keith

About these ads
2 Comments leave one →
  1. Jay permalink
    February 3, 2014 3:26 pm

    HI Keith, can you tell me where to “put” this colde in my solution?
    I am deploying from visual studio 2013 to the local SP dev server and getting the dreaded Error occurred in deployment step ‘Activate Features': Feature with Id ” is not installed in this farm, and cannot be added to this scope.

    I’m wondering if it’s a timing thing or if I have the scope incorrect.
    Farm solution, scoped to web and has a vaild siteURL (sub site)
    Because I can’t activate on deploy means (I think?) that I am unable to debug my code in VS.
    I’m having to deploy without activation, go to the site, activate the feature, see if the bug is fixed and, if not, go back to step 1.

Trackbacks

  1. SharePoint 2010: Recopilatorio de enlaces interesantes (XXIII)! « Pasión por la tecnología…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,621 other followers

%d bloggers like this: