/ Published in: C#
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
using System; using System.Collections.Generic; using System.Xml.Linq; using System.Text; using Microsoft.SharePoint; using Microsoft.SharePoint.Administration; using System.Globalization; using System.Xml; using System.Collections; using System.Linq; namespace SampleSharePointSolutions { public class UpdateFiles : SPFeatureReceiver { public override void FeatureActivated(SPFeatureReceiverProperties properties) { if (properties != null) { using (SPSite currentSite = (SPSite)properties.Feature.Parent) { using (var web = currentSite.OpenWeb()) { var ElementDefinitions = properties.Definition.GetElementDefinitions(CultureInfo.CurrentCulture); foreach (SPElementDefinition ElementDefinition in ElementDefinitions) { if (ElementDefinition.ElementType == "Module") { Helper.UpdateFilesInModule(ElementDefinition, web); } } } } } } public override void FeatureDeactivating(SPFeatureReceiverProperties properties) { //// throw new NotImplementedException(); } public override void FeatureInstalled(SPFeatureReceiverProperties properties) { ////throw new NotImplementedException(); } public override void FeatureUninstalling(SPFeatureReceiverProperties properties) { //// throw new NotImplementedException(); } } internal static class Helper { internal static void UpdateFilesInModule(SPElementDefinition elementDefinition, SPWeb web) { XElement xml = elementDefinition.XmlDefinition.ToXElement(); XNamespace xmlns = "http://schemas.microsoft.com/sharepoint/"; string featureDir = elementDefinition.FeatureDefinition.RootDirectory; Module module = (from m in xml.DescendantsAndSelf() select new Module { ProvisioningUrl = m.Attribute("Url").Value, PhysicalPath = Path.Combine(featureDir, m.Attribute("Path").Value), Files = (from f in m.Elements(xmlns.GetName("File")) { Name = f.Attribute("Url").Value, Properties = (from p in f.Elements(xmlns.GetName("Property")) select p).ToDictionary( n => n.Attribute("Name").Value, v => v.Attribute("Value").Value) }).ToArray() }).First(); if (module == null) { return; } foreach (Module.File file in module.Files) { string physicalPath = Path.Combine(module.PhysicalPath, file.Name); string virtualPath = string.Concat(web.Url, "/", module.ProvisioningUrl, "/", file.Name); if (File.Exists(physicalPath)) { { if (!CheckOutStatus(web.GetFile(virtualPath))) { web.GetFile(virtualPath).CheckOut(); } SPFile spFile = web.Files.Add(virtualPath, sreader.BaseStream, new Hashtable(file.Properties), true); spFile.CheckIn("Updated", SPCheckinType.MajorCheckIn); if (CheckContentApproval(spFile.Item)) { spFile.Approve("Updated"); } spFile.Update(); } } } } private static bool CheckOutStatus(SPFile file) { if (file.CheckOutStatus != SPFile.SPCheckOutStatus.None) { return true; } else { return false; } } private static bool CheckContentApproval(SPListItem listitem) { bool isContentApprovalEnabled = listitem.ParentList.EnableModeration; return isContentApprovalEnabled; } public static XElement ToXElement(this XmlNode node) { using (XmlWriter xmlWriter = xDoc.CreateWriter()) node.WriteTo(xmlWriter); return xDoc.Root; } } public class Module { public string ProvisioningUrl { get; set; } public string PhysicalPath { get; set; } public Module.File[] Files { get; set; } public class File { public string Name { get; set; } public Dictionary<string, string> Properties { get; set; } } } }
URL: http://vivek-soni.blogspot.com/2009/07/overwriting-files-using-module-element.html