Hi,
I want to retentive the multiple records from lead entity in Online CRM 2016 office 365 and bind this data in to worldview in asp.net . I am using Traget Frame work 4.5.2 also.
But getting as below error:
An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Description;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography.X509Certificates;
using System.Net;
using System.Configuration;
using System.Xml;
using System.IO;
using System.Data;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
using Microsoft.Xrm.Sdk.Client;
namespace LeadCustomer
{
public partial class Lead : PortalPage
{
string Result = string.Empty;
EntityCollection _LeadCollection;
DataSet ds;
DataRow dr;
DataTable dtLead;
Guid _LeadId = new Guid();
string Topic = string.Empty;
string Name = string.Empty;
string JobTile = string.Empty;
string MobilePhone = string.Empty;
string Email = string.Empty;
IOrganizationService _serviceProxy;
protected void Page_Load(object sender, EventArgs e)
{
// Bind The grid by using Lead Data
if (!IsPostBack)
{
BindGrid(_serviceProxy);
}
}
public void BindGrid(IOrganizationService Service)
{
try
{
ds = new DataSet();
string FetchXml =
"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
"<entity name='lead'>" +
"<attribute name='fullname'/>" +
"<attribute name='leadid'/>" +
"<attribute name='subject'/>" +
"<attribute name='mobilephone'/>" +
"<attribute name='jobtitle'/>" +
"<attribute name='emailaddress1'/>" +
"<order attribute='fullname' descending='false'/>" +
"<filter type='and'>" +
"<condition attribute='statuscode' operator='eq' value='1' />" +
"</filter>" +
"</entity>" +
"</fetch>";
using (OrganizationService _orgservice = new OrganizationService(connection))
{
_serviceProxy = (IOrganizationService)_orgservice;
_LeadCollection = _serviceProxy.RetrieveMultiple(new FetchExpression(FetchXml));
if (_LeadCollection.Entities.Count > 0)
{
dtLead = new DataTable();
dtLead.Columns.Add("Topic");
dtLead.Columns.Add("Name");
dtLead.Columns.Add("JobTitle");
dtLead.Columns.Add("MobilePhone");
dtLead.Columns.Add("Email");
}
foreach (Entity LeadRecord in _LeadCollection.Entities)
{
dr = dtLead.NewRow();
if (LeadRecord.Attributes.Contains("fullname"))
{
dr["Name"] = LeadRecord.Attributes["fullname"].ToString();
}
if (LeadRecord.Attributes.Contains("subject"))
{
dr["Topic"] = LeadRecord.Attributes["subject"].ToString();
}
if (LeadRecord.Attributes.Contains("jobtitle"))
{
dr["JobTitle"] = LeadRecord.Attributes["jobtitle"].ToString();
}
if (LeadRecord.Attributes.Contains("mobilephone"))
{
dr["MobilePhone"] = LeadRecord.Attributes["mobilephone"].ToString();
}
if (LeadRecord.Attributes.Contains("emailaddress1"))
{
dr["Email"] = LeadRecord.Attributes["emailaddress1"].ToString();
}
dtLead.Rows.Add(dr);
}
ds.Tables.Add(dtLead);
leadGrd.DataSource = ds;
leadGrd.DataBind();
}
}
catch (Exception Ex)
{
System.Console.WriteLine(Ex.Message.ToString());
}
}
#region Reterive The Organizatin Srvice
//public static IOrganizationService GetCRMService(string ServerURL, string UserName, string Password, string DomainName)
//{
// ClientCredentials credentials = new ClientCredentials();
// credentials.UserName.UserName = UserName; //"Administrator@ManipalDev.onmicrosoft.com";
// credentials.UserName.Password = Password; //"password@123";
// credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
// //credentials.Windows.ClientCredential = new System.Net.NetworkCredential(UserName, Password, DomainName);
// Uri organizationUri = new Uri(ServerURL);
// //Uri homeRealmUri = null;
// OrganizationServiceProxy orgService = new OrganizationServiceProxy(organizationUri, null, credentials, null);
// IOrganizationService _service = (IOrganizationService)orgService;
// return _service;
//}
#endregion
#region Create Lead Reord in CRM
public void CreateLeadRecord(string strTopic, string strName, string strJob, string strPhone, string strEmail)
{
try
{
using (OrganizationService _orgservice = new OrganizationService(connection))
{
_serviceProxy = (IOrganizationService)_orgservice;
Entity _Lead = new Entity("lead");
_Lead.Attributes["fullname"] = strName;
_Lead.Attributes["subject"] = strTopic;
_Lead.Attributes["mobilephone"] = strPhone;
_Lead.Attributes["emailaddress1"] = strEmail;
_Lead.Attributes["JobTitle"] = strJob;
_LeadId = _serviceProxy.Create(_Lead);
if (_LeadId != null)
{
System.Console.WriteLine("Lead Record is created...");
}
else
{
System.Console.WriteLine("Lead Record is not created...");
}
}
}
catch (Exception Ex)
{
System.Console.WriteLine(Ex.Message.ToString());
}
}
#endregion
protected void leadGrd_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
txtTopic.Text = leadGrd.SelectedRow.Cells[1].Text;
txtName.Text = leadGrd.SelectedRow.Cells[2].Text;
txtJobTitle.Text = leadGrd.SelectedRow.Cells[3].Text;
txtMobilePhone.Text = leadGrd.SelectedRow.Cells[4].Text;
txtEmail.Text = leadGrd.SelectedRow.Cells[5].Text;
//string strTopic=leadGrd.SelectedRow[]
// ,string strName,string strJob,string strPhone,string strEmail)
}
catch (Exception Ex)
{
System.Console.WriteLine(Ex.Message.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateLeadRecord(txtTopic.Text, txtName.Text, txtJobTitle.Text, txtMobilePhone.Text, txtEmail.Text);
}
}
}
// Portal Page.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Client;
using Microsoft.Xrm.Client.Services;
namespace LeadCustomer
{
public class PortalPage : System.Web.UI.Page
{
public Microsoft.Xrm.Client.CrmConnection connection =CrmConnection.Parse(GetServiceConfiguration());
#region Private Methods
/// <summary>
/// Gets web service connection information from the app.config file.
/// If there is more than one available, the user is prompted to select
/// the desired connection configuration by name.
/// </summary>
/// <returns>A String containing web service connection configuration information.</returns>
protected static String GetServiceConfiguration()
{
// Get available connection strings from app.config.
int count = ConfigurationManager.ConnectionStrings.Count;
// Create a filter list of connection strings so that we have a list of valid
// connection strings for Microsoft Dynamics CRM only.
List<KeyValuePair<String, String>> filteredConnectionStrings =
new List<KeyValuePair<String, String>>();
for (int a = 0; a < count; a++)
{
if (isValidConnectionString(ConfigurationManager.ConnectionStrings[a].ConnectionString))
filteredConnectionStrings.Add
(new KeyValuePair<String, String>
(ConfigurationManager.ConnectionStrings[a].Name,
ConfigurationManager.ConnectionStrings[a].ConnectionString));
}
// No valid connections strings found. Write out and error message.
if (filteredConnectionStrings.Count == 0)
{
Console.WriteLine("An app.config file containing at least one valid Microsoft Dynamics CRM " +
"connection String configuration must exist in the run-time folder.");
Console.WriteLine("\nThere are several commented out example connection strings in " +
"the provided app.config file. Uncomment one of them and modify the String according " +
"to your Microsoft Dynamics CRM installation. Then re-run the sample.");
return null;
}
// If one valid connection String is found, use that.
if (filteredConnectionStrings.Count == 1)
{
return filteredConnectionStrings[0].Value;
}
// If more than one valid connection String is found, let the user decide which to use.
if (filteredConnectionStrings.Count > 1)
{
Console.WriteLine("The following connections are available:");
Console.WriteLine("------------------------------------------------");
for (int i = 0; i < filteredConnectionStrings.Count; i++)
{
Console.Write("\n({0}) {1}\t",
i + 1, filteredConnectionStrings[i].Key);
}
Console.WriteLine();
Console.Write("\nType the number of the connection to use (1-{0}) [{0}] : ",
filteredConnectionStrings.Count);
String input = Console.ReadLine();
int configNumber;
if (input == String.Empty) input = filteredConnectionStrings.Count.ToString();
if (!Int32.TryParse(input, out configNumber) || configNumber > count ||
configNumber == 0)
{
Console.WriteLine("Option not valid.");
return null;
}
return filteredConnectionStrings[configNumber - 1].Value;
}
return null;
}
/// <summary>
/// Verifies if a connection String is valid for Microsoft Dynamics CRM.
/// </summary>
/// <returns>True for a valid String, otherwise False.</returns>
protected static Boolean isValidConnectionString(String connectionString)
{
// At a minimum, a connection String must contain one of these arguments.
if (connectionString.Contains("Url=") ||
connectionString.Contains("Server=") ||
connectionString.Contains("ServiceUri="))
return true;
return false;
}
#endregion
}
}
Please fix the issue.
//QueryExpression expression = new QueryExpression("lead");
//expression.ColumnSet.AddColumns("fullname", "subject", "mobilephone", "jobtitle", "emailaddress1");







