Click here to Skip to main content
15,891,749 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I was doing hands-on regarding accessing MS CRM data through OData service from Silverlight.
As a part of that I was practicing CRUD (Create, Read, Update and Delete) operations on the same.
I don’t face any problem reading create, read and delete operations.
Only problem I face is with read operations.
Let me describe my problem.
Data is fetched properly from CRM and URI generated after execution of query, works fine when executed independently in IE.
When I assign fetched data to listbox control it shows ‘SilverlightApplication43.CRMOdata.Contact’ and I want to show some specific Fields from contact such as First Name but somehow I am unable to do it.
I would appreciate if someone of you could help me.


My Code goes here



C#
namespace SilverlightApplication43
{
    public partial class MainPage : UserControl
    {
        public  DataServiceCollection<Contact> _contacts;
        System.Windows.Data.Binding list;
        (myorgname)Context context;
        public MainPage()
        {
            InitializeComponent();
            MainPage_Loaded(this,new RoutedEventArgs() );

        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            var serviceUri = "http://crmdev:5555/(Myorgname)/XRMServices/2011/OrganizationData.svc/";

            (Myorgname)Context ctx = new (Myorgname)Context(new Uri(serviceUri));

            //to avoid cross domain issues
            ctx.HttpStack = System.Data.Services.Client.HttpStack.ClientHttp;
            ctx.UseDefaultCredentials = false;
            ctx.Credentials = new NetworkCredential("administrator", "somepassword", "somedomain");

            context = ODataServiceFactory.GetInstance<(Myorgname)Context>();

            var query = from c in context.ContactSet
                        select c;

            _contacts = null;
            _contacts = new DataServiceCollection<Contact>();

            _contacts.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(_contacts_LoadCompleted);

            _contacts.LoadAsync(query);
        }
        void _contacts_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            var binding = (DataServiceCollection<Contact>)sender;

            listBox1.ItemsSource = _contacts;
            //I am doubtful about both statements
            listBox1.ItemsSource = _contacts.ToList<Contact>();
        }

    }
}
Posted
Updated 27-Nov-11 18:56pm
v2
Comments
[no name] 28-Nov-11 0:56am    
EDIT: updated <pre> tag
tcscode 4-Feb-13 6:47am    
Hi it seems good , i need refernce for the ODataServiceFactory
how do i get it ?

1 solution

I guess you should use datagrid instead of listbox. Anyways i will give you code which will display firstname from CRM contacts in your list box

simply add this code in _contacts_LoadCompleted handler and remove previous code.


List<Contact> CRMcontact = new List<Contact>(_contacts);
if (CRMcontact.Count > 0 && CRMcontact[0].AccountId != null)
{
string firstname = string.Empty;
foreach (var CRMCaseTaskActivity in CRMcontact)
{
listBox1.Items.Add(CRMCaseTaskActivity.FirstName.ToString());
//listBox1.ItemsSource = CRMCaseTaskActivity.FirstName.ToString();
}
}


I hope this should work
 
Share this answer
 
v2
Comments
Lousy Programmer 28-Nov-11 4:54am    
Thnx Man it worked...If possible give code with data grid
MorisAmini 1-Aug-13 18:55pm    
Would you please inform me about how you have instanciated the ODataServiceFactory
in
context = ODataServiceFactory.GetInstance<(Myorgname)Context>();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900