Forums

Forums / Developing Portals / Leveraging The Portal Connector in Code behind

Leveraging The Portal Connector in Code behind

Thread is closed for posting
6 posts, 2 answered
  1. salzheimer
    salzheimer avatar
    7 posts
    Registered:
    31 Jul 2015
    17 Sep 2015
    Link to this post
    I am just getting started with The Portal Connector and have looked for code samples but have not located any. Sitefinity's documentation and forums have been a great help in solving design issues. I am hoping TPC's support would be just as helpful.  

    This is the scenario: I have a complex event registration process that will temporarily store user input in Sitefinity. Upon staff verification the information would then be inputted into CRM. I think creating the entities in code would more sense then using The Portal Connector GUI designer. In addition I would like to leverage TPC the the admin panel for this widget, ideally using MVC for the user display. 

    Current Setup: SF 8.0.5700.0
  2. Missing user
    Missing user avatar
    Answered
    15 Oct 2015
    Link to this post
    Hello, 
    You will be able to build the functionality you need using the Portal Connector API. CrmConnectionManager in the Crm namespace has the functionality you need and should be a good starting point.  The code below will give you some idea on how to create, retrieve and delete data from CRM using API. If you have any further questions please contact TPC support team. We will be glad to assisst you.

    ...
    
    using Microsoft.Xrm.Sdk;
    using pavliks.PortalConnector.Crm;
    using pavliks.PortalConnector.Crm.Attributes;
    using pavliks.PortalConnector.Crm.Connection; ...

    public Guid CreateContact(string firstName, string lastName, string email)

    Entity contactEntity = new Entity("contact");
    contactEntity["firstname"] = firstName;
    contactEntity["lastname"] = lastName;
    contactEntity["emailaddress1"] = email;

    using (var connManager = new CrmConnectionManager())
    {
    ICrmConnection connection = connManager.Connection;
    if (connection is CrmEmptyConnection)                
    throw new Exception("Unable to connect to Dynamics CRM.");
    else 
    return connection.Create(contactEntity);
    }

    return Guid.Empty;

    }

    public void LoadContacts()

    EntityCollection data;

    // Fetch XML for retrieving contacts
    string fetchXML = "<fetch version=\"1.0\" output-format=\"xml-platform\" mapping=\"logical\"><entity name=\"contact\"><attribute name=\"firstname\" /><attribute name=\"lastname\" /><attribute name=\"emailaddress1\" /><attribute name=\"contactid\" /></entity></fetch>";

    using (var connManager = new CrmConnectionManager())
    {
    ICrmConnection connection = connManager.Connection;
    if (connection is CrmEmptyConnection)                
    throw new Exception("Unable to connect to Dynamics CRM.");
    else 
    data = connManager.Connection.RetrieveMultiple(new FetchExpression(fetchXML));

    }

    if (data != null)
    foreach (Entity entity in data.Entities)
    {
    // some processing here
    }
    }



    public void DeleteContact(Guid entityID, string entityName)
    {
    using (var connManager = new CrmConnectionManager())
    {
    if (connManager.Connection is CrmEmptyConnection)                
    throw new Exception("Unable to connect to Dynamics CRM.");
    else 
    connManager.Connection.Delete(entityName, entityID);
    }
    } ...
  3. salzheimer
    salzheimer avatar
    7 posts
    Registered:
    31 Jul 2015
    16 Mar 2016
    Link to this post
    The code sample was very helpful and after a few months I was able to return to this. Now I am receiving the following error when updating: 

    System.NullReferenceException: Microsoft Dynamics CRM has experienced an error. Reference number for administrators or support: #746B78C3

     public bool UpdateConstituent(ConstituentModel constituent, Guid entityId)         {
                 using ( var connManager = new CrmConnectionManager() )
                {                 ICrmConnection connection = connManager.Connection;
                     if ( connection is CrmEmptyConnection )
                         throw new Exception("Unable to connect to Dynamics CRM.");  
                   else       
                      {            
                     try                   
                         {
                                                     Entity constituentEntity = connManager.Connection.Retrieve("contact", entityId, new ColumnSet());                         
                            constituentEntity["contactid"] = constituent.ConstituentId;
                             constituentEntity["firstname"] = constituent.FirstName;
                             constituentEntity["middlename"] = constituent.MiddleName;
                             constituentEntity["lastname"] = constituent.LastName;
                             constituentEntity["salutation"] = constituent.Salutation;
                              constituentEntity["donotbulkemail"] = constituent.DoNotBulkEmail;
                             constituentEntity["donotemail"] = constituent.DoNotEmail;
                             constituentEntity["donotphone"] = constituent.DoNotPhone;
                             constituentEntity["donotpostalmail"] = constituent.DoNotPostalMail;
                            //contact     
                             constituentEntity["telephone1"] = constituent.BusinessPhone;
                             constituentEntity["telephone2"] = constituent.HomePhone;
                             constituentEntity["emailaddress1"] = constituent.EmailAddress1;
                             constituentEntity["emailaddress2"] = constituent.EmailAddress2;
                             constituentEntity["modifiedon"] = DateTime.Now;

                             if ( constituentEntity.EntityState == null || 
                                      constituentEntity.EntityState == EntityState.Changed )                         
                            {
                                                            connection.Update(constituentEntity);
                                                            return true;
                            }                         else
                             {
                                return false;                         
                             }
                          }                     catch ( Exception ex )   
                                                 {
                                                 return false;                    
                                                 }
                 }

    I am not sure what is wrong as you can tell the error is not very clear on the issue. Am I using the correct update method in The Portal Connector api? 

    Any insight is helpful.

    Thanks,

    Scott 
    Last modified on 16 Mar 2016 15:03 by salzheimer
  4. Missing user
    Missing user avatar
    Answered
    17 Mar 2016 in reply to salzheimer
    Link to this post
    Hello Scott,

    The error message you're receiving is coming from CRM. Most likely one (or more) of the filelds in your constituent entity has been customized to not allow nulls. You should check the CRM configuration and add some condition(s)  in your code checking for null values in your model similar to the code below:
    ...
    if (constituent.BusinessPhone!=null) {
        constituentEntity["telephone1"] = constituent.BusinessPhone;
    }

    Otherwise your code looks alright. I would remove the "else" keyword before the try block as it is redundant.
     
    Kind Regards,

    Boris
  5. salzheimer
    salzheimer avatar
    7 posts
    Registered:
    31 Jul 2015
    22 Mar 2016 in reply to Missing user
    Link to this post
    Boris thank you for the insight. Turns out I was setting the contact id field with an empty Guid and not the entity I retrieved. 
  6. Missing user
    Missing user avatar
    22 Mar 2016 in reply to salzheimer
    Link to this post
    I am glad to hear you've found a solution. You don't really need to set the Id of the entity because you're updating. Also the Guid entityId parameter is redundant since you have the Id in the constituent.ConstituentId. 

    Kind Regards,
    Boris
6 posts, 2 answered