1: using System;
2: using System.Collections.Generic;
3: using System.Xml;
4: using System.Text;
5: using System.Globalization;
6: using Microsoft.SharePoint;
7: using Microsoft.SharePoint.Administration;
8: using Microsoft.SharePoint.Utilities;
9:
10: namespace MB.SharePoint.FeatureReceivers
11: {
12: class RevertFilesToDefinitionFeatureReceiver : SPFeatureReceiver
13: {
14: public override void FeatureActivated(SPFeatureReceiverProperties properties)
15: {
16: try
17: {
18: SPWeb web = ((SPSite)properties.Feature.Parent).RootWeb;
19: CultureInfo ci = System.Globalization.CultureInfo.InvariantCulture;
20: SPElementDefinitionCollection elementFiles = properties.Definition.GetElementDefinitions(ci);
21:
22: foreach (SPElementDefinition elementFile in elementFiles)
23: {
24: try
25: {
26: XmlNode content = elementFile.XmlDefinition;
27: XmlDocument contentDoc = new XmlDocument();
28: contentDoc.LoadXml(content.OuterXml);
29:
30: XmlNamespaceManager xmlNamespaceManager = new XmlNamespaceManager(contentDoc.NameTable);
31: xmlNamespaceManager.AddNamespace("ns", "http://schemas.microsoft.com/sharepoint/");
32:
33:
34:
35: //check for an elementfile
36: if (content.Name == "Module")
37: {
38: XmlNode xmlModule = elementFile.XmlDefinition;
39: SPFolder spFolder = web.GetFolder("/" + xmlModule.Attributes["Url"].Value);
40: SPDocumentLibrary spDocumentLibrary = (SPDocumentLibrary)web.Lists[spFolder.ContainingDocumentLibrary];
41:
42: foreach (XmlNode xmlFile in xmlModule.SelectNodes("ns:File", xmlNamespaceManager))
43: {
44: SPFile spFile = web.GetFile(String.Concat(xmlModule.Attributes["Url"].Value, "/", xmlFile.Attributes["Url"].Value));
45: if (spFile != null && spFile.CustomizedPageStatus == SPCustomizedPageStatus.Customized)
46: spFile.RevertContentStream();
47: }
48: }
49: }
50: catch (Exception ex)
51: {
52: //absorbing error (so that other files can try), you'll want to log here
53: }
54: }
55: }
56: catch (Exception ex)
57: {
58: //absorbing error, you'll want to log here
59: }
60: }
61:
62: public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
63: {
64: }
65:
66: public override void FeatureInstalled(SPFeatureReceiverProperties properties)
67: {
68: }
69:
70: public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
71: {
72: }
73:
74: }
75: }