none
代码该怎么写 RRS feed

  • 问题

  • 我有一个自定义实体,和account实体是多对多关系

    我要通过调用webService添加他们的记录,也就是说添加到他们的中间表里面

    代码改怎么写

    2010年6月3日 8:10

答案

  • sdk:

    // =====================================================================
    // File:	 AssociateEntities.cs
    // Summary: This sample shows how associate two entities.
    // =====================================================================
    //
    // This file is part of the Microsoft CRM V4.0 SDK Code Samples.
    //
    // Copyright (C) Microsoft Corporation. All rights reserved.
    //
    // This source code is intended only as a supplement to Microsoft
    // Development Tools and/or on-line documentation. See these other
    // materials for detailed information regarding Microsoft code samples.
    //
    // THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
    // PARTICULAR PURPOSE.
    //
    // =====================================================================
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using CrmSdk;
    
    namespace Microsoft.Crm.Sdk.Reference.CrmServiceApi.Miscellaneous
    {
      /// <summary>
      /// This sample shows how associate two entities.
      /// </summary>
      class AssociateEntities
      {
        public static bool Run(string crmServerUrl, string orgName)
        {
          bool success = true;
    
          try
          {
            // Set up the CRM Service.
            CrmService service = Microsoft.Crm.Sdk.Utility.CrmServiceUtility.GetCrmService(crmServerUrl, orgName);
    
            #region Setup Data Required for this Sample
    
            // Create a lead.
            lead newLead = new lead();
            newLead.subject = "Lead1";
            newLead.firstname = "Bill";
            newLead.lastname = "G";
            newLead.companyname = "Microsoft";
            Guid leadId = service.Create(newLead);
    
            // Create a contact 
            contact cont = new contact();
            cont.fullname = "Contact1";
            Guid contactId = service.Create(cont);
            
            #endregion
            
            // Create an associateEntities Request.
            AssociateEntitiesRequest request = new AssociateEntitiesRequest();
    
            // Set the id of moniker1 to the id of the lead
            request.Moniker1 = new Moniker();
            //SDK: request.Moniker1.Id = new Guid("B050F053-6968-DC11-BB3A-0003FFBAD37A");
            request.Moniker1.Id = leadId;
            request.Moniker1.Name = EntityName.lead.ToString();
    
            // Set the id of moniker2 to the id of the contact
            request.Moniker2 = new Moniker();
            //SDK: request.Moniker2.Id = new Guid("1DCDEE97-35BB-44BE-8353-58BC36592656");
            request.Moniker2.Id = contactId;
            request.Moniker2.Name = EntityName.contact.ToString();
    
            // Set the relationship name to associate on
            request.RelationshipName = "contactleads_association";
            
            // Execute the request
            service.Execute(request);
    
          
            #region Check Success
    
            // Verify that the contact is associated with the lead
    
            // Create a query expression.
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "lead";
            qe.ColumnSet = new AllColumns();
    
            // Create the link entity from role to systemuserroles.
            LinkEntity le = new LinkEntity();
            le.LinkFromEntityName = "lead";
            le.LinkFromAttributeName = "leadid";
            le.LinkToEntityName = "contactleads";
            le.LinkToAttributeName = "leadid";
            LinkEntity le2 = new LinkEntity();
            le2.LinkFromEntityName = "contactleads";
            le2.LinkFromAttributeName = "contactid";
            le2.LinkToEntityName = "contact";
            le2.LinkToAttributeName = "contactid";
    
            // Create the condition to test the contact ID.
            ConditionExpression ce = new ConditionExpression();
            ce.AttributeName = "contactid";
            ce.Operator = ConditionOperator.Equal;
            ce.Values = new object[] { contactId };
    
            // Add the condition to the link entity.
            le2.LinkCriteria = new FilterExpression();
            le2.LinkCriteria.Conditions = new ConditionExpression[] { ce };
    
            // Add the from and to links to the query.
            le.LinkEntities = new LinkEntity[] { le2 };
            qe.LinkEntities = new LinkEntity[] { le };
    
            // Verify that lead was returned from the query
            BusinessEntityCollection bec = service.RetrieveMultiple(qe);
            if (bec.BusinessEntities.Length == 0)
            {
              success = false;
            }
            else
            {
              lead retrievedLead = (lead)bec.BusinessEntities[0];
              if (retrievedLead.leadid.Value != leadId)
              {
                success = false;
              }
            }
     
            #endregion
    
            #region Remove Data Required for this Sample
    
            // Delete the contact
            service.Delete(EntityName.contact.ToString(), contactId);
    
            // Delete the lead
            service.Delete(EntityName.lead.ToString(), leadId);
    
            #endregion
    
          }
          catch (System.Web.Services.Protocols.SoapException)
          {
            // Perform error handling here.
            throw;
          }
          catch (Exception)
          {
            throw;
          }
    
          return success;
        }
    
      }
    }
    

    2010年6月3日 14:18