Custom Workflow Activity Image Download Failing C# (Crm Online 2016) - dynamics-crm

I'm new to custom workflow activities in Dynamics CRM 2016. I'm trying to download an image from url in the code below, the workflow is deployed in Dynamics crm online 2016. When I try to debug in visual studio the Plugin Registration Tool crashes without showing any errors at this line:
byte[] imageBytes = client.DownloadData(imageUrl);
Code:
private string ConvertImageToBase64(string imageUrl)
{
var base64String = string.Empty;
using (WebClient client = new WebClient())
{
byte[] imageBytes = client.DownloadData(imageUrl);==== Fails here.
base64String = Convert.ToBase64String(imageBytes);
}
return base64String;
}

Related

Sending emails from Visual Studio using Exchange Web Service

I am trying to send email using Exchange Web Service (Microsoft.Exchange.WebServices.NETStandard) and it is working when I am using Linqpad.
But the same code does not send email when running in Visual Studio. There is no error either.
I am running either app as myself.
Appreciate any idea to troubleshoot this issue.
var service = new ExchangeService(ExchangeVersion.Exchange2016)
{
Url = new Uri("..."),
Credentials = CredentialCache.DefaultNetworkCredentials
};
var address = "...";
var message = new EmailMessage(service)
{
From = address,
Subject = "test",
Body = new MessageBody(BodyType.Text, "test")
};
message.ToRecipients.Add(address);
message.Send();
I changed the Nuget reference in Visual Studio to Microsoft.Exchange.WebService and that solved the issue.

ChatBot did not work in Web Emulator but work well in Local Bot Framework emulator

I developed the ChatBot that integrates with SharePoint On Premise. When I debug the ChatBot in emulator, it work. But When I debug on Web Emulator in Azure and Website Hosted in Company Website by using DirectLine, it did not work.
Does anyone know how to solve it?
Herewith my screenshot.
Left hand side is from Web Emulator, Right hand side is from local Bot Framework Emulator
Update with Source Code (09 December 2019)
XmlNamespaceManager xmlnspm = new XmlNamespaceManager(new NameTable());
Uri sharepointUrl = new Uri("https://mvponduty.sharepoint.com/sites/sg/daw/");
xmlnspm.AddNamespace("atom", "http://www.w3.org/2005/Atom");
xmlnspm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");
xmlnspm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
NetworkCredential cred = new System.Net.NetworkCredential("engsooncheah#mvponduty.onmicrosoft.com", "Pa$$w0rd", "mvponduty.onmicrosoft.com");
HttpWebRequest listRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "_api/lists/getByTitle('" + "data#work" + "')/items?$filter=Keywords%20eq%20%27bloomberg%27");
listRequest.Method = "GET";
listRequest.Accept = "application/atom+xml";
listRequest.ContentType = "application/atom+xml;type=entry";
listRequest.Credentials = cred;
//LINE 136 start from below
HttpWebResponse listResponse = (HttpWebResponse)listRequest.GetResponse();
StreamReader listReader = new StreamReader(listResponse.GetResponseStream());
XmlDocument listXml = new XmlDocument();
listXml.LoadXml(listReader.ReadToEnd());
if (listResponse.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("Connected");
await turnContext.SendActivityAsync("Connected");
}
// Get and display all the document titles.
XmlElement root = listXml.DocumentElement;
XmlNodeList elemList = root.GetElementsByTagName("content");
XmlNodeList elemList_title = root.GetElementsByTagName("d:Title");
XmlNodeList elemList_desc = root.GetElementsByTagName("d:Description");
//for LINK
XmlNodeList elemList_Id = root.GetElementsByTagName("d:Id");
XmlNodeList elemList_Source = root.GetElementsByTagName("d:Sources");
XmlNodeList elemList_ContentTypeId = root.GetElementsByTagName("d:ContentTypeId");
var attachments = new List<Attachment>();
for (int i = 0; i < elemList.Count; i++)
{
string title = elemList_title[i].InnerText;
string desc = elemList_desc[i].InnerText;
string baseurllink = "https://mvponduty.sharepoint.com/sites/sg/daw/Lists/data/DispForm.aspx?ID=";
string LINK = baseurllink + elemList_Id[i].InnerText + "&Source=" + elemList_Source[i].InnerText + "&ContentTypeId=" + elemList_ContentTypeId[i].InnerText;
//// Hero Card
var heroCard = new HeroCard(
title: title.ToString(),
text: desc.ToString(),
buttons: new CardAction[]
{
new CardAction(ActionTypes.OpenUrl,"LINK",value:LINK)
}
).ToAttachment();
attachments.Add(heroCard);
}
var reply = MessageFactory.Carousel(attachments);
await turnContext.SendActivityAsync(reply);
Update 17 December 2019
I had try using Embedded and Direct Line. But the Error still same.
The Bot is not hosted in SharePoint.
Update 06 January 2020
Its did not work in Azure Bot Services
Based on your description, you can fetch data from it locally. This means your code and logic are all right.
I noticed that your sharePoint URL is : https://mvponduty.sharepoint.com/sites/sg/daw/ and I tried to access it, and also tried to access your whole request URL : https://mvponduty.sharepoint.com/sites/sg/daw/_api/lists/getByTitle('data#work')/items?$filter=Keywords eq 'bloomberg' the response of the two are all 404.
And you said this is an on-prem site , so could you pls have a check that if this site can be reached from a public network?
I assume when you test your code locally, you can access this site as you are in your internal network which will be able to access the on-prem site. However, when you publish your code to Azure, it is not in your internal work anymore: it is in public network so that can't access your on-prem sharePoint site which caused this error.
As we know, bot code is hosted on Azure app service, if this error is caused by the above reason, maybe Azure App Service Hybrid Connections feature will be helpful in this scenario.
ChatBot seems to be working fine? it's sending and receiving messages. There's some code you have that is behaving differently when run locally versus hosted. There's Xml, is it a file or generated? You need check that it's following the same logic and using same data as when it is run locally. Maybe if you paste some of the (non confidential) code where it crashes, we can have more ideas how to help
When you publish your bot, there will be an option as below :
Select Edit App Service Settings. Add only the following details, nothing else :
MicrosoftAppId : <xxxxx>
MicrosoftAppPassword : <xxxxx>
Click Apply, Ok.
Make sure you remove the Microsoft App Id and Microsoft App Password from appsettings.json, so that it works in bot emulator as well.
Now Publish the bot. It will work at both places.
Hope this is helpful.

Skype: Not able to send file attachment to user

I am not able to send file attachment from a bot to a user in Skype. I am using bot builder version 3.5.0.
Below is my code.
ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity reply = activity.CreateReply("blah");
reply.Attachments = new List();
Attachment attach = new Attachment();
attach.ContentType = "application/pdf";
// I can browse the below URL in browser and access the PDF
attach.ContentUrl = "https://test.azurewebsites.net/Image/Test.pdf";
attach.Name = "Test.pdf";
attach.Content = "Test";
attach.ThumbnailUrl = attach.ContentUrl;
reply.Attachments.Add(attach);
await connector.Conversations.ReplyToActivityAsync(reply);
Aside from the dire need to upgrade your version of botbuilder, there is also a sample for this. please refer to it for further guidance. It is located in the botbuilder-samples repo. in the sample they are constructing the attachments very similar to how you are:
private static Attachment GetInternetAttachment()
{
return new Attachment
{
Name = "BotFrameworkOverview.png",
ContentType = "image/png",
ContentUrl = "https://learn.microsoft.com/en-us/bot-framework/media/how-it-works/architecture-resize.png"
};
}
So this is most likely caused by the very outdated version of botbuilder you are using

Power BI Embedded from Xamarin

I'm working to connect to Power BI Embedded from an iOS and Android app written in Xamarin.
I was working through the example for 'App owns data' at: https://learn.microsoft.com/en-us/power-bi/developer/get-azuread-access-token, however, the function UserPasswordCredential is not available to create an AuthenticationContext in the Xamarin version of C#.
My question: Is it a supported scenario to connect to Power BI Embedded from Xamarin. If so, pointers to docs / examples are welcome.
You can find Sample here
public Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
{
var uri = new Uri(returnUri);
var authContext = new AuthenticationContext(authority);
var platformParams = new PlatformParameters(CrossCurrentActivity.Current.Activity);
if (authContext.TokenCache?.ReadItems()?.Any() is true)
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
return authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
}

Push data from a web from into CRM 2015 online

I am trying to create a web from (web application) in Visual Studio 2012 to push data into CRM 2015 online. It keep saying Metadata Contains A Reference That Cannot Be Resolved, when the program tried to call OrgService.
Before that, I create a windows form to do so, it works fine. It can connect to CRM 2015 online and create a new entity record successfully. But when I move the code to Web Application. I does not work.
Code:
private void button1_Click(object sender, EventArgs e)
{
ClientCredentials cre = new ClientCredentials();
cre.UserName.UserName = "MyEmailAddress";
cre.UserName.Password = "Password";
Uri serviceUri = new Uri("https://QA.crm.dynamics.com/XRMServices/2011/Organization.svc");
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);//Error occurs here!!!!!!
proxy.EnableProxyTypes();
IOrganizationService service = (IOrganizationService)proxy;
Entity contact = new Entity("contact");
contact["firstname"] = Convert.ToString(firstname.Text);
contact["lastname"] = Convert.ToString(lastname.Text);
contact["emailaddress1"] = Convert.ToString(email.Text);
contact["mobilephone"] = Convert.ToString(phone.Text);
proxy.Create(contact);
}
Error occurs at:
OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
Is there anyone know what is going on here? I appreciate your help.
Thanks.
Try to use following code for initialization of service proxy:
IServiceManagement<IOrganizationService> orgServiceManagement =
ServiceConfigurationFactory.CreateManagement<IOrganizationService>(new Uri("https://democrm.api.crm5.dynamics.com/XRMServices/2011/Organization.svc"));
AuthenticationCredentials authCredentials = new AuthenticationCredentials();
authCredentials.ClientCredentials.UserName.UserName = _userName;
authCredentials.ClientCredentials.UserName.Password = _password;
AuthenticationCredentials tokenCredentials = orgServiceManagement.Authenticate(authCredentials);
IOrganizationService organizationProxy = new OrganizationServiceProxy(orgServiceManagement, tokenCredentials.SecurityTokenResponse);

Resources