replace highlights
This commit is contained in:
@@ -14,18 +14,18 @@ Posts like this one [http://sharepoint.stackexchange.com/questions/41358/delete-
|
||||
|
||||
Here was my specific case. In my FeatureDeactivating method I was setting all the SPWebs back to the original v4.master like this:
|
||||
|
||||
{% highlight csharp %}
|
||||
```csharp
|
||||
foreach (SPWeb site in siteColl.AllWebs)
|
||||
{
|
||||
site.MasterUrl = masterUrl;
|
||||
site.CustomMasterUrl = masterUrl;
|
||||
site.Update();
|
||||
}
|
||||
{% endhighlight %}
|
||||
```
|
||||
|
||||
Notice how I am looping through the SPSite.AllWebs SPWebCollection and updating each SPWeb after I reset the master page URLs. Now, here is what I was doing to try to delete the master page file from the _catalogs/masterpage library:
|
||||
|
||||
{% highlight csharp %}
|
||||
```csharp
|
||||
string fileUrl = SPUrlUtility.CombineUrl(
|
||||
siteColl.ServerRelativeUrl,
|
||||
file.FullRelativeUrl);
|
||||
@@ -39,21 +39,21 @@ try
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
{% endhighlight %}
|
||||
```
|
||||
|
||||
When I would get to the spFile.Delete() line, it would catch the exception whose error message is the title of this post. But why?? The master page wasn’t being referenced anywhere anymore! As I would discover, the problem is on this line:
|
||||
|
||||
{% highlight csharp %}
|
||||
```csharp
|
||||
SPFile spFile = siteColl.RootWeb.GetFile(fileUrl);
|
||||
{% endhighlight %}
|
||||
```
|
||||
|
||||
Do you see it? I didn’t at first either. The RootWeb SPWeb reference isn’t pointing to the same object that the same SPWeb in the SPSite.AllWebs collection is pointing to. So, technically, the RootWeb object still thinks its master page URLs are pointing to my custom master that I’m trying to delete because it hasn’t had Update() called on it.
|
||||
|
||||
To fix it, I had to get my SPFile from one of the SPWebs in SPSite.AllWebs, like this:
|
||||
|
||||
{% highlight csharp %}
|
||||
```csharp
|
||||
SPFile spFile = siteColl.AllWebs.First().GetFile(fileUrl);
|
||||
{% endhighlight %}
|
||||
```
|
||||
|
||||
That enables me to delete the file with no errors.
|
||||
|
||||
@@ -67,7 +67,7 @@ When iterating through SPWebs dispose of items with the using statement. This wi
|
||||
|
||||
So it might look like this:
|
||||
|
||||
{% highlight csharp %}
|
||||
```csharp
|
||||
using(SPSite siteColl = properties.Feature.Parent as SPSite)
|
||||
{
|
||||
foreach (SPWeb site in siteColl.AllWebs)
|
||||
@@ -75,4 +75,4 @@ using(SPSite siteColl = properties.Feature.Parent as SPSite)
|
||||
site.MasterUrl = masterUrl; site.CustomMasterUrl = masterUrl; site.Update();
|
||||
}
|
||||
}
|
||||
{% endhighlight %}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user