How to use C# Web API in Dynamics 365 - dynamics-crm

I am using online instance of Dynamics 365. I have created C# Web API to export data from activity entity. When I am running it from visual studio it's working fine.
Now I want to call it on button click in Dynamics 365. The requirement is that when user click on the button then the Web API should be call and data will be exported.
I don't have idea, how to get this task done. can anyone help me to solve this problem. kindly provide me steps to get this task.
Web API code is given below
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.ServiceModel;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk;
using System.Windows.Forms;
namespace Experiments
{
class Program
{
private static OrganizationService _orgService;
static void Main(string[] args)
{
try
{
CrmConnection connection = CrmConnection.Parse(ConfigurationManager.ConnectionStrings["CrmOnline"].ConnectionString);
using (_orgService = new OrganizationService(connection))
{
var exportToExcelRequest = new OrganizationRequest("ExportToExcel");
exportToExcelRequest.Parameters = new ParameterCollection();
//Has to be a savedquery aka "System View" or userquery aka "Saved View"
//The view has to exist, otherwise will error out
exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("View", new EntityReference("savedquery", new Guid("{00000000-0000-0000-00AA-000010001902}"))));
exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("FetchXml", #"<?xml version='1.0'?>
<fetch distinct='false' mapping='logical' output-format='xml-platform' version='1.0'>
<entity name='activitypointer'>
<attribute name='subject'/>
<attribute name='ownerid'/>
<attribute name='prioritycode'/>
<attribute name='regardingobjectid'/>
<attribute name='activitytypecode'/>
<attribute name='statecode'/>
<attribute name='scheduledstart'/>
<attribute name='scheduledend'/>
<attribute name='activityid'/>
<attribute name='instancetypecode'/>
<attribute name='community'/>
<attribute name='senton'/>
<attribute name='statuscode'/>
<order descending='false' attribute='scheduledend'/>
<filter type='and'>
<condition attribute='actualdurationminutes' value='43800' operator='le'/>
</filter>
<link-entity name='systemuser' alias='activitypointerowningusersystemusersystemuserid' link-type='outer' visible='false' to='owninguser' from='systemuserid'>
<attribute name='internalemailaddress'/>
</link-entity>
<link-entity name='email' alias='email_engagement' link-type='outer' visible='false' to='activityid' from='activityid'><attribute name='isemailfollowed'/>
<attribute name='lastopenedtime'/>
<attribute name='delayedemailsendtime'/>
</link-entity>
</entity>
</fetch>"));
exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("LayoutXml", #"
<grid name='resultset' object='2' jump='fullname' select='1' icon='1' preview='1'>
<row name='result' id='activitypointerid'>
<cell name='activitytypecode' width='150' />
<cell name='statecode' width='112' />
<cell name='scheduledstart' width='110' />
<cell name='scheduledend' width='110' />
</row>
</grid>"));
//need these params to keep org service happy
exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("QueryApi", ""));
exportToExcelRequest.Parameters.Add(new KeyValuePair<string, object>("QueryParameters",new InputArgumentCollection()));
var exportToExcelResponse = _orgService.Execute(exportToExcelRequest);
if (exportToExcelResponse.Results.Any())
{
File.WriteAllBytes("Activities.xlsx", exportToExcelResponse.Results["ExcelFile"] as byte[]);
}
}
}
catch (FaultException<OrganizationServiceFault> ex)
{
string message = ex.Message;
throw;
}
}
}
}
Thanks.

I'd recommend you compile your C# code as an Action and include it as a step in a workflow. See here on how to invoke a custom action from a workflow.
I'd then recommend you install the Ribbon Workbench. This will let you customise your forms and navigations to add one or many buttons. You can use the workbench to customise these buttons, setting their commands to call your workflow which in turn calls your Action (your C# code).
Note there are several solutions to achieve what you're asking, I've just suggested one. Other solutions would likely include JavaScript and calling the Web API client-side.

Since you are using Dynamics 365 online, I will recommend you to create a Microsoft FLOW App with a CDS connector. Hope this helps.

Related

How can I open an App using it own background service

I'm creating Xamarin.Forms App and it has a service that runs in the background monitoring if any messages arrive. If a message arrives, the service should open the App.
I tried to open the App using Intent (Dependency Service) and I also tried using Xamarin.Essentials. Neither way worked.
My last attempt was with Xamarin.Essentials and gave the following error:
[Error][1]
[1]: https://i.stack.imgur.com/5jPGf.png
This is my code using Intent (Dependency Service):
//Intent (Dependency Service)
string teste = "com.saferit.saferproject";
openAppManager = DependencyService.Get<IAppHandler>();
openAppManager.Initialize(teste);
openAppManager.LaunchApp();
[enter image description here][2]
[2]: https://i.stack.imgur.com/lLx3g.png
This is my native code for Android
public void LaunchApp()
{
Intent intent = new Intent();
intent.SetClassName(Package, "activity");
intent.AddFlags(ActivityFlags.NewTask);
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
[Complete Native Code][3]
[3]: https://i.stack.imgur.com/G4zOV.png
This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.saferit.saferproject" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="27" android:targetSdkVersion="29" />
<!--<application android:label="SaferProject.Android"></application>-->
<application android:label="com.saferit.saferproject" android:icon="#drawable/logo">
<activity android:icon="#drawable/logo" android:label="com.saferit.saferproject" android:name="com.saferit.saferproject">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.saferit.saferproject" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
This is my attempt using Xamarin.Essential:
//Xamarin.Essentials
var appname = "com.saferit.saferproject://";
var supportsUri = await Launcher.CanOpenAsync(appname);
if (supportsUri)
await Launcher.OpenAsync(appname);
Looking for here at stackoverflow I found this question:
Open Notification list page when user clicks on navigation
I tried to implement it on my App but I also couldn't get it to work.
In all three cases the errors are very similar.
I think the problem is in the configuration of the URI of my App.
Can someone help me?

Azure Load test does not report data driven urls

I have a simple load test that basically executes a single webtest on a constant load. That webtest is hooked to an xml file data source that contains urls to my entire site.
When I execute the load test from my local environment, the test summary page reports the individual urls in the "Top 5 slowest pages" i.e. "https://mysite.or/page" . But when I execute the same test from Azure (i.e. changed Test run location to VSTS in .testsettings), the links are reported as "https://{{Enviroment}}{{Sitemap.url.loc}}". This seems to be just a reporting issue and I can validate that azure is correctly invoking the urls from the data source. Why would the tests from Azure not report the url constructed from the datasource?
Load Test Summary: Executed from Local
Same test executed on Azure
Webtest:
<?xml version="1.0" encoding="utf-8"?>
<WebTest Name="GenericSitemap" Id="02954e81-f3a7-4c9c-94f5-3a4304f88361" Owner="" Priority="2147483647" Enabled="True" CssProjectStructure="" CssIteration="" Timeout="0" WorkItemIds="" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010" Description="" CredentialUserName="" CredentialPassword="" PreAuthenticate="True" Proxy="default" StopOnError="False" RecordedResultFile="" ResultsLocale="">
<Items>
<Request Method="GET" Guid="01c37ffa-92db-42e8-9d25-a042dcd0123d" Version="1.1" Url="https://{{Enviroment}}{{Sitemap.url.loc}}" ThinkTime="0" Timeout="300" ParseDependentRequests="True" FollowRedirects="True" RecordResult="True" Cache="False" ResponseTimeGoal="0" Encoding="utf-8" ExpectedHttpStatusCode="0" ExpectedResponseUrl="https://{{Enviroment}}{{Sitemap.url.loc}}" ReportingName="" IgnoreHttpStatusCode="False" />
</Items>
<DataSources>
<DataSource Name="Sitemap" Provider="Microsoft.VisualStudio.TestTools.DataSource.XML" Connection="|DataDirectory|\..\Data\sitemap.xml">
<Tables>
<DataSourceTable Name="url" SelectColumns="SelectOnlyBoundColumns" AccessMethod="Random" />
</Tables>
</DataSource>
</DataSources>
<ContextParameters>
<ContextParameter Name="Enviroment" Value="mysite.net" />
</ContextParameters>
</WebTest>
Thanks to #AdrianHHH. I got it working by creating a requestPlugin and setting it on the data driven requests.
Here's my plugin:
[DisplayName("Set Request Params")]
[Description("Fix request urls when run from Azure")]
public class SetRequestParams : WebTestRequestPlugin
{
public override void PreRequest(object sender, PreRequestEventArgs e)
{
e.Request.ReportingName = e.Request.Url;
}
}

Dynamics CRM - Downloading files from notes in C# results in corrupt files with FetchXML

I am using Dynamics CRM and am trying to download a file from the notes (annotation) entity. I am getting the Base64 String and converting to Byte[] like this:
Convert.FromBase64String((string)item.Attributes["documentbody"]);
But when I do so and try to open the file it is corrupt. Word Documents and PDFs refuse to open. Images however open partially. Ie. I get about 3/4 of the image but the rest of the image is solid grey.
I am guessing FetchXML is not returning the full string. Is there a way to to do this in FetchXML, overiding the limit? I have Googled but cannot see any mention of FetchXML limit?
There is a similar SO question here: FetchXml Query Returns Trimmed DocumentBody?
But I am not using "distinct" in my query.
Here is what I have so far:
<fetch>
<entity name="annotation" >
<attribute name="documentbody" />
<filter type="and" >
<condition attribute="objectid" operator="eq" value="{D4FFE0CD-CDFA-E711-80E4-005056BA77B6}" />
<condition attribute="isdocument" operator="eq" value="1" />
</filter>
<order attribute="modifiedon" descending="true" />
</entity>
</fetch>
You don't say how you are saving or opening the file, which I suspect is probably the issue.
Re: "I am guessing FetchXML is not returning the full string" - I don't think that is correct, I've not seen this happen.
This is a working example of saving a file to disk.
Exporting Annotation (Note) Attachment
public void ExportDocuments(IOrganizationService service, String filePath)
{
String fetch = #"<fetch mapping='logical' count='100' version='1.0'>
<entity name='annotation'>
<attribute name='filename' />
<attribute name='documentbody' />
<attribute name='mimetype' />
</entity>
</fetch>";
foreach (Entity e in service.RetrieveMultiple(new FetchExpression(fetch)))
{
if (!String.IsNullOrWhiteSpace(e.Attributes["documentbody"].ToString()))
{
byte[] data = Convert.FromBase64String(e.Attributes["documentbody"].ToString());
File.WriteAllBytes(filePath + e.Attributes["filename"].ToString(), data);
}
}
}
I believe your fetchXML query have the issue. I too faced the same issue but with images in SSRS reports. Images were not loaded properly/got corrupt.
I fixed the issue by updating the fetch XML query with distinct = false as in the below statement.
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" >
.....
</fetch>

Download NuGet from behind a proxy using inline MSBuild Task

I am using the NuGet.targets file from NuGet to automatically download NuGet.exe and then restore the packets in my project.
This was working well, however at work we have a proxy and this method was failing due to a (407) Proxy Authentication Required Exception. I modified the targets file to use the proxy details and although this method works in an application it does not work in the MSBuild Task, the code is identical.
If I hardcode the proxy and my login details it works, when I build my solution NuGet.exe is downloaded and the packages are restored correctly. The problem only appears to be the authentication in the MSBuild task, I have absolutely no idea why. I have attached my modified code.
If anyone can help I'd appreciate it. Thanks
<UsingTask TaskName="DownloadNuGet" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<OutputFilename ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Core" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Net" />
<Using Namespace="Microsoft.Build.Framework" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
try
{
OutputFilename = Path.GetFullPath(OutputFilename);
Log.LogMessage("Downloading latest version of NuGet.exe...");
using(WebClient webClient = new WebClient())
{
webClient.UseDefaultCredentials = true;
webClient.Proxy = WebRequest.GetSystemWebProxy();
webClient.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
webClient.DownloadFile("https://www.nuget.org/nuget.exe", OutputFilename);
return true;
}
}
catch (Exception ex)
{
Log.LogErrorFromException(ex);
return false;
}
]]>
</Code>
</Task>
</UsingTask>

How to consume wcf service running as windows service in ajax client

I have created a WCF service and it is hosted in windows service. When I have added a web reference to that service to an asp.net web forms project through right client menu in the solution explorer I am able to access the service and add reference to it.
Now I want to access this service through AJAX client (i.e in ASP.NET project through ScriptManager component)and call the service in a timer to get continuous stream of values.
I have never worked on AJAX or web that much, I did not find an suitable example on net on this.
I'm using WSHttpBinding.
I'm posting my code so that you can tell where I'm doing wrong.
WCF Service Library Code:
ITestService.cs code....
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Web;
namespace TestServiceLibrary
{
// NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
[ServiceContract(Namespace="TestServiceLibrary")]
public interface ITestService
{
[OperationContract]
[WebGet]
double Add(double n1, double n2);
// TODO: Add your service operations here
}
}
TestService.cs code...............
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace TestServiceLibrary
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in App.config.
public class TestService : ITestService
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
}
}
TestServiceHost.cs (code of console application)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using TestServiceLibrary;
namespace TestServiceHost
{
class Program
{
static void Main(string[] args)
{
ServiceHost myhost = new ServiceHost(typeof(TestService));
myhost.Open();
while (System.Console.ReadKey().Key != System.ConsoleKey.Enter)
{
//System.Threading.Thread.Sleep(100);
}
myhost.Close();
}
}
}
XML Configuration of app.config... same in both wcf service library and wcf service host(console application in this case..)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="TestServiceLibrary.TestService" behaviorConfiguration="TestServiceLibrary.Service1Behavior">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8731/TestServiceLibrary/TestService/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint name="TestService_wsHttpBinding" address ="" binding="wsHttpBinding" contract="TestServiceLibrary.ITestService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TestServiceLibrary.Service1Behavior">
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Web Client (asp.net client, default.aspx) code...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple AJAX Service Client Page</title>
<script type="text/javascript">
// <![CDATA[
// This function creates an asynchronous call to the service
function makeCall(operation){
var n1 = document.getElementById("num1").value;
var n2 = document.getElementById("num2").value;
// If user filled out these fields, call the service
if(n1 && n2){
// Instantiate a service proxy
var proxy = new TestServiceLibrary.ITestService();
// Call correct operation on proxy
switch(operation){
case "Add":
proxy.Add(parseFloat(n1), parseFloat(n2), onSuccess, onFail, null);
break;
}
}
}
// This function is called when the result from the service call is received
function onSuccess(mathResult){
document.getElementById("result").value = mathResult;
}
// This function is called if the service call fails
function onFail(){
document.getElementById("result").value = "Error";
}
// ]]>
</script>
</head>
<body>
<h1>
Simple AJAX Service Client Page</h1>
<p>
First Number:
<input type="text" id="num1" /></p>
<p>
Second Number:
<input type="text" id="num2" /></p>
<input id="btnAdd" type="button" onclick="return makeCall('Add');" value="Add" />
<p>
Result:
<input type="text" id="result" /></p>
<form id="mathForm" action="" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server">
<Services>
<asp:ServiceReference Path="http://localhost:8732/TestServiceLibrary/TestService/" />
</Services>
</asp:ScriptManager>
</form>
</body>
</html>
The error Im getting when accessing the webservice through asp.net in ajax is Microsoft JScript runtime error: 'TestServiceLibrary' is undefined
Please go through this code and help me in finding the problem. Thank you all for your responses.
Looks like the problem is with my service hosting and the endpoint i'm using.
I should modified my service hosting in the console application to use WebServiceHost instead of ServiceHost, then only the ajax clients can talk to my service. Instead of wsHttpBinding, I should use webHttpBinding.
So the code for webHosting is as follows.
using (var host = new WebServiceHost(
typeof(TestService)))
{
// Start listening for messages
host.Open();
Console.WriteLine("Press any key to stop the service.");
Console.ReadKey();
// Close the service
host.Close();
}
The xml configuration of my console is
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<services>
<service
name="TestServiceLibrary.TestService"
behaviorConfiguration="">
<endpoint address="http://localhost:8732/TestService"
binding="webHttpBinding"
bindingConfiguration=""
name="TestService_WebHttp"
contract="TestServiceLibrary.ITestService" />
</service>
</services>
</system.serviceModel>
</configuration>
Now when I did this changes I'm able to call my service through ie using the following url in ie http://localhost:8732/TestService/Add?n1=20&n2=20 and result returned by it is as follows <double xmlns="http://schemas.microsoft.com/2003/10/Serialization/">40</double>
Finally i found the solution to my problem. Im using JSON as way of communicating data and the script for receiving the data is as follows:
<script type="text/javascript">
$("#mybutton").click(function () {
$.getJSON("http://localhost:8732/TestService/Add", null, function (result) {
});
});
</script>
Use some tool like firebug to determine what is happening with the request. WSHttpBinding is secure by default. Check your security settings. Try first with no security to make sure it is not a security issue.
Have you attempted to connect to the service from the AJAX client yet? If so, are you getting any errors?
Without seeing code, there could be a number of things as Chandermani has said.
I've not done AJAX with WCF, but looking over the article Preet recommended, I would suggest checking (if you haven't already) that your AJAX client has the necessary code as per the article.
Are your service operations decorated with [WebGet]?
Do you have the config file for the AJAX client set up properly? Is the service's config file set up properly?

Resources