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?