What is EntityReference and QueryExpression? Please give me some simple examples.
EntityReference
It is used for lookup fields in
365 – e.g. to link records via a 1 to many relationship. The lookup
field is shown on the ‘child’. You need to specify the ‘parent’ entity
type and record id.
For example if you are creating a accountand want to set the primary contact.
Entity account = new Entity("account");
account["name"] = "James Account";
account["primarycontactid"] = new EntityReference("contact", contactId);
service.Create(account);
QueryExpression
QueryExpression provides an object model to construct a query. Queries
can also be created using FetchXML, a proprietary XML based query
language.
For example of you wanted to retrieve all contacts full name and telephone number.
QueryExpression query = new QueryExpression()
{
Distinct = false,
EntityName = Contact.EntityLogicalName,
ColumnSet = new ColumnSet("fullname", "address1_telephone1"),
};
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
Related
I got problems with #Query annotation or Query query = new Query() - I can't include/exclude some fields.
I find code samples like:
#Query(value = "{'id': ?0}",fields = "{'id':1}")
User findUserById(String id);
Result should be user with only one field -> id but its showing other fields to..
So I found other samples like:
Query query = new Query();
query.fields().include("id");
query.addCriteria(Criteria.where("id").is(id));
User one = mongoTemplate.findOne(query, User.class);
Same result there..
Any ideas?
Integer findUserById(String id);
Use the DataType of Id [like Integer] to get only id.
Change datatype of according the requirement/usecase
Entity contact = new Entity("contact");
contact.Attributes.Add("fullname", "h api test");
contact.Attributes.Add("emailaddress1", "hh#devenv1.local");
contact.Attributes.Add("telephone1", "1");
contact.Attributes["parentcusotmerid"] = new EntityReference("Organization", );
Guid contactId = m_OrgServ.Create(contact);
Console.WriteLine(contactId);
The lookup field I want to set
The logicalname of the lookup field is parentcusotmerid, and
m_OrgSerc.create
is basically
Service.create
I am setting attribute values for the fields, it works fine for normal text boxes where I am entering values, however for lookup values it doesn't work. I know lookup fields are of type EntityReference, so I need to know the LogicalName of the entity the lookup is pointing and the Id of the record.
I have tried it but its asking for the GUID of the Organization field now, so I'm not sure if I am going about it the right way?
You cannot set "parentcustomerid" to organization. It's special reference field that takes either Account or Contact entity reference as parameter.
If you want to set it you go like this
contact.Attributes["parentcusotmerid"] = new EntityReference("account", Guid.NewGuid());
or
contact.Attributes["parentcusotmerid"] = new EntityReference("contact", Guid.NewGuid());
where Guid.NewGuid() is Guid of your Account or Contact that you want to reference
As we know Dynamics CRM has a specific attribute value: Customer. This value combines the Client and Account entity, but I'm blind or MSDN doesn't have specification about retrieving this field in query.
For example:
QueryByAttribute query = new QueryByAttribute(entName);
query.ColumnSet = new ColumnSet(new String[] { searchAttr });
query.Attributes.Add(searchAttr);
query.Values.Add(searchValue);
EntityCollection retrived = service.RetrieveMultiple(query);
This code accepts entity name and searches the attribute's name and value, but when I run it I don't know which type of entity I get from my DataSouce: Client or Account.
So the question is: is it possible to retrieve Customer entity in one query?
No, you must first know which entity you are trying to retrieve.
Get the value held within the Customer field as an EntityReference:
var customer = entity.GetAttributeValue<EntityReference>("customerid");
Get the LogicalName of the EntityReference:
var customerEntity = customer.LogicalName;
I have these two tables in MySql:
[Person]
PersonId
NameFirst
NameLast
[Email]
EmailId
PersonId
EmailAddress
In VS2010, I added a new item, ADO.NET Entity Data Model. I connect to MySql and "drag-and-drop" my the two tables into the .edmx designer. Great! It has relationships and all.
Now I want to bind something like this to a Gridview WITHOUT using an EntityDataSource control:
SELECT * FROM Person INNER JOIN Email ON Person.PersonId = Email.PersonId
How am I to do this programmatically using the modern approach? I noticed in my .edmx, the tables have "Navigation Properties" and the related tables are listed there. I just don't know the concept and syntax to use it since my skills are still "DataSet-SQL Queries-DataAdapter" based.
You can start with something like this:
var query = from x in Context.Persons // Entity set on your context
from y in x.Emails // Navigation property
select new PersonProjection // Your custom class for flattened result
{
PersonId = x.PersonId,
FirstName = x.NameFirst,
LastName = x.NameLast,
EmailId = y.EmailId,
EmailAddress= y.EmailAddress
};
gridView.DataSource = query.ToList();
gridView.DataBind();
Relationship relation = new Relationship("campaignlist_association");
Entity campaign = (from c in orgServiceContext.CreateQuery("campaign")
select c).FirstOrDefault<Entity>();
foreach (Guid id in listsMarketingGuid)
{
Entity list = (from l in orgServiceContext.CreateQuery("list")
where l["listid"].Equals(id)
select l).FirstOrDefault<Entity>();
orgServiceContext.AddLink(campaign, relation, list);
orgServiceContext.AddLink(list, relation, campaign);
}
orgServiceContext.SaveChanges();
I would like to add a relation between a marketing list and a campaign but when the SaveChanges statment is executed I got an error "Associate is not supported for CampaignItem".
Do you any idea ?
Thanks
use Associate method as for building relationship:
_service.Associate(EntityLogicalName,EntityId,relationship,relatedEntities);
where EntityLogicalName is name of entity
EntityId is id of entity
relationship:wat kind of relationship
relatedentities:to which entiites u want to build relation of above entity.
it needs to call method AddItemCampaignRequest
I got the error
"Associate is not supported for CampaignItem"
when trying to associate product to campaign. This worked for me:
var request = new AddItemCampaignRequest
{
CampaignId = yourCampaign.Id,
EntityId = productToAssociate.Id,
EntityName = ProductEntity.EntityLogicalName,
};
_serviceProxy.Execute(request);
Creds to Mitch Milam
Hope this will help someone.