Saturday, March 16, 2013

Solved: The web being updated was changed by an external process.

Today I had to troubleshoot SharePoint site provisioning code which misbehaved on a particular server. It is always fun if code works well on a dozen SharePoint farms and then fails on one farm.

The code is a really simple C# console app which reads an xml file and it then uses the taxonomy specified in the xml file to create a SP web.

As part of the provisioning, the code will activate a set of standard SharePoint features and then activate the custom developed features.

I used the application on many different SharePoint 2010 farms to provision over 30 sites but recently we ran the tool on a new farm and we suddenly started getting the following error when the code tries to activate the SharePoint Publishing Web feature:

Error: Provisioning did not succeed.  Details: Failed to initialize some site properties for Web at Url: 'http://demo/Contoso'  OriginalException: The web being updated was changed by an external process.

clip_image001

This is the original code – before I made changes:

Console.WriteLine("Creating a new web with title: " + webTitle);

site.AllowUnsafeUpdates = true;

SPWeb newweb = site.AllWebs.Add(webTitle, webTitle, webTitle, lcid, "STS#1", false, false);

site.AllowUnsafeUpdates = false;
Console.WriteLine("Web created. Ready to activate web features.");

if (newweb != null)
{
    ActivateWebFeatures(newweb);
}
Console.WriteLine("Web created successfully");

I discovered that the SPWeb newweb = site.AllWebs.Add( call actually returns the SPWeb object before all provisioning has been completed so the next time I try to update newweb I get the error “The web being updated was changed by an external process.”

In order to solve this I fetched the new updated instance of the SPWeb, so I added the following line of code: newweb = site.OpenWeb(webTitle);

The following code works well:

Console.WriteLine("Creating a new web with title: " + webTitle);

site.AllowUnsafeUpdates = true;

SPWeb newweb = site.AllWebs.Add(webTitle, webTitle, webTitle, lcid, "STS#1", false, false);

site.AllowUnsafeUpdates = false;

Console.WriteLine("Web created. Ready to activate web features.");

newweb = site.OpenWeb(webTitle);

if (newweb != null)
{
    ActivateWebFeatures(newweb);
}

Console.WriteLine("Web created successfully");

image

8 comments:

Peri's Blog said...

Before you call a new instance of the newWeb you must dispose the old instance, else it doesnt work.

newWeb.Dispose();
newweb = site.OpenWeb(webTitle);

if (newweb != null)
{
ActivateWebFeatures(newweb);
}

Peri's Blog said...

Actually adding a delay helped too

Thread.Sleep(3000);

sandeep samuel said...

Hi John,

Nice post ,thanks for sharing this information .






Sharepoint Development

Dalton Olson said...

This is good technical detail about SharePoint codes which works in many farms but fall on particular farm, and how author with the help of this content try to solve this problem for SharePoint developers.

Anonymous said...

Your code is leaking memory, you need to change it as with best practices, it won't going to work...
https://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx

Anonymous said...

You will required to dispose this new web object by adding it within using (SPWeb addedWeb = web.Webs.Add(strWebUrl)) block.. and if you do add it to using block

Cindy Dy said...


Thank you for the information. You have a very good article. I found it informative and useful. Keep up the good work and God bless!

Yong
www.gofastek.com

Karthika Shree said...

Finding the time and actual effort to create a superb article like this is great thing. I’ll learn many new stuff right here! Good luck for the next post buddy..
Sharepoint Training in Chennai

Post a Comment