private void TsmiGetLatestVersionClick(object sender, EventArgs e)
        {
            var selectedWr = webresourceTreeView1.SelectedResource;
            if (selectedWr.Entity == null || selectedWr.Entity.Id == Guid.Empty)
            {
                MessageBox.Show(ParentForm,
                    "This web resource has not been synchronized to CRM server yet. You cannot get latest version",
                    Resources.MessageBox_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            WorkAsync(new WorkAsyncInfo
            {
                Message = "Retrieving latest version...",
                AsyncArgument = selectedWr,
                Work = (bw, evt) =>
                {
                    var wrm = new AppCode.WebResourceManager(Service);
                    var wr = wrm.RetrieveWebResource(((WebResource)evt.Argument).Entity.Id);

                    ((WebResource)evt.Argument).Entity = wr;
                    ((WebResource)evt.Argument).InitialBase64 = wr.GetAttributeValue<string>("content");

                    evt.Result = evt.Argument;
                },
                PostWorkCallBack = evt =>
                {
                    webresourceTreeView1_WebResourceSelected(null,
                        new WebResourceSelectedEventArgs((WebResource)evt.Result));
                }
            });
        }
        private void UpdateWebResources(bool publish, IEnumerable<WebResource> webResources, bool addToSolution = false)
        {
            var solutionUniqueName = string.Empty;
            if (addToSolution)
            {
                var sPicker = new SolutionPicker(Service) { StartPosition = FormStartPosition.CenterParent };

                if (sPicker.ShowDialog(this) == DialogResult.OK)
                {
                    solutionUniqueName = sPicker.SelectedSolution["uniquename"].ToString();
                }
                else
                {
                    return;
                }
            }

            SetWorkingState(true);
            var parameters = new object[] { webResources, publish, solutionUniqueName };

            WorkAsync(new WorkAsyncInfo("Updating web resources...",
                 (bw, e) =>
                 {
                     var webResourceManager = new AppCode.WebResourceManager(Service);
                     var resourceToPublish = new List<WebResource>();
                     var resources = ((IEnumerable<WebResource>)((object[])e.Argument)[0]).ToList();

                     var wrDifferentFromServer = new List<WebResource>();

                     foreach (var wr in resources)
                     {
                         Entity serverVersion = null;
                         if (wr.Entity != null && wr.Entity.Id != Guid.Empty)
                         {
                             serverVersion = webResourceManager.RetrieveWebResource(wr.Entity.Id);
                         }

                         if (serverVersion != null && serverVersion.GetAttributeValue<string>("content") != wr.InitialBase64)
                         {
                             wrDifferentFromServer.Add(wr);
                         }
                         else
                         {
                             bw.ReportProgress(1, string.Format("Updating {0}...", wr.Entity.GetAttributeValue<string>("name")));

                             wr.Entity.Id = webResourceManager.UpdateWebResource(wr.Entity);
                             resourceToPublish.Add(wr);
                             wr.InitialBase64 = wr.Entity.GetAttributeValue<string>("content");
                         }
                     }

                     if (wrDifferentFromServer.Count > 0)
                     {
                         if (
                             CommonDelegates.DisplayMessageBox(null,
                                 string.Format(
                                     "The following web resources were updated on the server by someone else:\r\n{0}\r\n\r\nAre you sure you want to update them with your content?",
                                     String.Join("\r\n", wrDifferentFromServer.Select(r => r.Entity.GetAttributeValue<string>("name")))),
                                 "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                         {
                             foreach (var resource in wrDifferentFromServer)
                             {
                                 bw.ReportProgress(1, string.Format("Updating {0}...", resource.Entity.GetAttributeValue<string>("name")));

                                 resource.Entity.Id = webResourceManager.UpdateWebResource(resource.Entity);
                                 resourceToPublish.Add(resource);
                                 resource.InitialBase64 = resource.Entity.GetAttributeValue<string>("content");
                             }
                         }
                     }

                     // Process post Update command
                     if (!string.IsNullOrEmpty(Options.Instance.AfterUpdateCommand))
                     {
                         foreach (var webResource in resourceToPublish)
                         {
                             evtManager.ActAfterUpdate(webResource);
                         }
                     }

                     // if publish
                     if ((bool)((object[])e.Argument)[1] && wrDifferentFromServer.Count <= resources.Count())
                     {
                         bw.ReportProgress(2, "Publishing web resources...");

                         webResourceManager.PublishWebResources(resourceToPublish);
                     }

                     // Process post Publish command
                     if (!string.IsNullOrEmpty(Options.Instance.AfterPublishCommand))
                     {
                         foreach (var webResource in resourceToPublish)
                         {
                             evtManager.ActAfterPublish(webResource);
                         }
                     }

                     if (((object[])e.Argument)[2].ToString().Length > 0 && wrDifferentFromServer.Count < resources.Count())
                     {
                         bw.ReportProgress(3, "Adding web resources to solution...");

                         webResourceManager.AddToSolution(resourceToPublish, ((object[])e.Argument)[2].ToString());
                     }
                 })
            {
                AsyncArgument = parameters,
                PostWorkCallBack = e =>
                {
                    if (e.Error != null)
                    {
                        MessageBox.Show(this, e.Error.Message, Resources.MessageBox_ErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    if (tslResourceName.Text.Contains(" (not published)"))
                    {
                        tslResourceName.Text = tslResourceName.Text.Replace(" (not published)", "");
                        tslResourceName.ForeColor = Color.Black;
                    }

                    SetWorkingState(false);
                },
                ProgressChanged = e => SetWorkingMessage(e.UserState.ToString())
            });
        }
        private void DeleteWebResource()
        {
            try
            {
                if (TreeViewHelper.CheckOnlyThisNode(webresourceTreeView1))
                    return;

                TreeNode selectedNode = webresourceTreeView1.SelectedNode;

                if (selectedNode.ImageIndex > 1)
                {
                    if (DialogResult.Yes == MessageBox.Show(this,
                                                            "This web resource will be deleted from the Crm server if you are connected and this web resource exists.\r\nAre you sure you want to delete this web resource?",
                                                            Resources.MessageBox_QuestionTitle,
                                                            MessageBoxButtons.YesNo,
                                                            MessageBoxIcon.Question))
                    {
                        var wr = selectedNode.Tag as WebResource;

                        if (wr != null && wr.Entity != null && wr.Entity.Id != Guid.Empty)
                        {
                            webresourceTreeView1.Service = Service;

                            wrManager = new AppCode.WebResourceManager(Service);

                            WorkAsync(new WorkAsyncInfo("Deleting web resource...", e => wrManager.DeleteWebResource((Entity)e.Argument))
                            {
                                AsyncArgument = wr.Entity,
                                PostWorkCallBack = e =>
                                {
                                    if (e.Error != null)
                                    {
                                        MessageBox.Show(this, "An error occured: " + e.Error, Resources.MessageBox_ErrorTitle,
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    else
                                    {
                                        webresourceTreeView1.RemoveNode(selectedNode);
                                    }

                                    SetWorkingState(false);
                                }
                            });
                        }
                        else
                        {
                            webresourceTreeView1.RemoveNode(selectedNode);
                        }
                    }
                }
                else
                {
                    webresourceTreeView1.RemoveNode(selectedNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "Error while deleting web resource: " + error.Message, Resources.MessageBox_ErrorTitle,
                             MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void TsmiFindUnusedWebResourcesClick(object sender, EventArgs e)
        {
            var allresources = webresourceTreeView1.GetAllResources();
            WorkAsync(new WorkAsyncInfo("Starting analysis...", (bw, evt) =>
            {
                var resources = (List<WebResource>)evt.Argument;

                var unusedWebResources = new List<Entity>();
                int i = 1;
                foreach (var resource in resources)
                {
                    var wr = resource.Entity;

                    bw.ReportProgress((i * 100) / resources.Count, "Analyzing web resource " + wr["name"] + "...");

                    wrManager = new AppCode.WebResourceManager(Service);
                    if (!wrManager.HasDependencies(wr.Id))
                    {
                        unusedWebResources.Add(wr);
                    }
                    i++;
                }

                evt.Result = unusedWebResources;
            })
            {
                AsyncArgument = allresources,
                ProgressChanged = evt => SetWorkingMessage(string.Format("{0}% - {1}", evt.ProgressPercentage, evt.UserState)),
                PostWorkCallBack = evt =>
                {
                    var dialog = new UnusedWebResourcesListDialog((List<Entity>)evt.Result, Service);
                    dialog.ShowInTaskbar = true;
                    dialog.StartPosition = FormStartPosition.CenterParent;
                    dialog.ShowDialog(this);
                }
            });
        }