public void VsixDeployWebResourcesTest()
        {
            //first delete all the test deployed javascriot files
            var javaScriptFiles = GetJavaScriptFiles();

            DeleteJavaScriptFileRecords();
            Assert.IsFalse(GetJavaScriptFileRecords().Any());

            //create an app, deploy and verify created
            VisualStudioService.SetSelectedItems(GetJavaScriptFiles().Select(f => new FakeVisualStudioProjectItem(f)).ToArray());
            var app = CreateAndLoadTestApplication <DeployWebResourceModule>();

            //run the deploy including a redirect to enter package settings first
            var originalConnection = HijackForPackageEntryRedirect(app);
            //lets delete the settings files, then verify they are recreated during the redirect entry
            var solutionItemsFolder = Path.Combine(VisualStudioService.SolutionDirectory, VisualStudioService.ItemFolderName);

            FileUtility.DeleteFiles(solutionItemsFolder);
            var solutionSettingFiles = FileUtility.GetFiles(solutionItemsFolder);

            Assert.AreEqual(0, solutionSettingFiles.Count());

            //run the dialog
            var dialog = app.NavigateToDialog <DeployWebResourceModule, DeployWebResourceDialog>();

            VerifyPackageEntryRedirect(originalConnection, dialog);
            //verify the 2 settings files recreated
            solutionSettingFiles = FileUtility.GetFiles(solutionItemsFolder);
            Assert.AreEqual(2, solutionSettingFiles.Count());
            Assert.AreEqual(GetJavaScriptFiles().Count(), GetJavaScriptFileRecords().Count());

            var records             = GetJavaScriptFileRecords();
            var currentComponentIds = XrmRecordService.GetSolutionComponents(dialog.Service.PackageSettings.Solution.Id, OptionSets.SolutionComponent.ObjectTypeCode.WebResource).ToList();

            Assert.IsTrue(records.All(r => currentComponentIds.Contains(r.Id)));
            //okay the code also allows match by display name
            //so I will add this to verify that too
            //basically change ones display name and file name then verify it still matches
            var files         = GetJavaScriptFiles();
            var firstFileInfo = new FileInfo(files.First());
            var record        = records.First(e => e.GetStringField(Fields.webresource_.name) == firstFileInfo.Name);

            XrmRecordService.Delete(record);
            var newRecord = XrmRecordService.NewRecord(Entities.webresource);

            newRecord.SetField(Fields.webresource_.name, "jrm_fakescriptname", XrmRecordService);
            newRecord.SetField(Fields.webresource_.displayname, firstFileInfo.Name, XrmRecordService);
            newRecord.SetField(Fields.webresource_.webresourcetype, OptionSets.WebResource.Type.ScriptJScript, XrmRecordService);
            XrmRecordService.Create(newRecord);

            //create an app, deploy again and verify not duplicated
            VisualStudioService.SetSelectedItems(GetJavaScriptFiles().Select(f => new FakeVisualStudioProjectItem(f)).ToArray());
            app    = CreateAndLoadTestApplication <DeployWebResourceModule>();
            dialog = app.NavigateToDialog <DeployWebResourceModule, DeployWebResourceDialog>();

            Assert.AreEqual(GetJavaScriptFiles().Count(), GetJavaScriptFileRecords().Count());
        }
        public IRecord ReCreateTestSolution()
        {
            var testSolution = XrmRecordService.GetFirst(JosephM.Xrm.Schema.Entities.solution, JosephM.Xrm.Schema.Fields.solution_.uniquename, "TESTSCRIPTSOLUTION");

            if (testSolution != null)
            {
                XrmRecordService.Delete(testSolution);
            }

            var publisher = XrmRecordService.GetFirst(JosephM.Xrm.Schema.Entities.publisher, JosephM.Xrm.Schema.Fields.publisher_.uniquename, "josephmcgregor");

            testSolution = XrmRecordService.NewRecord(JosephM.Xrm.Schema.Entities.solution);
            testSolution.SetField(JosephM.Xrm.Schema.Fields.solution_.publisherid, publisher.ToLookup(), XrmRecordService);
            testSolution.SetField(JosephM.Xrm.Schema.Fields.solution_.uniquename, "TESTSCRIPTSOLUTION", XrmRecordService);
            testSolution.SetField(JosephM.Xrm.Schema.Fields.solution_.friendlyname, "TESTSCRIPTSOLUTION", XrmRecordService);
            testSolution.SetField(JosephM.Xrm.Schema.Fields.solution_.version, "1.0.0.0", XrmRecordService);
            testSolution.Id = XrmRecordService.Create(testSolution);
            return(testSolution);
        }
        public void VsixDeployWebResourcesTest()
        {
            //first delete all the test deployed javascriot files
            var javaScriptFiles = GetJavaScriptFiles();

            DeleteJavaScriptFileRecords();
            Assert.IsFalse(GetJavaScriptFileRecords().Any());

            //create an app, deploy and verify created
            VisualStudioService.SetSelectedItems(GetJavaScriptFiles().Select(f => new FakeVisualStudioProjectItem(f)).ToArray());
            var testApplication = CreateAndLoadTestApplication <DeployWebResourceModule>();
            var dialog          = testApplication.NavigateToDialog <DeployWebResourceModule, DeployWebResourceDialog>();

            Assert.AreEqual(GetJavaScriptFiles().Count(), GetJavaScriptFileRecords().Count());

            //okay the code also allows match by display name
            //so I will add this to verify that too
            //basically change ones display name and file name then verify it still matches
            var files         = GetJavaScriptFiles();
            var records       = GetJavaScriptFileRecords();
            var firstFileInfo = new FileInfo(files.First());
            var record        = records.First(e => e.GetStringField(Fields.webresource_.name) == firstFileInfo.Name);

            XrmRecordService.Delete(record);
            var newRecord = XrmRecordService.NewRecord(Entities.webresource);

            newRecord.SetField(Fields.webresource_.name, "jrm_fakescriptname", XrmRecordService);
            newRecord.SetField(Fields.webresource_.displayname, firstFileInfo.Name, XrmRecordService);
            newRecord.SetField(Fields.webresource_.webresourcetype, OptionSets.WebResource.Type.ScriptJScript, XrmRecordService);
            XrmRecordService.Create(newRecord);

            //create an app, deploy again and verify not duplicated
            VisualStudioService.SetSelectedItems(GetJavaScriptFiles().Select(f => new FakeVisualStudioProjectItem(f)).ToArray());
            testApplication = CreateAndLoadTestApplication <DeployWebResourceModule>();
            dialog          = testApplication.NavigateToDialog <DeployWebResourceModule, DeployWebResourceDialog>();

            Assert.AreEqual(GetJavaScriptFiles().Count(), GetJavaScriptFileRecords().Count());
        }
        public override void ExecuteExtention(DeployIntoFieldRequest request, DeployIntoFieldResponse response,
                                              ServiceRequestController controller)
        {
            var records = new List <IRecord>();

            var publishIds = new List <string>();

            var numberToDo = request.Files.Count();
            var numberDone = 0;

            foreach (var file in request.Files.Select(f => f?.Replace("∕", "∕")))
            {
                var fileInfo = new FileInfo(file);

                controller.UpdateProgress(++numberDone, numberToDo, "Importing " + fileInfo.Name);
                var thisResponseItem = new DeployIntoFieldResponseItem()
                {
                    Name = fileInfo.Name
                };
                response.AddResponseItem(thisResponseItem);

                try
                {
                    var containingFolderName       = fileInfo.Directory.Name;
                    var containingFolderParentName = fileInfo.Directory.Parent != null
                        ? fileInfo.Directory.Parent.Name
                        : null;
                    //get target record type
                    string recordType = null;
                    if (Service.GetAllRecordTypes().Any(r => r == containingFolderName))
                    {
                        recordType = Service.GetAllRecordTypes().First(r => r == containingFolderName);
                    }
                    else if (Service.GetAllRecordTypes().Any(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower()))
                    {
                        recordType = Service.GetAllRecordTypes().First(r => Service.GetDisplayName(r)?.ToLower() == containingFolderName.ToLower());
                    }
                    else
                    {
                        throw new NullReferenceException($"Could not find matching type by logical or display name for folder name of {containingFolderName}");
                    }

                    thisResponseItem.RecordType = recordType;

                    if (recordType == Entities.adx_webfile)
                    {
                        //this one goes into an attachment
                        var matchingRecord = GetRecordToDeployInto(recordType, fileInfo.Name, containingFolderParentName);
                        if (matchingRecord == null)
                        {
                            throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into");
                        }
                        thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open");
                        //get matching attachment by name else create a new one
                        var fileAttachments = Service.RetrieveAllAndClauses(Entities.annotation, new[]
                        {
                            new Condition(Fields.annotation_.filename, ConditionType.Equal, fileInfo.Name),
                            new Condition(Fields.annotation_.objectid, ConditionType.Equal, matchingRecord.Id)
                        }).OrderBy(n => n.GetDateTime(Fields.annotation_.createdon)).ToArray();

                        var contentBytes        = File.ReadAllBytes(file);
                        var contentBase64String = Convert.ToBase64String(contentBytes);
                        if (fileAttachments.Any())
                        {
                            //lets update the last modifed one
                            var attachmentToUpdate = fileAttachments.First();
                            if (attachmentToUpdate.GetStringField(Fields.annotation_.documentbody) != contentBase64String)
                            {
                                attachmentToUpdate.SetField(Fields.annotation_.documentbody, contentBase64String, Service);
                                Service.Update(attachmentToUpdate, new[] { Fields.annotation_.documentbody });
                                thisResponseItem.Updated = true;
                            }
                        }
                        else
                        {
                            //lets create a new attachment
                            var newAttachment = Service.NewRecord(Entities.annotation);
                            newAttachment.SetLookup(Fields.annotation_.objectid, matchingRecord.Id, matchingRecord.Type);
                            newAttachment.SetField(Fields.annotation_.subject, fileInfo.Name, Service);
                            newAttachment.SetField(Fields.annotation_.filename, fileInfo.Name, Service);
                            newAttachment.SetField(Fields.annotation_.documentbody, contentBase64String, Service);
                            Service.Create(newAttachment);
                            thisResponseItem.Created = true;
                        }
                    }
                    else
                    {
                        //get the record with the same name as the file
                        var nameToMatch = fileInfo.Name.Substring(0, fileInfo.Name.LastIndexOf("."));

                        var matchingRecord = GetRecordToDeployInto(recordType, nameToMatch, containingFolderParentName);
                        if (matchingRecord == null)
                        {
                            throw new NullReferenceException($"There is no {Service.GetDisplayName(recordType)} record name {fileInfo.Name} to load the file attachment into");
                        }
                        thisResponseItem.Link = new Url(Service.GetWebUrl(matchingRecord.Type, matchingRecord.Id), "Open");
                        var targetField = GetTargetField(fileInfo, recordType);

                        var contentText = File.ReadAllText(file);
                        if (matchingRecord.GetStringField(targetField) != contentText)
                        {
                            matchingRecord.SetField(targetField, contentText, Service);
                            Service.Update(matchingRecord, new[] { targetField });
                            thisResponseItem.Updated = true;
                        }
                    }
                }
                catch (Exception ex)
                {
                    thisResponseItem.Exception = ex;
                }
            }
        }
Exemple #5
0
        public override void ExecuteExtention(DeployWebResourceRequest request, DeployWebResourceResponse response,
                                              ServiceRequestController controller)
        {
            var records = new List <IRecord>();

            var publishIds = new List <string>();

            var totalTasks     = request.Files.Count() + 1;
            var tasksCompleted = 0;

            foreach (var file in request.Files)
            {
                var fileInfo = new FileInfo(file);
                var fileName = fileInfo.Name;
                controller.UpdateProgress(++tasksCompleted, totalTasks, "Deploying " + fileName);

                var responseItem = new DeployWebResourceResponseItem();
                responseItem.Name = fileName;
                response.AddResponseItem(responseItem);

                try
                {
                    var content       = File.ReadAllBytes(file);
                    var contentString = Convert.ToBase64String(content);

                    //okay lets allow match on either name or display name
                    var match = Service.GetFirst(Entities.webresource, Fields.webresource_.name, fileName);
                    if (match == null)
                    {
                        match = Service.GetFirst(Entities.webresource, Fields.webresource_.displayname, fileName);
                    }
                    if (match != null)
                    {
                        if (match.GetStringField(Fields.webresource_.content) != contentString)
                        {
                            match.SetField(Fields.webresource_.content, contentString, Service);
                            Service.Update(match, new[] { Fields.webresource_.content });
                            publishIds.Add(match.Id);
                            responseItem.Updated = true;
                        }
                    }
                    else
                    {
                        var record = Service.NewRecord(Entities.webresource);
                        record.SetField(Fields.webresource_.name, fileInfo.Name, Service);
                        record.SetField(Fields.webresource_.displayname, fileInfo.Name, Service);
                        record.SetField(Fields.webresource_.content, Convert.ToBase64String(content), Service);
                        record.SetField(Fields.webresource_.webresourcetype, GetWebResourceType(fileInfo.Extension), Service);
                        record.Id = Service.Create(record);
                        publishIds.Add(record.Id);
                        responseItem.Created = true;
                    }
                }
                catch (Exception ex)
                {
                    responseItem.Exception = ex;
                }
            }

            if (publishIds.Any())
            {
                controller.UpdateProgress(totalTasks, totalTasks, "Publishing Files");
                var xml = new StringBuilder();
                xml.Append("<importexportxml><webresources>");
                foreach (var id in publishIds)
                {
                    xml.Append("<webresource>" + id + "</webresource>");
                }
                xml.Append("</webresources></importexportxml>");
                Service.Publish(xml.ToString());
            }

            //add plugin assembly to the solution
            var componentType = OptionSets.SolutionComponent.ObjectTypeCode.WebResource;

            if (PackageSettings.AddToSolution)
            {
                Service.AddSolutionComponents(PackageSettings.Solution.Id, componentType, publishIds);
            }
        }