locked
BING MAP with Opportunity. RRS feed

  • Question

  • Business Goal: I want BING map integration with CRM i need all Opportunity accounts and customers to be mapped(using there address) in the MAP on Dashboard.

    Repro Steps:

    1. I am using Silverlight 4.0 and i am getting all the data of opportunity that is currently in CRM using SAOP.
    2. getting the logical name of the lookup in the entity if the entity is account i run account Retrieve function.
    3. error: retrieve function is not working i want to use refrenceEntity. (Lookup)                    

    Expected Results:

    1. retrieve function must be invoked
    Actual Results:
    1. sb.AppendLine(customerguid.ToString()); is not working. this is just a simple line to be displayed on silverlight

    Environment:

    1. CRM 2011, Opportunity fetch record is working,

    CODE:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Maps.MapControl;
    using SoapForSilverlightSample.ServiceReference2;
    using System.ServiceModel.Description;
    
    
    namespace SoapForSilverlightSample
    {
        using SoapForSilverlightSample.CrmSdk;
        using System.Text;
        using System.Threading;
        using System.Data.Services.Client;
        using SoapForSilverlightSample.ServiceReferenceOdata;
        using System.Windows.Browser;
    
    
        public partial class MainPage : UserControl
        {
            int MaxRecordsToReturn = 50000;
            List<string> address = new List<string>();
    
            public MainPage(App app)
            {
                InitializeComponent();
                app.UnhandledException += new EventHandler<ApplicationUnhandledExceptionEventArgs>(app_UnhandledException);
            }
            void onLoad(string Addr)
            {
                string Address = Addr;
                 
                GeocodeServiceClient myGeoCodeClient = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                // create an event hanler for GeocodeCompleted event as the service would be call asynchronously
                myGeoCodeClient.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(myGeoCodeClient_GeocodeCompleted);
                // create the request and pass the address to it
                GeocodeRequest myGeoCodeRequest = new GeocodeRequest(); myGeoCodeRequest.Credentials = new Microsoft.Maps.MapControl.Credentials();
                myGeoCodeRequest.Credentials.ApplicationId = ((ApplicationIdCredentialsProvider)myMap.CredentialsProvider).ApplicationId;
                myGeoCodeRequest.Query = Address;
    
    
                // Pass the request to GeocodeAsyn method
                myGeoCodeClient.GeocodeAsync(myGeoCodeRequest);
            }
            void myGeoCodeClient_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
            {
                // create a map layer
                MapLayer myMapLayer = new MapLayer();
                myMap.Children.Add(myMapLayer);
                myMap.ZoomLevel = 1;
                // create a location collection class
                LocationCollection myLocationColl = new LocationCollection();
    
                foreach (GeocodeResult gr in e.Result.Results)
                {
                    Pushpin myPushPin = new Pushpin();
                    // set it to first found location
                    myPushPin.Location = gr.Locations[0];
                    // add it to location collection
                    // which would be used to set the map's bound
                    myLocationColl.Add(myPushPin.Location);
                    // Add the drawn point to the route layer.                   
                    myMapLayer.Children.Add(myPushPin);
                    
    
                }
                var bounds = new LocationRect(myLocationColl);
                myMap.SetView(bounds);
            }
            private void ReportError(Exception ex)
            {
                this.ReportMessage("Exception: " + SilverlightUtility.ConvertToString(ex));
            }
    
            private void ReportMessage(string message)
            {
                this.Dispatcher.BeginInvoke(() => ResultsBox.Text = message);
            }
    
            private void app_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
            {
                this.ReportError(e.ExceptionObject);
            }
    
            private void OpportunityList_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    QueryExpression query = new QueryExpression()
                    {
                        EntityName = "opportunity",
                        ColumnSet = new ColumnSet()
                        {
                            Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] {"name","customerid"})
    
                        },
                        Orders = new System.Collections.ObjectModel.ObservableCollection<OrderExpression>(new OrderExpression[]
    				{
    					new OrderExpression() { AttributeName = "name", OrderType = OrderType.Ascending }
    				})
                    };
    
                    query.PageInfo = new PagingInfo { Count = MaxRecordsToReturn, PageNumber = 1, PagingCookie = null };
    
                    OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
                    request["Query"] = query;
    
                    IOrganizationService service = SilverlightUtility.GetSoapService();
    
                    service.BeginExecute(request, new AsyncCallback(OpportunityList_ClickCallback), service);
    
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }
            }
            StringBuilder sb = new StringBuilder();
            private void OpportunityList_ClickCallback(IAsyncResult result)
            {
                try
                {
                    OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
                    EntityCollection results = (EntityCollection)response["EntityCollection"];
    
                    
                    if (results.Entities.Count == 0)
                    { sb.AppendLine("There are no Account records in the system."); }
    
                    foreach (Entity entity in results.Entities)
                    {
                        sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("name")); //"address1_city","address1_line1","address1_postalcode"
                        
                        //sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("opportunityratingcode"));
                        //sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("address1_postalcode"));
    
                        SoapForSilverlightSample.CrmSdk.EntityReference customerId = entity.GetAttributeValue<SoapForSilverlightSample.CrmSdk.EntityReference>("customerid");
                        
                        //sb.AppendLine(customerGuid.ToString());
                        Guid custId = customerId.Id;
                        
                        string logicalName = customerId.LogicalName;
                        string address="Lahore, Pakistan";
                        sb.AppendLine("Account GUID = " + custId);
                        sb.AppendLine("Account Logical Name =" + logicalName);
                        if (logicalName == "account")
                        {
    
                            sb.AppendLine("i am in foreachloop logical Name");
                            getAddressOf(custId);
                        }
                        else
                        {
                            //sb.AppendLine("i am in foreachloop logical Name else");
                            getAddressOf(custId);
                            //address = getAddressOfCustomer(customerGuid);
                        }
                        //MessageBox.Show(customerId.ToString());
                        //sb.AppendLine(customerId);
                        //string address = getAddressOf(customerGuid);
                            //VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
                            ManualResetEvent e = new ManualResetEvent(false);
                            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { onLoad(address); e.Set(); });
                            e.WaitOne();
                            //onLoad(address);
                        
                    }
                    if (results.MoreRecords)
                    {
                        sb.AppendLine("Only the first " + MaxRecordsToReturn + " records were returned.");
                    }
                    this.ReportMessage(sb.ToString());
    
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }
            }
    
            private SynchronizationContext _syncContext;
            private crm5org9a12dContext _context;
            private String _serverUrl;
            private void showErrorDetails(object ex)
            {
            }
            private void getAddressOf(Guid customerguid)
            {
                sb.AppendLine(customerguid.ToString());
    
                
            }
           
    
    
            private void AccountList_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    QueryExpression query = new QueryExpression()
                    {
                        EntityName = "account",
                        ColumnSet = new ColumnSet()
                        {
                            Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] { "address1_city", "address1_line1", "address1_stateorprovince", "address1_country" })
    
                        },
                        Orders = new System.Collections.ObjectModel.ObservableCollection<OrderExpression>(new OrderExpression[]
    				{
    					new OrderExpression() { AttributeName = "address1_city", OrderType = OrderType.Ascending }
    				})
                    };
    
                    query.PageInfo = new PagingInfo { Count = MaxRecordsToReturn, PageNumber = 1, PagingCookie = null };
    
                    OrganizationRequest request = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
                    request["Query"] = query;
    
                    IOrganizationService service = SilverlightUtility.GetSoapService();
    
                    service.BeginExecute(request, new AsyncCallback(AccountList_ClickCallback), service);
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }
            }
    
            private void AccountList_ClickCallback(IAsyncResult result)
            {
                try
                {
                    OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
                    EntityCollection results = (EntityCollection)response["EntityCollection"];
    
                    StringBuilder sb = new StringBuilder();
                    if (results.Entities.Count == 0)
                    { sb.AppendLine("There are no Account records in the system."); }
    
                    foreach (Entity entity in results.Entities)
                    {
                        sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("address1_city")); //"address1_city","address1_line1","address1_postalcode"
                        sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("address1_line1"));
                        //sb.AppendLine("Account Name = " + entity.GetAttributeValue<string>("address1_postalcode"));
                        string address = entity.GetAttributeValue<string>("address1_city");
                        address += ", ";
                        address += entity.GetAttributeValue<string>("address1_stateorprovince");
                        address += ", ";
                        address += entity.GetAttributeValue<string>("address1_country");
                        sb.AppendLine(address);
    
                        //VEMap.Find(what, where, findType, shapeLayer, startIndex, numberOfResults, showResults, createResults, useDefaultDisambiguation, setBestMapView, callback);
                        ManualResetEvent e = new ManualResetEvent(false);
                        System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { onLoad(address); e.Set(); });
                        e.WaitOne();
                        //onLoad(address);
    
                    }
                    if (results.MoreRecords)
                    {
                        sb.AppendLine("Only the first " + MaxRecordsToReturn + " records were returned.");
                    }
                    this.ReportMessage(sb.ToString());
    
                }
                catch (Exception ex)
                {
                    this.ReportError(ex);
                }
            }
    }
    }
    


    Wednesday, September 14, 2011 11:33 AM

Answers

  • Hi

    Are your AccountList_Click and AccontList_ClickCallback functions sitting in a seperate class file?

    If yes you may be getting some kind UI thread error. The problem is you are trying to pass the value to variable or control declared in UI thread, which is not possible.

    You need to remove that line or move the functions to xaml file.

    I hope this helps.


    Amreek singh Senior CRM Consultant CDC Praxa Sydney,Australia http://mscrmshop.blogspot.com/

    • Proposed as answer by Amreek Singh Wednesday, September 14, 2011 1:38 PM
    • Marked as answer by MubasherSharif Thursday, September 15, 2011 2:06 PM
    • Edited by Amreek Singh Thursday, September 15, 2011 2:17 PM
    Wednesday, September 14, 2011 1:37 PM