public void ActAfterUpdate(WebResource resource) { if (options.AfterUpdateCommand.Contains("{FilePath}") && string.IsNullOrEmpty(resource.FilePath)) { throw new Exception("It is required that the web resource has a file path in its properties to use a command referencing the tag {FilePath}"); } RunCommand(options.AfterUpdateCommand.Replace("{FilePath}", resource.FilePath)); }
internal static void CreateEmptyWebResource(string extension, TreeView tv, Control mainControl) { var callerNode = tv.SelectedNode; var nwrDialog = new NewWebResourceDialog(extension) { StartPosition = FormStartPosition.CenterParent }; if (nwrDialog.ShowDialog(mainControl) == DialogResult.OK) { var tempNode = callerNode; string name = callerNode.Text; while (tempNode.Parent != null) { name = string.Format("{0}/{1}", tempNode.Parent.Text, name); tempNode = tempNode.Parent; } var webResource = new Entity("webresource"); webResource["content"] = ""; webResource["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(extension)); webResource["name"] = string.Format("{0}/{1}", name, string.Format("{0}.{1}", nwrDialog.WebResourceName, extension)); var wr = new WebResource(webResource, null); var parts = nwrDialog.WebResourceName.Split('/'); for (int i = 0; i < parts.Length; i++) { if (i != parts.Length - 1) { var folderNode = new TreeNode(parts[i]) { ImageIndex = 1, SelectedImageIndex = 1 }; callerNode.Nodes.Add(folderNode); callerNode.Expand(); callerNode = folderNode; } else { var node = new TreeNode(string.Format("{0}.{1}", parts[i], extension)) { ImageIndex = WebResource.GetImageIndexFromExtension (extension) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = wr; callerNode.Nodes.Add(node); callerNode.Expand(); } } } }
/// <summary> /// Initializes a new instance of class UpdateForm /// </summary> /// <param name="script">Script to display or to create</param> /// <param name="service">Xrm Organization Service</param> public UpdateForm(WebResource script, IOrganizationService service) { InitializeComponent(); innerService = service; currentWebResource = script; FillControls(); }
/// <summary> /// Updates the provided web resource /// </summary> /// <param name="wr">Web resource to update</param> internal void UpdateWebResource(WebResource wr) { try { if (wr.Id == Guid.Empty) { Entity existingEntity = RetrieveWebResource(wr.ToString()); if (existingEntity == null) { wr.Create(innerService); } else { wr.Id = existingEntity.Id; if (existingEntity.Contains("displayname") && string.IsNullOrEmpty(wr.EntityDisplayName)) { wr.EntityDisplayName = existingEntity.GetAttributeValue <string>("displayname"); } if (existingEntity.Contains("description") && string.IsNullOrEmpty(wr.EntityDescription)) { wr.EntityDescription = existingEntity.GetAttributeValue <string>("description"); } if (existingEntity.Contains("dependencyxml") && string.IsNullOrEmpty(wr.EntityDependencyXml)) { wr.EntityDependencyXml = existingEntity.GetAttributeValue <string>("dependencyxml"); } if (existingEntity.Contains("languagecode") && wr.EntityLanguageCode == 0) { wr.EntityLanguageCode = existingEntity.GetAttributeValue <int>("languagecode"); } wr.Update(innerService); } } else { wr.Update(innerService); } } catch (Exception error) { throw new Exception("Error while updating web resource: " + error.Message); } }
public static void UpdateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad) { var subNodes = parentFolderNode.Nodes; var subFolders = di.GetDirectories(); foreach (var subFolder in subFolders) { if (!subNodes.ContainsKey(subFolder.Name) || subNodes[subFolder.Name].ImageIndex != 1) { var folderNode = new TreeNode(subFolder.Name) { ImageIndex = 1, SelectedImageIndex = 1, Tag = subFolder.FullName, Name = subFolder.Name }; parentFolderNode.Nodes.Add(folderNode); UpdateFolderStructure(folderNode, subFolder, invalidFilenames, extensionsToLoad); } else { UpdateFolderStructure(subNodes[subFolder.Name], subFolder, invalidFilenames, extensionsToLoad); } } foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { if (WebResource.IsInvalidName(fiChild.Name) || !WebResource.ValidExtensions.Contains(fiChild.Extension.Remove(0, 1).ToLower())) { invalidFilenames.Add(fiChild.FullName); continue; } if (extensionsToLoad == null || extensionsToLoad.Contains(fiChild.Extension)) { if (!subNodes.ContainsKey(fiChild.Name) || subNodes[fiChild.Name].ImageIndex <= 1) { CreateWebResourceNode(fiChild, parentFolderNode); } else { var wr = (WebResource)subNodes[fiChild.Name].Tag; wr.WebResourceEntity["content"] = Convert.ToBase64String(File.ReadAllBytes(wr.FilePath)); } } } }
/// <summary> /// Updates the provided web resource /// </summary> /// <param name="script">Web resource to update</param> internal void UpdateWebResource(WebResource wr) { try { var script = wr.Entity; if (!script.Contains("webresourceid")) { Entity existingEntity = RetrieveWebResource(script["name"].ToString()); if (existingEntity == null) { script.Id = CreateWebResource(script); } else { script.Id = existingEntity.Id; if (!script.Contains("displayname") && existingEntity.Contains("displayname")) { script["displayname"] = existingEntity["displayname"]; } if (!script.Contains("description") && existingEntity.Contains("description")) { script["description"] = existingEntity["description"]; } innerService.Update(script); } } else { innerService.Update(script); } wr.SetAsUpdated(); } catch (Exception error) { throw new Exception("Error while updating web resource: " + error.Message); } }
/// <summary> /// Create a TreeView folder structure depending on the DirectoryInfo /// element in parameters /// </summary> /// <param name="parentFolderNode">Current TreeNode</param> /// <param name="di">Current physical directory info</param> /// <param name="invalidFilenames"></param> /// <param name="inValidWrNameRegex"></param> public static void CreateFolderStructure(TreeNode parentFolderNode, DirectoryInfo di, List <string> invalidFilenames, List <string> extensionsToLoad) { foreach (DirectoryInfo diChild in di.GetDirectories()) { if (WebResource.IsInvalidName(diChild.Name)) { invalidFilenames.Add(diChild.FullName); continue; } // If the current physical directory has sub directories or // javascript file, a new TreeNode has to be created if (diChild.GetDirectories().Length > 0 || diChild.GetFiles("*.*").Length > 0) { var folderNode = new TreeNode(diChild.Name) { ImageIndex = 1, SelectedImageIndex = 1 }; parentFolderNode.Nodes.Add(folderNode); CreateFolderStructure(folderNode, diChild, invalidFilenames, extensionsToLoad); } } foreach (FileInfo fiChild in di.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { if (WebResource.IsInvalidName(fiChild.Name) || !WebResource.ValidExtensions.Contains(fiChild.Extension.Remove(0, 1).ToLower())) { invalidFilenames.Add(fiChild.FullName); continue; } if (extensionsToLoad.Contains(fiChild.Extension)) { // Create a TreeNode for each javascript file CreateWebResourceNode(fiChild, parentFolderNode); } } }
internal static void AddExistingWebResource(TreeView tv, Control mainControl) { TreeNode selectedNode = tv.SelectedNode; TreeNode tempNode = selectedNode; string name = tempNode.Text; while (tempNode.Parent != null) { name = string.Format("{0}/{1}", tempNode.Parent.Text, name); tempNode = tempNode.Parent; } var ofd = new OpenFileDialog { Multiselect = true, Title = "Select file(s) for web resource(s)" }; if (ofd.ShowDialog(mainControl) == DialogResult.OK) { var errorList = new List<string>(); foreach (string fileName in ofd.FileNames) { var fi = new FileInfo(fileName); //Test valid characters if (WebResource.IsInvalidName(fi.Name)) { errorList.Add(fileName); } else { var webResource = new Entity("webresource"); webResource["content"] = Convert.ToBase64String(File.ReadAllBytes(fileName)); webResource["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))); webResource["name"] = string.Format("{0}/{1}", name, fi.Name); webResource["displayname"] = string.Format("{0}/{1}", name, fi.Name); var wr = new WebResource(webResource, fileName); var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = wr; selectedNode.Nodes.Add(node); selectedNode.Expand(); } if (errorList.Count > 0) { MessageBox.Show("Some file have not been added since their name does not match naming policy\r\n" + string.Join("\r\n", errorList)); } } } tv.TreeViewNodeSorter = new NodeSorter(); tv.Sort(); }
/// <summary> /// Create a TreeNode for the javascript file passed in parameter /// </summary> /// <param name="fiChild">Javascript FileInfo</param> /// <param name="parent">Parent element (TreeView or TreeNode)</param> public static void CreateWebResourceNode(FileInfo fiChild, object parent) { string scriptName = fiChild.Name; string fileName = scriptName; // Creating the script "name" attribute which depends on the current // TreeView structure var treeNode = parent as TreeNode; if (treeNode != null) { var parentNode = treeNode; while (parentNode != null && !parentNode.Text.EndsWith("_")) { fileName = string.Format("{0}/{1}", parentNode.Text, fileName); parentNode = parentNode.Parent; } if (parentNode != null) fileName = string.Format("{0}/{1}", parentNode.Text, fileName); } int imageIndex = 0; switch (fiChild.Extension.ToLower().Remove(0, 1)) { case "html": case "htm": imageIndex = 2; break; case "css": imageIndex = 3; break; case "js": imageIndex = 4; break; case "xml": imageIndex = 5; break; case "png": imageIndex = 6; break; case "jpg": imageIndex = 7; break; case "gif": imageIndex = 8; break; case "xap": imageIndex = 9; break; case "xsl": imageIndex = 10; break; case "ico": imageIndex = 11; break; } // Create new virtual web resource var script = new Entity("webresource"); script["name"] = fileName; script["webresourcetype"] = new OptionSetValue(imageIndex - 1); // Add content script["content"] = Convert.ToBase64String(File.ReadAllBytes(fiChild.FullName)); // Generate display name (Credit to badhabits) var lastSlash = fileName.LastIndexOf("/", StringComparison.Ordinal); var displayName = lastSlash > -1 ? fileName.Substring(lastSlash + 1) : fileName; script["displayname"] = displayName; var scriptObject = new WebResource(script, fiChild.FullName); var node = new TreeNode { Text = scriptName, Name = scriptName, Tag = scriptObject, ImageIndex = imageIndex, SelectedImageIndex = imageIndex }; var node1 = parent as TreeNode; if (node1 != null) { node1.Nodes.Add(node); } else { ((TreeView)parent).Nodes.Add(node); } }
internal static void AddExistingWebResource(TreeView tv, Control mainControl) { TreeNode selectedNode = tv.SelectedNode; TreeNode tempNode = selectedNode; string name = tempNode.Text; while (tempNode.Parent != null) { name = string.Format("{0}/{1}", tempNode.Parent.Text, name); tempNode = tempNode.Parent; } var ofd = new OpenFileDialog { Multiselect = true, Title = "Select file(s) for web resource(s)" }; if (ofd.ShowDialog(mainControl) == DialogResult.OK) { var errorList = new List <string>(); foreach (string fileName in ofd.FileNames) { var fi = new FileInfo(fileName); //Test valid characters if (WebResource.IsInvalidName(fi.Name)) { errorList.Add(fileName); } else { var webResource = new Entity("webresource"); webResource["content"] = Convert.ToBase64String(File.ReadAllBytes(fileName)); webResource["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))); webResource["name"] = string.Format("{0}/{1}", name, fi.Name); webResource["displayname"] = string.Format("{0}/{1}", name, fi.Name); var wr = new WebResource(webResource, fileName); var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = wr; selectedNode.Nodes.Add(node); selectedNode.Expand(); } if (errorList.Count > 0) { MessageBox.Show("Some file have not been added since their name does not match naming policy\r\n" + string.Join("\r\n", errorList)); } } } tv.TreeViewNodeSorter = new NodeSorter(); tv.Sort(); }
/// <summary> /// Create a TreeNode for the javascript file passed in parameter /// </summary> /// <param name="fiChild">Javascript FileInfo</param> /// <param name="parent">Parent element (TreeView or TreeNode)</param> public static void CreateWebResourceNode(FileInfo fiChild, object parent) { string scriptName = fiChild.Name; string fileName = scriptName; // Creating the script "name" attribute which depends on the current // TreeView structure var treeNode = parent as TreeNode; if (treeNode != null) { var parentNode = treeNode; while (parentNode != null && !parentNode.Text.EndsWith("_")) { fileName = string.Format("{0}/{1}", parentNode.Text, fileName); parentNode = parentNode.Parent; } if (parentNode != null) { fileName = string.Format("{0}/{1}", parentNode.Text, fileName); } } int imageIndex = 0; switch (fiChild.Extension.ToLower().Remove(0, 1)) { case "html": case "htm": imageIndex = 2; break; case "css": imageIndex = 3; break; case "js": imageIndex = 4; break; case "png": imageIndex = 6; break; case "jpg": imageIndex = 7; break; case "gif": imageIndex = 8; break; case "xap": imageIndex = 9; break; case "xsl": imageIndex = 10; break; case "ico": imageIndex = 11; break; } // Create new virtual web resource var script = new Entity("webresource"); script["name"] = fileName; script["webresourcetype"] = new OptionSetValue(imageIndex - 1); // Add content script["content"] = Convert.ToBase64String(File.ReadAllBytes(fiChild.FullName)); // Generate display name (Credit to badhabits) var lastSlash = fileName.LastIndexOf("/", StringComparison.Ordinal); var displayName = lastSlash > -1 ? fileName.Substring(lastSlash + 1) : fileName; script["displayname"] = displayName; var scriptObject = new WebResource(script, fiChild.FullName); var node = new TreeNode { Text = scriptName, Tag = scriptObject, ImageIndex = imageIndex, SelectedImageIndex = imageIndex }; var node1 = parent as TreeNode; if (node1 != null) { node1.Nodes.Add(node); } else { ((TreeView)parent).Nodes.Add(node); } }
private void tv_DragDrop(object sender, DragEventArgs e) { var errorList = new List<string>(); // Retrieve the current selected node var treeview = (TreeView)sender; var location = tv.PointToScreen(Point.Empty); var currentNode = treeview.GetNodeAt(e.X - location.X, e.Y - location.Y); var files = (string[])e.Data.GetData(DataFormats.FileDrop); foreach (var file in files) { var fi = new FileInfo(file); string nodeObjectName = GetName(currentNode); // Test valid characters if (WebResource.IsNameInvalid(fi.Name)) { errorList.Add(file); } else { // Create CRM web resource var webResource = new Entity("webresource") { ["content"] = Convert.ToBase64String(File.ReadAllBytes(file)), ["webresourcetype"] = new OptionSetValue(WebResource.GetTypeFromExtension(fi.Extension.Remove(0, 1))), ["name"] = string.Format("{0}/{1}", nodeObjectName, fi.Name), ["displayname"] = string.Format("{0}/{1}", nodeObjectName, fi.Name) }; var newWebResource = new WebResource(webResource, fi.FullName); // Create file if the current node has a filepath in its tag // this means, wen resources come from disk if (currentNode.Tag != null && currentNode.Tag is string && Directory.Exists(currentNode.Tag.ToString())) { var resultingFileName = Path.Combine(currentNode.Tag.ToString(), fi.Name); if (resultingFileName.ToLower() != fi.FullName.ToLower()) { if (DialogResult.Yes == MessageBox.Show( "Would you like to also copy this file to folder '" + currentNode.Tag + "'?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) { File.WriteAllBytes(resultingFileName, File.ReadAllBytes(file)); } } } var node = new TreeNode(fi.Name) { ImageIndex = WebResource.GetImageIndexFromExtension(fi.Extension.Remove(0, 1)) }; node.SelectedImageIndex = node.ImageIndex; node.Tag = newWebResource; newWebResource.Node = node; currentNode.Nodes.Add(node); currentNode.Expand(); } } if (errorList.Any()) { MessageBox.Show(ParentForm, Resources.WebresourceTreeView_InvalidFileNameWarningMessage + string.Join("\r\n", errorList), Resources.MessageBox_WarningTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Add a node in the current parent node in the treeview /// </summary> /// <param name="nameParts">Web resource name parts</param> /// <param name="index">Index of the part to use</param> /// <param name="parent">Parent item (TreeView or TreeNode)</param> /// <param name="webResource">Web resource content</param> private void AddNode(string[] nameParts, int index, object parent, WebResource webResource) { if (index == 0) { var treeview = (TreeView)parent; // If the current root for the web resource exists if (treeview.Nodes.Find(nameParts[index], false).Length == 0) { var node = new TreeNode(nameParts[index]); node.Name = node.Text; // If the name part is the last one, it is the web resource // so it is stored in the Tag property for later usage if (index == nameParts.Length - 1) { node.Tag = webResource; int imageIndex = webResource.Entity.GetAttributeValue<OptionSetValue>("webresourcetype").Value + 1; node.ImageIndex = imageIndex; node.SelectedImageIndex = imageIndex; } else { // 0 is image index for Root image node.ImageIndex = 0; node.SelectedImageIndex = 0; } treeview.Nodes.Add(node); AddNode(nameParts, index + 1, node, webResource); } else { AddNode(nameParts, index + 1, treeview.Nodes.Find(nameParts[index], false)[0], webResource); } } else if (index < nameParts.Length) { var treenode = (TreeNode)parent; if (treenode.Nodes.Find(nameParts[index], false).Length == 0) { var node = new TreeNode(nameParts[index]); node.Name = node.Text; // If the name part is the last one, it is the web resource // so it is stored in the Tag property for later usage if (index == nameParts.Length - 1) { node.Tag = webResource; int imageIndex = webResource.Entity.GetAttributeValue<OptionSetValue>("webresourcetype").Value + 1; node.ImageIndex = imageIndex; node.SelectedImageIndex = imageIndex; webResource.Node = node; } else { if (index == 0) { node.ImageIndex = 0; node.SelectedImageIndex = 0; } else { // 0 is image index for Folder image node.ImageIndex = 1; node.SelectedImageIndex = 1; } } treenode.Nodes.Add(node); AddNode(nameParts, index + 1, node, webResource); } else { AddNode(nameParts, index + 1, treenode.Nodes.Find(nameParts[index], false)[0], webResource); } } }
public WebResourceSelectedEventArgs(WebResource webResource) { WebResource = webResource; }