通过Silverlight的OData服务访问MS CRM数据 [英] Accessing MS CRM data through OData service from Silverlight

查看:66
本文介绍了通过Silverlight的OData服务访问MS CRM数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动手实践如何通过Silverlight的OData服务访问MS CRM数据.
作为其一部分,我正在同一台设备上练习CRUD(创建,读取,更新和删除)操作.
阅读创建,读取和删除操作不会遇到任何问题.
我面临的唯一问题是读取操作.
让我描述一下我的问题.
可以从执行查询后生成的CRM和URI中正确提取数据,在IE中独立执行时可以正常工作.
当我将获取的数据分配给列表框控件时,它显示为"SilverlightApplication43.CRMOdata.Contact",并且我想显示联系人的某些特定字段,例如名字",但以某种方式我无法执行.
如果您能帮助我,我将不胜感激.


我的代码在这里



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



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>();
        }

    }
}

推荐答案

我想您应该使用datagrid而不是listbox.无论如何,我都会为您提供代码,该代码将在您的列表框中显示CRM联系人的名字

只需在_contacts_LoadCompleted处理程序中添加此代码,然后删除以前的代码即可.


列表<联系人> CRMcontact =新列表<联系人>(_ contacts);
if(CRMcontact.Count> 0&& CRMcontact [0] .AccountId!= null)
{
字符串名= string.Empty;
foreach(CRMcontact中的var CRMCaseTaskActivity)
{
listBox1.Items.Add(CRMCaseTaskActivity.FirstName.ToString());
//listBox1.ItemsSource = CRMCaseTaskActivity.FirstName.ToString();
}
}


我希望这应该起作用
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


这篇关于通过Silverlight的OData服务访问MS CRM数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆