locked
Microsoft Dynamic CRM 2011 RRS feed

  • Question

  • Hi,

    I am developing a sample C#.Net web application which enters data into custom entity.

    I am doing this using web service. But i am getting following error

    ====================

    Cannot apply indexing with [] to an expression of type 'CRM.Entity'    E:\CRM Example Projects\CRM Example\CRM Test Example-Web Service\Default.aspx.cs    28    9    E:\...\CRM Test Example-Web Service\
    ====================

    at

     protected void Page_Load(object sender, EventArgs e)
        {
            using (var xrmServiceClient = InstantiateXrmService())
            {
                var timesheetid = CreateCrmTimesheet(xrmServiceClient);
            }
        }
     
        private static Guid CreateCrmTimesheet(OrganizationServiceClient xrmServiceClient)
        {
            var timesheet = new Entity
            {
                LogicalName = "new_timesheet"
            };
     
            timesheet["name"]= "Test timesheet";
     
            return xrmServiceClient.Create(timesheet);
        }
     
        private static OrganizationServiceClient InstantiateXrmService()
        {
            var xrmServiceClient = new OrganizationServiceClient();
     
            // Uncomment the following line if you want to use an explicit CRM account to make the service calls
            xrmServiceClient.ClientCredentials.Windows.ClientCredential = new NetworkCredential("**""**""**");
     
            return xrmServiceClient;
        }

    How can i solve it??


    Regards, Milan Hingu

    Tuesday, March 27, 2012 11:24 AM

Answers

  •  credentials.UserName.UserName = "Your User Name";
    
                credentials.UserName.Password = "Your Password ";
               deviceCredentials.UserName.UserName = ConfigurationManager.AppSettings["dusername"];
                deviceCredentials.UserName.Password = ConfigurationManager.AppSettings["duserid"]; organizationUri = new Uri("Your Organization URI"); homeRealmUri = null; using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, deviceCredentials)) { IOrganizationService service = (IOrganizationService)serviceProxy; Entity timesheet = new Entity("new_timesheet"); timesheet["name"]= "test time sheet"; service.Create(timesheet); }

    Copy and paste the code replace your credentials.
    Note URI can be located at developer resources.

    If you need the code please send me an Email i will send you complete web application


    Thanks and Regards Blog: http://mubashersharif.blogspot.com
    Email: mubasher.sharif@gmail.com
    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    _____________________________________________________________
    Mubasher




    Tuesday, March 27, 2012 12:17 PM

All replies

  • Hi

    Instead of declaring timesheet as var,

    try Entity timesheet = new Entity

    I hope this helps.


    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer". Amreek Singh Senior CRM Consultant CDC Praxa Sydney, Australia http://mscrmshop.blogspot.com http://crm2011usersettings.codeplex.com

    • Proposed as answer by Amreek Singh Tuesday, March 27, 2012 11:42 AM
    Tuesday, March 27, 2012 11:42 AM
  • Hi

    I have not worked much with WCF endpoint (OrganizationServiceClient), but I guess the reason is that by default "Entity" in WCF endpoint does not include defintion for Attributcollection.

    So I guess you need to over-ride the Entity method in Reference.cs by adding a helper class.

    Here is a nice post by Daniel Cai on how to do it.

    http://danielcai.blogspot.se/2011/06/step-by-step-walkthrough-use-crm-2011.html 

    See if this helps.

    Tuesday, March 27, 2012 11:48 AM
  • Amreek

    No means to offend you, I believe the approach followed by Milan is also OK. I have seen examples where you can create entity instances that way.

    But the WCF endpoint in CRM 2011, has some limitations and to overcome it we need to over-ride exisitng classes in Reference.cs. This can be done by adding helper classes in solution.

    Tuesday, March 27, 2012 11:51 AM
  • Still it not resolved...

    Plus i dont find reference.cs file...


    Regards, Milan Hingu

    Tuesday, March 27, 2012 12:11 PM
  •  credentials.UserName.UserName = "Your User Name";
    
                credentials.UserName.Password = "Your Password ";
               deviceCredentials.UserName.UserName = ConfigurationManager.AppSettings["dusername"];
                deviceCredentials.UserName.Password = ConfigurationManager.AppSettings["duserid"]; organizationUri = new Uri("Your Organization URI"); homeRealmUri = null; using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, deviceCredentials)) { IOrganizationService service = (IOrganizationService)serviceProxy; Entity timesheet = new Entity("new_timesheet"); timesheet["name"]= "test time sheet"; service.Create(timesheet); }

    Copy and paste the code replace your credentials.
    Note URI can be located at developer resources.

    If you need the code please send me an Email i will send you complete web application


    Thanks and Regards Blog: http://mubashersharif.blogspot.com
    Email: mubasher.sharif@gmail.com
    If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    _____________________________________________________________
    Mubasher




    Tuesday, March 27, 2012 12:17 PM
  • Ya.. I have tried that.

    I downloaded a sample console application. In that its running correct.

    but i have made a new web application ,in that its not working...

    In downloaded console ..its going perfect.

    But i want in web application.


    Regards, Milan Hingu

    Tuesday, March 27, 2012 12:22 PM
  • Hi

    You do not need to do any changes in Reference.cs actually.

    You need to add a new class file (same namespace as that of your servicename).

    In the link I provided, the name given to this new class is XrmExtensionMethods.cs.

    Copy paste the part having Entity class.

    I think you can get it working.

    Other way is obviously, to do it through Entity abx = new Entity();

    Good luck

    Tuesday, March 27, 2012 1:39 PM
  • Hi MubasherSharif,

    my email id is milan.hingu@indusa.com

    Tuesday, April 3, 2012 5:05 AM