views:

138

answers:

2

I need to replace the view form on an existing list on a sharepoint site. This has to be done in a feature, and I don't own the schema to the list, so I can't simply update the xml.

My feature adds the file to the list in the elements.xml:

  <Module Name="Replacement Form" Path="" Url="Lists/ListName">
    <File Url="ReplacementDispForm.aspx" />
  </Module>

and the feature updates the display form for the specific content type in the feature activating feature receiver:

  SPList list = web.Lists[listName];
  SPContentType type = list.ContentTypes[typeName];
  type.DisplayFormUrl = formUrl;
  type.Update();

That all works ok. The problem is when updating the feature - as the page contains a web part, upgrading etc causes the page to then have an additional web part - sharepoint simply adds the webpart definition meaning that each deploy adds +1 web part.

So I'm trying to delete the file in the feature deactivating code, but it simple errors that the file cannot be deleted:

string name = "Lists/ListName/ReplacementDispForm.aspx";
SPFile file = web.GetFile(name);
file.Delete();

the file.Delete is the line that errors with SPException 'Could not delete this folder'. I'm not sure if this is because the file isn't being added to the list correctly, or is my delete code incorrect?

A: 

Your code looks good. I recently did something similar, but instead of deleting the file in the deactivate, I created a new feature that deleted the file, then reactivated the first feature:

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPWeb web = properties.Feature.Parent as SPWeb;
    web.Files.Delete("Lists/ListName/ReplacementDispForm.aspx");
    web.Features.Add(new Guid("{E6E82DC3-8802-4332-BD98-0025E0A435F3}"), true);
}

Not sure if that makes any difference or not, but it at least it shows that you're going in the right direction.

The only other thing that stands out to me is the provision itself. Defaults might be coming into play, but you might want to explicitly add Type="Ghostable" to the File element.

Rich Bennema
A: 

You could also extract the schema.xml from the list even though you don't own it. The SPSource tool extracts the schema.xml from any list on your website.

Might not be worth it as you seem like you've already done most of the work creating the feature, etc. But next time you need to do something like that.

Hugo Migneron