locked
Update Records in CRM 2011 using LINQ RRS feed

  • Question

  • I am trying to Update records in the OpportunitySet using LINQ. Right now I am pulling all the data down and they are being Databound to fields in a Grid. This is what I have so far:

       Uri organizationUri = new Uri("http://servername/XRMServices/2011/Organization.svc");
      Uri homeRealmUri = null;
      ClientCredentials credentials = new ClientCredentials();
      credentials.Windows.ClientCredential = new System.Net.NetworkCredential("user", "pass", "domain");
      OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
      // Get the IOrganizationService
      IOrganizationService orgService = (IOrganizationService)orgProxy;
      //Get OrganizationServiceContext -the organization service context class implements the IQueryable interface and
      //a .NET Language-Integrated Query (LINQ) query provider so we can write LINQ queries against Microsoft Dynamics CRM data.
      OrganizationServiceContext orgServiceContext = new OrganizationServiceContext(orgService);
    
    
      var linqQuery = (from r in orgServiceContext.CreateQuery("opportunity")
               join a in orgServiceContext.CreateQuery("account") on ((EntityReference)r["accountid"]).Id equals a["accountid"]
               join c in orgServiceContext.CreateQuery("contact") on ((EntityReference)r["new_contact"]).Id equals c["contactid"]
               where ((EntityReference)r["new_channelpartner"]).Id.Equals(new Guid("c55c2e09-a3be-e011-8b2e-00505691002b"))
               select new
               {
                 OpportunityId = !r.Contains("opportunityid") ? string.Empty : r["opportunityid"],
                 CustomerId = !r.Contains("customerid") ? string.Empty : ((EntityReference)r["customerid"]).Name,
                 Priority = !r.Contains("opportunityratingcode") ? string.Empty : r.FormattedValues["opportunityratingcode"],
                 ContactName = !r.Contains("new_contact") ? string.Empty : ((EntityReference)r["new_contact"]).Name,
                 Source = !r.Contains("new_source") ? string.Empty : r["new_source"],
                 CreatedOn = !r.Contains("createdon") ? string.Empty : r["createdon"],
                 State = !a.Contains("address1_stateorprovince") ? string.Empty : a["address1_stateorprovince"],
                 Zip = !a.Contains("address1_postalcode") ? string.Empty : a["address1_postalcode"],
                 Eval = !r.Contains("new_colderevaluation") ? string.Empty : r.FormattedValues["new_colderevaluation"],
                 EvalVal = !r.Contains("new_colderevaluation") ? string.Empty :((OptionSetValue)r["new_colderevaluation"]).Value.ToString(),
                 DistributorName = !r.Contains("new_channelpartner") ? string.Empty : ((EntityReference)r["new_channelpartner"]).Name,
                 ContactStreetAddress = !c.Contains("address1_line1") ? string.Empty : c["address1_line1"],
                 ContactCity = !c.Contains("address1_city") ? string.Empty : c["address1_city"],
                 ContactState = !c.Contains("address1_stateorprovince") ? string.Empty : c["address1_stateorprovince"],
                 ContactZip = !c.Contains("address1_postalcode") ? string.Empty : c["address1_postalcode"],
                 ContactPhone = !c.Contains("telephone1") ? string.Empty : c["telephone1"],
                 ContactMobilePhone = !c.Contains("mobilephone") ? string.Empty : c["mobilephone"],
                 ContactEmail = !c.Contains("emailaddress1") ? string.Empty : c["emailaddress1"],
                 Notes = !r.Contains("new_rsmnotes") ? string.Empty : r["new_rsmnotes"],
                 EstimatedCloseDate = !r.Contains("estimatedclosedate") ? string.Empty : r["estimatedclosedate"],
                 MaturityValue = !r.Contains("estimatedvalue") ? string.Empty : ((Money)r["estimatedvalue"]).Value.ToString()
               });
    
      grdLeadList.DataSource = linqQuery;
    
      grdLeadList.DataBind();
    


    This code pulls all my information down and then assign it to controls using something like:

    <asp:TestBox ID="Label1" runat="server" Text='<%# DataBinder.Eval( Container, "DataItem.ContactEmail" ) %>' />

    But now I am trying to make it so when you click a save button it will save any changes in the textbox. How would I do that using LINQ?

    Thanks!

    Sunday, August 28, 2011 6:37 PM

Answers

  • Datasets you generate using the CRM Service Context CreateQuery method are one-way only.  They will not support dataset updates the way Linq to SQL does.  The CRM Service interface only supports single entity updates.  If you create a call-back method from your grid and pass it the guid of the line you are updating you should be able to do it through the Service Update statement.
    • Marked as answer by CrazyeD1583 Wednesday, August 31, 2011 3:01 AM
    Monday, August 29, 2011 12:49 PM