Beispiel #1
0
        internal static void ClickNew(DTE dte)
        {
            try
            {
                dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
                if (UtilityPackage.GetCrmServiceClient(dte))
                {
                    var crmServiceClient = (CrmServiceClient)UtilityPackage.GetGlobal("CrmServiceClient", dte);
                    var crmUrl           = (string)UtilityPackage.GetGlobal("CrmUrl", dte);

                    UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] Connected");

                    var fullFileName = dte.SelectedItems.Item(1).ProjectItem.FileNames[0];
                    var fileName     = Path.GetFileName(fullFileName);
                    var solutions    = XrmHelper.GetAllSolutions(crmServiceClient);
                    var formItems    = new FormItems(CrmItemType.NewWebResource, solutions, fullFileName, crmUrl);
                    formItems.SetWebResourceName(Utility.GetCurrentProjectDirectoryName(dte));
                    if (formItems.ShowDialog() == DialogResult.OK)
                    {
                        var solutionUniqueName = formItems.SolutionUniqueName;
                        var resourceName       = formItems.ResourceName;
                        var resourceId         = DeployNewWebResource(dte, crmServiceClient, crmUrl, fullFileName, resourceName, solutionUniqueName);
                        AddToCache(dte, fullFileName, resourceId, resourceName);
                    }
                }
                dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationDeploy);
            }
            catch
            {
                UtilityPackage.SetDTEStatusBarAndStopAnimate(dte, "Deploy WebResource failed");
            }
        }
Beispiel #2
0
        private static void PublishWebResource(DTE dte, CrmServiceClient crmServiceClient, Guid webResourceId, string crmUrl, string webResourceName, string fileName)
        {
            var publishXml = $"<importexportxml><webresources><webresource>{webResourceId}</webresource></webresources></importexportxml>";
            var request    = new PublishXmlRequest {
                ParameterXml = publishXml
            };

            crmServiceClient.Execute(request);
            UtilityPackage.SetDTEStatusBar(dte, $"Deployed: [{fileName}] to [{webResourceName}]");
        }
Beispiel #3
0
        private static void DeployWebResource(DTE dte, CrmServiceClient crmServiceClient, string crmUrl, string fullFileName, string fileName, Guid webResourceId)
        {
            var requests = new OrganizationRequestCollection();

            var webResource = new Entity("webresource")
            {
                Id = webResourceId
            };

            webResource["content"] = Convert.ToBase64String(File.ReadAllBytes(fullFileName));
            var request = new UpdateRequest {
                Target = webResource
            };

            requests.Add(request);

            var publishXml = $"<importexportxml><webresources><webresource>{webResource.Id}</webresource></webresources></importexportxml>";
            var pubRequest = new PublishXmlRequest {
                ParameterXml = publishXml
            };

            requests.Add(pubRequest);

            var multipleRequest = new ExecuteMultipleRequest
            {
                Requests = requests,
                Settings = new ExecuteMultipleSettings
                {
                    ContinueOnError = false,
                    ReturnResponses = true
                }
            };

            UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] Updating & Publishing WebResource");
            var multipleResponse = (ExecuteMultipleResponse)crmServiceClient.Execute(multipleRequest);

            foreach (var response in multipleResponse.Responses)
            {
                if (response.Fault == null)
                {
                    continue;
                }
                UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] Deploy WebResource failed");
                return;
            }
            var webResourceName = string.Empty;
            var selected        = DevKitSetting.SelectedWebResources.Where(x => x.FullFileName == fullFileName).FirstOrDefault();

            if (selected != null)
            {
                webResourceName = selected.WebResourceName;
            }
            UtilityPackage.SetDTEStatusBar(dte, $"Deployed: [{fileName}] to [{webResourceName}]");
        }
Beispiel #4
0
        private static void AddWebResourceToSolution(DTE dte, CrmServiceClient crmServiceClient, string solutionUniqueName, Guid webResourceId, string webResourceName)
        {
            var request = new AddSolutionComponentRequest
            {
                AddRequiredComponents = true,
                ComponentType         = 61,
                ComponentId           = webResourceId,
                SolutionUniqueName    = solutionUniqueName
            };

            crmServiceClient.Execute(request);
            UtilityPackage.SetDTEStatusBar(dte, $"[{webResourceName}] Added to solution: [{solutionUniqueName}]");
        }
Beispiel #5
0
        private static void DoDeployReport(DTE dte, CrmServiceClient crmServiceClient, Guid reportId, string fullFileName)
        {
            var update = new Entity("report", reportId);

            update["bodytext"] = File.ReadAllText(fullFileName);
            try
            {
                crmServiceClient.Update(update);
                UtilityPackage.SetDTEStatusBar(dte, $"Deployed: [{fullFileName}]");
            }
            catch
            {
                UtilityPackage.SetDTEStatusBar(dte, $"Deploy report failed !!!");
            }
        }
Beispiel #6
0
 internal static void Click(DTE dte)
 {
     try
     {
         dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
         if (UtilityPackage.GetCrmServiceClient(dte))
         {
             var crmServiceClient = (CrmServiceClient)UtilityPackage.GetGlobal("CrmServiceClient", dte);
             var crmUrl           = (string)UtilityPackage.GetGlobal("CrmUrl", dte);
             UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] Connected");
             var fullFileName = dte.SelectedItems.Item(1).ProjectItem.FileNames[0];
             var fileName     = Path.GetFileName(fullFileName);
             var resourceId   = GetCachedResourceId(fullFileName, dte);
             if (resourceId != Guid.Empty)
             {
                 DeployWebResource(dte, crmServiceClient, crmUrl, fullFileName, fileName, resourceId);
             }
             else
             {
                 var resources = GetResources(crmServiceClient, fullFileName);
                 if (resources.Count == 0)
                 {
                     UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] WebResource: {fileName} not found");
                 }
                 else
                 {
                     var formItems = new FormItems(CrmItemType.WebResource, resources, fullFileName, crmUrl);
                     if (formItems.ShowDialog() == DialogResult.OK)
                     {
                         resourceId = formItems.ObjectId;
                         var resourceName = formItems.ResourceName;
                         AddToCache(dte, fullFileName, resourceId, resourceName);
                         DeployWebResource(dte, crmServiceClient, crmUrl, fullFileName, fileName, resourceId);
                     }
                 }
             }
         }
         dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationDeploy);
     }
     catch
     {
         UtilityPackage.SetDTEStatusBarAndStopAnimate(dte, "Deploy WebResource failed");
     }
 }
Beispiel #7
0
        internal static void Click(DTE dte)
        {
            try
            {
                dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
                if (UtilityPackage.GetCrmServiceClient(dte))
                {
                    var crmServiceClient = (CrmServiceClient)UtilityPackage.GetGlobal("CrmServiceClient", dte);
                    var crmUrl           = (string)UtilityPackage.GetGlobal("CrmUrl", dte);
                    UtilityPackage.SetDTEStatusBar(dte, $"[{crmUrl}] Connected");

                    var fullFileName = dte.SelectedItems.Item(1).ProjectItem.FileNames[0];

                    var cacheReportFromVS = GetCacheReportFromVS(dte, fullFileName);
                    if (cacheReportFromVS == null)
                    {
                        var cacheReportFromFile = GetCacheReportFromFile(fullFileName);

                        var formReport = new FormReport(crmServiceClient, cacheReportFromFile);
                        if (formReport.ShowDialog() == DialogResult.OK)
                        {
                            var language   = formReport.Language;
                            var solution   = formReport.Solution;
                            var reportName = formReport.ReportName;
                            var reportId   = formReport.ReportId;

                            DoDeployReport(dte, crmServiceClient, reportId, fullFileName);
                            AddToCache(dte, language, solution, reportName, reportId, fullFileName);
                        }
                    }
                    else
                    {
                        DoDeployReport(dte, crmServiceClient, cacheReportFromVS.Value, fullFileName);
                    }
                }
                dte.StatusBar.Animate(false, vsStatusAnimation.vsStatusAnimationDeploy);
            }
            catch
            {
                UtilityPackage.SetDTEStatusBarAndStopAnimate(dte, "Deploy WebResource failed");
            }
        }
        internal static void Click(DTE dte)
        {
            dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
            var selectedItem = dte.SelectedItems.Item(1);

            ProjectUniqueName = selectedItem.ProjectItem.ContainingProject.UniqueName;
            var activeConfiguration = dte.Solution.SolutionBuild.ActiveConfiguration.Name;

            DTE = dte;
            try
            {
                UtilityPackage.SetDTEStatusBar(dte, "Building project report...");
                dte.Events.BuildEvents.OnBuildProjConfigDone += BuildEvents_OnBuildProjConfigDone;
                dte.Solution.SolutionBuild.BuildProject(activeConfiguration, ProjectUniqueName, true);
            }
            catch
            {
                dte.Events.BuildEvents.OnBuildProjConfigDone -= BuildEvents_OnBuildProjConfigDone;
                UtilityPackage.SetDTEStatusBar(dte, "   !!!   Building project report fail   !!!   ", true);
                MessageBox.Show("Build report project fail. Please double check and try it again.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #9
0
        internal static void Click(DTE dte)
        {
            try
            {
                dte.StatusBar.Animate(true, vsStatusAnimation.vsStatusAnimationDeploy);
                var config = UtilityPackage.IsValid(dte);
                if (config == null)
                {
                    return;
                }
                UtilityPackage.SetDTEStatusBar(dte, " !!! Read DynamicsCrm.DevKit.Cli.json config !!! ");
                var check = UtilityPackage.GetGlobal("CrmService", dte);
                if (check == null)
                {
                    var connection = UtilityPackage.IsConnection(config.CrmConnection);
                    if (connection == null)
                    {
                        UtilityPackage.SetDTEStatusBar(dte, " !!! Connection Dynamics CRM  failed !!! ", true);
                        return;
                    }
                    UtilityPackage.SetGlobal("CrmService", connection, dte);
                }
                var crmService = (OrganizationServiceProxy)UtilityPackage.GetGlobal("CrmService", dte);
                UtilityPackage.SetDTEStatusBar(dte, " !!! Connected Dynamics CRM !!! ");
                var webResourceId = IsFoundWebResource(dte, crmService, config.JsonWebResource.prefix);
                if (webResourceId == Guid.Empty)
                {
                    UtilityPackage.SetDTEStatusBar(dte, " !!! WebResource not found !!! ", true);
                    return;
                }
                var requests = new OrganizationRequestCollection();

                var webResource = new Entity("webresource")
                {
                    Id = webResourceId
                };
                var content = File.ReadAllText(dte.ActiveDocument.FullName);
                webResource["content"] = Convert.ToBase64String(Encoding.UTF8.GetBytes(content));
                var request = new UpdateRequest {
                    Target = webResource
                };
                requests.Add(request);

                var publishXml = $"<importexportxml><webresources><webresource>{webResource.Id}</webresource></webresources></importexportxml>";
                var pubRequest = new PublishXmlRequest {
                    ParameterXml = publishXml
                };
                requests.Add(pubRequest);

                var multipleRequest = new ExecuteMultipleRequest
                {
                    Requests = requests,
                    Settings = new ExecuteMultipleSettings
                    {
                        ContinueOnError = false,
                        ReturnResponses = true
                    }
                };
                UtilityPackage.SetDTEStatusBar(dte, " !!! Updating & publishing WebResource !!! ");
                var multipleResponse = (ExecuteMultipleResponse)crmService.Execute(multipleRequest);
                foreach (var response in multipleResponse.Responses)
                {
                    if (response.Fault == null)
                    {
                        continue;
                    }
                    UtilityPackage.SetDTEStatusBar(dte, " !!! Deploy WebResource failed !!! ", true);
                    return;
                }
                UtilityPackage.SetDTEStatusBar(dte, " !!! Deploy WebResource succeeded !!! ", true);
            }
            catch
            {
                UtilityPackage.SetDTEStatusBar(dte, " !!! Deploy WebResource failed !!! ", true);
            }
        }
        private static void BuildEvents_OnBuildProjConfigDone(string Project, string ProjectConfig, string Platform, string SolutionConfig, bool Success)
        {
            DTE.Events.BuildEvents.OnBuildProjConfigDone -= BuildEvents_OnBuildProjConfigDone;
            if (!Success || !ProjectUniqueName.EndsWith(Project))
            {
                return;
            }
            UtilityPackage.SetDTEStatusBar(DTE, "Build project report succeeded!");
#if DEBUG
            ProjectUniqueName = @"C:\src\github\phuocle\Dynamics-Crm-DevKit\test\TestReport\Test.Abc.Report.2015\Test.Abc.Report.2015.rptproj";
#endif
            var xml  = File.ReadAllText(ProjectUniqueName);
            var xdoc = XDocument.Parse(xml);
            //Fist check for project VS2015
            var node = (from x in xdoc?.Descendants("Project")?.Descendants("Configurations")?.Elements("Configuration")
                        where x?.Element("Name")?.Value == ProjectConfig
                        select x)?.FirstOrDefault();
            var outputPath = node?.Descendants("Options")?.FirstOrDefault()?.Element("OutputPath")?.Value;
            //if null, then check for project VS2017
            if (outputPath == null)
            {
                var nodes = (from x in xdoc?.Root.Elements()
                             where x?.Name?.LocalName == "PropertyGroup"
                             select x);
                foreach (var n in nodes)
                {
                    if (n.Elements().Where(x => x?.Name?.LocalName == "FullPath" && x?.Value == "Debug").Any())
                    {
                        outputPath = n.Elements().Where(x => x?.Name?.LocalName == "OutputPath").FirstOrDefault()?.Value;
                        break;
                    }
                }
            }
            if (outputPath == null)
            {
                throw new Exception("Cannot read the Output directory of the current report project");
            }
            var folderOutput = Path.Combine(Path.GetDirectoryName(ProjectUniqueName), outputPath);
            var fileName     = Path.GetFileName(DTE.SelectedItems.Item(1).ProjectItem.FileNames[0]);
            var deployFile   = Path.Combine(folderOutput, fileName);
            if (!File.Exists(deployFile))
            {
                throw new Exception($"Cannot find deployable report: {deployFile}");
            }

            var config = UtilityPackage.IsValid(DTE);
            if (config == null)
            {
                return;
            }
            UtilityPackage.SetDTEStatusBar(DTE, " !!! Read DynamicsCrm.DevKit.Cli.json config !!! ");
            var check = UtilityPackage.GetGlobal("CrmService", DTE);
            if (check == null)
            {
                var connection = UtilityPackage.IsConnection(config.CrmConnection);
                if (connection == null)
                {
                    UtilityPackage.SetDTEStatusBar(DTE, " !!! Connection Dynamics CRM  failed !!! ", true);
                    return;
                }
                UtilityPackage.SetGlobal("CrmService", connection, DTE);
            }
            var crmService = (IOrganizationService)UtilityPackage.GetGlobal("CrmService", DTE);
            UtilityPackage.SetDTEStatusBar(DTE, " !!! Connected Dynamics CRM !!! ");

            var fetchData = new
            {
                ismanaged = "0",
                filename  = fileName
            };
            var fetchXml = $@"
<fetch>
  <entity name='report'>
    <attribute name='reportid' />
    <filter type='and'>
      <condition attribute='ismanaged' operator='eq' value='{fetchData.ismanaged/*0*/}'/>
      <condition attribute='filename' operator='eq' value='{fetchData.filename/*ReportTemplate.rdl*/}'/>
    </filter>
  </entity>
</fetch>";
            var rows     = crmService.RetrieveMultiple(new FetchExpression(fetchXml));
            if (rows.Entities.Count == 0)
            {
                throw new Exception("Please deploy this report first by Dynamics 365");
            }
            if (rows.Entities.Count != 1)
            {
                throw new Exception($"Found {rows.Entities.Count} reports file name: {fileName} in the system. Cannot deploy.");
            }
            UtilityPackage.SetDTEStatusBar(DTE, "Deploying Report ...");
            var update = new Entity("report", rows.Entities[0].Id);
            update["bodytext"] = File.ReadAllText(deployFile);
            crmService.Update(update);
            UtilityPackage.SetDTEStatusBar(DTE, "   !!!   Deploy Report succeeded   !!!   ", true);
        }
Beispiel #11
0
        private static Guid DeployNewWebResource(DTE dte, CrmServiceClient crmServiceClient, string fullFileName, string webResourceName)
        {
            var fileContent = Convert.ToBase64String(File.ReadAllBytes(fullFileName));
            var webResource = new Entity("webresource")
            {
                ["name"]        = webResourceName,
                ["displayname"] = webResourceName,
                ["content"]     = fileContent
            };
            var webResourceFileInfo = new FileInfo(fullFileName);
            var fileType            = WebResourceWebResourceType.ScriptJScript;

            switch (webResourceFileInfo.Extension.ToLower().TrimStart('.'))
            {
            case "html":
            case "htm":
                fileType = WebResourceWebResourceType.WebpageHtml;
                break;

            case "js":
                fileType = WebResourceWebResourceType.ScriptJScript;
                break;

            case "png":
                fileType = WebResourceWebResourceType.PngFormat;
                break;

            case "gif":
                fileType = WebResourceWebResourceType.GifFormat;
                break;

            case "jpg":
            case "jpeg":
                fileType = WebResourceWebResourceType.JpgFormat;
                break;

            case "css":
                fileType = WebResourceWebResourceType.StyleSheetCss;
                break;

            case "ico":
                fileType = WebResourceWebResourceType.IcoFormat;
                break;

            case "xml":
                fileType = WebResourceWebResourceType.DataXml;
                break;

            case "xsl":
            case "xslt":
                fileType = WebResourceWebResourceType.StyleSheetXsl;
                break;

            case "xap":
                fileType = WebResourceWebResourceType.SilverlightXap;
                break;

            case "resx":
                fileType = WebResourceWebResourceType.StringResx;
                break;

            case "svg":
                fileType = WebResourceWebResourceType.SvgFormat;
                break;
            }
            webResource["webresourcetype"] = new OptionSetValue((int)fileType);
            if (fileType == WebResourceWebResourceType.StringResx)
            {
                var fileName = webResourceFileInfo.Name.Substring(0, webResourceFileInfo.Name.Length - webResourceFileInfo.Extension.Length);
                var arr      = fileName.Split(".".ToCharArray());
                if (int.TryParse(arr[arr.Length - 1], out var languagecode))
                {
                    var req = new RetrieveProvisionedLanguagesRequest();
                    var res = (RetrieveProvisionedLanguagesResponse)crmServiceClient.Execute(req);
                    if (res.RetrieveProvisionedLanguages.Contains(languagecode))
                    {
                        webResource["languagecode"] = languagecode;
                    }
                    else
                    {
                        throw new Exception($"Language code not found: {languagecode}");
                    }
                }
            }
            UtilityPackage.SetDTEStatusBar(dte, $"Created WebResource: [{webResourceName}]");
            return(crmServiceClient.Create(webResource));
        }