retrieve,update,assign,create related object records in Microsoft CRM 2011

Hi,
here is a sample of how to obtain records from CRM.

You need prebuild classes from current Microsoft CRM 2011 deployment.to do this use crmsvcutil.exe which comes from CRM SDK (...SDK\bin\).
Built Class includes both standart Entities and Custom Entities.
Usage : crmsvcutil.exe /url:http(s)://crm.???.com(:444)/organization_name/XRMServices/2011/Organization.svc /out:"\OrganizationCodes.cs " /username:muhammet.atalay /password:ILoveCrm /domain:??? /serviceContextName:...ServiceContext
Do not forget to add OrganizationCodes.cs into your project.

ServiceContext uses as below while retrieving any records via LinQ.
ServiceContext context;
           context = new ServiceContext((IOrganizationService)_serviceProxy);


List contacts = context.ContactSet.Where(c => c.fieldName == criteria).ToList();
you can create a record as follow;
Contact conn = new Contact();
set fields
_serviceProxy.Create(booking);
assign record to a user while creating record
example:
conn.OwnerId = new EntityReference("systemuser", new Guid("7D1423DC-E912-E211-AE96-005056856784"));
assign existing record to a user
Create the Request Object and Set the Request Object's Properties
AssignRequest assign = new AssignRequest
{
Assignee = new EntityReference(SystemUser.EntityLogicalName,
new Guid("7D1423DC-E912-E211-AE96-005056820011")),
Target = new EntityReference(booking.LogicalName, booking.oti_bookingId.Value)
};
// Execute the Request
_serviceProxy.Execute(assign);
update record,you may use context instead ServşceProxy
context.UpdateObject(booking);
context.SaveChanges();

make a relation with other record
if record is newly created
Relationship rsRelation = new Relationship("name of the relation");
EntityCollection relatedEntities = new EntityCollection
{
EntityName = con.LogicalName,
Entities = {
con
}
};

booking.RelatedEntities.Add(rsTourist, relatedEntities);

if the record exists,
Relationship rs = new Relationship("name of the relation");

get Property for the relation
context.LoadProperty(entity_instance, "name of the relation");

if (entity_instance.RelatedEntities.Count > 0)
{
DataCollection relationcoll = tour.RelatedEntities[rs].Entities;
if (!relationcoll.Contains(related_entity_instance))
{
entity_ instance.RelatedEntities[rs].Entities.Add(related_entity_instance);
context.UpdateObject(tour);
context.SaveChanges();
}
}
good luck.

Comments

Popular posts from this blog

Complex Query in QueryExpression in Microsoft CRM 2011

Exception caught instantiating TERADATA report server extension SQL Reporting Services

Microsoft Power Apps Portal integration with Dynamics 365 CE On-Premise - Step By Step Guide