Showing posts with label Dynamic Entity. Show all posts
Showing posts with label Dynamic Entity. Show all posts

Tuesday, April 28, 2009

Retrieve Multiple Dynamic Entity Records Example

Example:





try      
{
BusinessEntityCollection retrieved = RetreiveMultipleEntityDetails(entityName.Trim().ToLower(), new string[] { filterColumn }, new string[] { filterValue }, null);            if (retrieved != null)                  return (DynamicEntity)retrieved.BusinessEntities[0];            return null;      }
 
Function: 
 
 
 
public BusinessEntityCollection RetreiveMultipleEntityDetails(string entityName, string[] filterColumns, string[] filterValues, string[] ColumnList)        
{
RetrieveMultipleResponse retrieved = new RetrieveMultipleResponse();
try
{
/// Create the query object.
QueryByAttribute query = new QueryByAttribute();
query.EntityName = entityName.Trim().ToLower();
if (ColumnList == null)
{
query.ColumnSet = new AllColumns();
}
else
{
ColumnSet columns = new ColumnSet();
columns.Attributes = ColumnList;
query.ColumnSet = columns;
}
query.Attributes = filterColumns;
query.Values = filterValues;
RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
retrieve.Query = query;
retrieve.ReturnDynamicEntities = true;
 
retrieved = (RetrieveMultipleResponse)Service.Execute(retrieve);
if (retrieved != null)
return retrieved.BusinessEntityCollection;                return null;
}
#region Exception Handling
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new Exception(ex.Detail.InnerText);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
#endregion Exception Handling
}

Wednesday, February 18, 2009

Get Dynamic Entity Primary Attribute Guid Example


Example by CrmSdk


public Guid GetDynamicEntityPrimaryAttributeGuid(DynamicEntity entity)
{
    if (entity != null)
    {
        string strPrimaryKeyAttributeName = entity.Name.ToLower() + "id";
        return ((Microsoft.Crm.Sdk.Key)entity.Properties[strPrimaryKeyAttributeName]).Value;
    }
    return Guid.Empty;
}

Example by Web Service

public Guid GetDynamicEntityPrimaryAttributeGuid(DynamicEntity entity)
{
    if (entity != null)
    {
        string strPrimaryKeyAttributeName = entity.Name.ToLower() + "id";
        for (int i = 0; i < entity.Properties.Length; i++)
        {
            if (entity.Properties[i].Name.ToLower() == strPrimaryKeyAttributeName)
            {
                KeyProperty property = (KeyProperty)entity.Properties[i];
                return property.Value.Value;
            }
        }
    }
    return Guid.Empty;
}

Create Dynamic Entity Record Example


StringProperty firstname = new StringProperty();
firstname.Name = "firstname";
firstname.Value = "Ranjit"; 

StringProperty lastname = new StringProperty();
lastname.Name = "lastname";
lastname.Value = "Singh"; 

// Create the DynamicEntity object.
DynamicEntity contactEntity = new DynamicEntity(); 

// Set the name of the entity type.
contactEntity.Name = EntityName.contact.ToString(); 

// Set the properties of the contact.
contactEntity.Properties = new Property[] {firstname, lastname}; 

// Create the target.
TargetCreateDynamic targetCreate = new TargetCreateDynamic();
targetCreate.Entity = contactEntity; 

// Create the request object.
CreateRequest create = new CreateRequest(); 

// Set the properties of the request object.
create.Target = targetCreate; 

// Execute the request.
CreateResponse created = (CreateResponse) service.Execute(create);

Monday, February 16, 2009

Dynamic Entity Add Attribute Helper Methods

Examples: Calling the below methods:

string strDaysAttributeName = "cap_actualeffortdays";
string strHoursAttributeName = "cap_actualefforthours";
 
if (ObjProject.Properties.Contains(strDaysAttributeName))
    ((CrmNumber)ObjProject.Properties[strDaysAttributeName]).Value = iDays;
else
    CrmHelper.AddNumberProperty(ObjProject, strDaysAttributeName, iDays);

 
if (ObjProject.Properties.Contains(strHoursAttributeName))
    ((CrmNumber)ObjProject.Properties[strHoursAttributeName]).Value = iHours;
else
    CrmHelper.AddNumberProperty(ObjProject, strHoursAttributeName, iHours);


public static void AddStringProperty(DynamicEntity entity, string strColumnName, string DefaultValue)
{
    #region If Column attribute not in entity Collection 

        if (!entity.Properties.Contains(strColumnName))
        {
            StringProperty AttributeContactID = new StringProperty();
            AttributeContactID.Name = strColumnName;
            AttributeContactID.Value = DefaultValue;
        }
    #endregion If Column attribute not in entity Collection
}

 

public static void AddNumberProperty(DynamicEntity entity, string strColumnName, int DefaultValue)
{
    #region If Column attribute not in entity Collection 

        if (!entity.Properties.Contains(strColumnName))
        {
            CrmNumberProperty AttributeID = new CrmNumberProperty();
            AttributeID.Name = strColumnName;
            AttributeID.Value = new CrmNumber(DefaultValue);
        }
    #endregion If Column attribute not in entity Collection
}

Update Dynamic Entity Record Example


public static bool UpdateDynamicEntity(ICrmService Service, DynamicEntity entityName)
{
try
{
    // Create the update target.
    TargetUpdateDynamic updateDynamic = new TargetUpdateDynamic(); 

    // Set the properties of the target.
    updateDynamic.Entity = entityName; 

    //   Create the update request object.
    UpdateRequest update = new UpdateRequest(); 

    //   Set request properties.
    update.Target = updateDynamic; 

    //   Execute the request.
    UpdateResponse updated = (UpdateResponse)Service.Execute(update);

    return true;
}
catch (System.Web.Services.Protocols.SoapException ex)
{                
    return false;
}
catch (Exception ex)
{                
    return false;
}
}

Retrieve Dynamic Entity Records Example



DynamicEntity ObjProject = GetDynamicEntity(crmService, "new_timesheet", new Guid("{21CCFA77-FFF8-DD11-AE96-0003FFD751E0}"));


public static DynamicEntity GetDynamicEntity(ICrmService crmService, string strEntityName, Guid EntityGuid)
{
try
            {
#region Retrieve Entity Record Dynamically
// Create the retrieve target.
                TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic();
// Set the properties of the target.
                targetRetrieve.EntityName = strEntityName;
targetRetrieve.EntityId = EntityGuid;


// Create the request object.
                RetrieveRequest retrieve = new RetrieveRequest();


// Set the properties of the request object.
                retrieve.Target = targetRetrieve;
retrieve.ColumnSet = new AllColumns();


// Indicate that the BusinessEntity should be retrieved as a DynamicEntity.
                retrieve.ReturnDynamicEntities = true; 


// Execute the request.
                RetrieveResponse retrieved = (RetrieveResponse)crmService.Execute(retrieve);


// Extract the DynamicEntity from the request.
                DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity;
return entity;


#endregion
            }
catch (System.Web.Services.Protocols.SoapException ex)
{
throw new System.Exception(ex.Detail.InnerText);
}
catch (Exception ex)
{
throw new System.Exception(ex.Message);
}
}

Sunday, February 15, 2009

Working with Dynamic Entity

The DynamicEntity feature lets you generically access the data in any attribute on any entity in the system. In order to develop against the Microsoft CRM SDK, you must generate and consume a WSDL during code compilation. Because you must do this against a specific instance of the application, the only entities available are the core set included in the initial installation of Microsoft CRM. The WSDL does contain entity and attribute customizations but it is recommended that your code works with a clean installation. See Using the Microsoft CRM WebServices.


The DynamicEntity class is also used in plug-ins / callouts so that your callout code can work generically with any attribute on any entity without a re-compile.


The DynamicEntity class does not provide a name-base indexer, however, you can write your own. A simple approach to this problem is to loop through the dynamic entities property bag until you find the appropriate property.