views:

115

answers:

3

Hi all:

Following this post which I posted some time ago, I now get the same error every time I try to rewire 2 web's URLs.

Basically, this is the code. It runs in a LongRunningOperationJob:

SPWeb existingWeb = null;
using (existingWeb = site.OpenWeb(wedId))
{
    SPWeb destinationWeb = createNewSite(existingWeb);
    existingWeb.AllowUnsafeUpdates = true;
    existingWeb.Name = existingWeb.Name + "_old";
    existingWeb.Title = existingWeb.Title + "_old";
    existingWeb.Description = existingWeb.Description + "_old";

    existingWeb.Update()
    existingWeb.AllowUnsafeUpdates = false;

    destinationWeb.AllowUnsafeUpdates = true;
    destinationWeb.Name = existingWeb.Name;
    destinationWeb.Title = existingWeb.Title;
    destinationWeb.Description = existingWeb.Description;

    destinationWeb.Update();
    destinationWeb.AllowUnsafeUpdates = false;

    // null this for what its worth
    existingWeb = null;
    destinationWeb = null;
} // <---- Exception raised here

Basically, the code is trying to rename the existing site's URL to something else, and have the destination web's url point to the old site's URL.

When I run this for the first time, I received the Exception mentioned in the subject.

However, every run after, I do not see the exception anymore.

The webs DO get rewired... but at the cost of the app dying an unnecessary and terrible death.

I'm at a complete lost as to what is going on and needs urgent help. Does sharepoint keep any hidden table from me or is the logic above has fatal problems?

Thanks.

A: 

My wild-ass-guess is that, since you're setting the destinationWeb to null, at the end of the using block it tries to dispose of an object that isn't there and blows up. Remove that statement and try.

zincorp
Unfortunately that guess fell short...
BeraCim
A: 

This might not be related to your error, but I see two things with this code:

  1. destinationWeb is not being disposed
  2. based on statement order, I would expect both sites to end up being suffixed "_old"

I would rewrite the code this way:

SPWeb existingWeb = null; 
using (existingWeb = site.OpenWeb(wedId)) 
{ 
    using (SPWeb destinationWeb = createNewSite(existingWeb))
    {
        destinationWeb.AllowUnsafeUpdates = true; 
        destinationWeb.Name = existingWeb.Name; 
        destinationWeb.Title = existingWeb.Title; 
        destinationWeb.Description = existingWeb.Description; 

        existingWeb.AllowUnsafeUpdates = true; 
        existingWeb.Name += "_old"; 
        existingWeb.Title += "_old"; 
        existingWeb.Description += "_old"; 

        existingWeb.Update() 
        existingWeb.AllowUnsafeUpdates = false; 

        destinationWeb.Update(); 
        destinationWeb.AllowUnsafeUpdates = false; 
    }
}
Rich Bennema
@Rich Bennema: yeah I was being lazy in omitting some of the code in here. The sites' name, title and descriptions were set properly. But the destinationWeb not being disposed of is interesting. I drilled it down to the point where if I do not use the "using" keyword in my post, the error disappeared. I'm guessing that some objects are still being referenced in existingWeb at the end of the using block (possibly a web part). Asides that theory, I still have no ideas.
BeraCim
A: 

In the end, I replaced the using block with try catch finally block, and nullify the references in finally. That exception has never bothered me again.

Thanks.

BeraCim