Posted By

digiteyes on 08/26/10


Tagged

deployment feature sharepoint


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

digiteyes
Tyster
cybermax
jlaws0n


Overwriting Files in SharePoint using Module Element


 / Published in: C#
 

URL: http://vivek-soni.blogspot.com/2009/07/overwriting-files-using-module-element.html

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml.Linq;
  4. using System.Text;
  5. using Microsoft.SharePoint;
  6. using Microsoft.SharePoint.Administration;
  7. using System.Globalization;
  8. using System.Xml;
  9. using System.Collections;
  10. using System.Linq;
  11.  
  12. namespace SampleSharePointSolutions
  13. {
  14. public class UpdateFiles : SPFeatureReceiver
  15. {
  16. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  17. {
  18. if (properties != null)
  19. {
  20. using (SPSite currentSite = (SPSite)properties.Feature.Parent)
  21. {
  22. using (var web = currentSite.OpenWeb())
  23. {
  24. var ElementDefinitions = properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture);
  25.  
  26. foreach (SPElementDefinition ElementDefinition in ElementDefinitions)
  27. {
  28. if (ElementDefinition.ElementType == "Module")
  29. {
  30. Helper.UpdateFilesInModule(ElementDefinition, web);
  31. }
  32. }
  33. }
  34.  
  35. }
  36. }
  37. }
  38.  
  39. public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
  40. {
  41. //// throw new NotImplementedException();
  42. }
  43.  
  44. public override void FeatureInstalled(SPFeatureReceiverProperties properties)
  45. {
  46. ////throw new NotImplementedException();
  47. }
  48.  
  49. public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
  50. {
  51. //// throw new NotImplementedException();
  52. }
  53. }
  54.  
  55. internal static class Helper
  56. {
  57. internal static void UpdateFilesInModule(SPElementDefinition elementDefinition, SPWeb web)
  58. {
  59. XElement xml = elementDefinition.XmlDefinition.ToXElement();
  60. XNamespace xmlns = "http://schemas.microsoft.com/sharepoint/";
  61. string featureDir = elementDefinition.FeatureDefinition.RootDirectory;
  62. Module module = (from m in xml.DescendantsAndSelf()
  63. select new Module
  64. {
  65. ProvisioningUrl = m.Attribute("Url").Value,
  66. PhysicalPath = Path.Combine(featureDir, m.Attribute("Path").Value),
  67. Files = (from f in m.Elements(xmlns.GetName("File"))
  68. select new Module.File
  69. {
  70. Name = f.Attribute("Url").Value,
  71. Properties = (from p in f.Elements(xmlns.GetName("Property"))
  72. select p).ToDictionary(
  73. n => n.Attribute("Name").Value,
  74. v => v.Attribute("Value").Value)
  75. }).ToArray()
  76. }).First();
  77.  
  78. if (module == null)
  79. {
  80. return;
  81. }
  82.  
  83. foreach (Module.File file in module.Files)
  84. {
  85. string physicalPath = Path.Combine(module.PhysicalPath, file.Name);
  86. string virtualPath = string.Concat(web.Url, "/", module.ProvisioningUrl, "/", file.Name);
  87.  
  88. if (File.Exists(physicalPath))
  89. {
  90. using (StreamReader sreader = new StreamReader(physicalPath))
  91. {
  92. if (!CheckOutStatus(web.GetFile(virtualPath)))
  93. {
  94. web.GetFile(virtualPath).CheckOut();
  95. }
  96. SPFile spFile = web.Files.Add(virtualPath, sreader.BaseStream, new Hashtable(file.Properties), true);
  97. spFile.CheckIn("Updated", SPCheckinType.MajorCheckIn);
  98. if (CheckContentApproval(spFile.Item))
  99. {
  100. spFile.Approve("Updated");
  101. }
  102.  
  103. spFile.Update();
  104. }
  105. }
  106. }
  107.  
  108. }
  109.  
  110. private static bool CheckOutStatus(SPFile file)
  111. {
  112. if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None)
  113. {
  114. return true;
  115. }
  116. else
  117. {
  118. return false;
  119. }
  120. }
  121.  
  122. private static bool CheckContentApproval(SPListItem listitem)
  123. {
  124. bool isContentApprovalEnabled = listitem.ParentList.EnableModeration;
  125.  
  126. return isContentApprovalEnabled;
  127. }
  128.  
  129. public static XElement ToXElement(this XmlNode node)
  130. {
  131. XDocument xDoc = new XDocument();
  132.  
  133. using (XmlWriter xmlWriter = xDoc.CreateWriter())
  134.  
  135. node.WriteTo(xmlWriter);
  136.  
  137. return xDoc.Root;
  138.  
  139. }
  140. }
  141.  
  142. public class Module
  143. {
  144. public string ProvisioningUrl { get; set; }
  145. public string PhysicalPath { get; set; }
  146. public Module.File[] Files { get; set; }
  147.  
  148. public class File
  149. {
  150. public string Name { get; set; }
  151. public Dictionary<string, string> Properties { get; set; }
  152. }
  153. }
  154. }

Report this snippet  

You need to login to post a comment.