Dynamics crm 2011 addlink with relationship campaignlist_association - linq

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.

Related

What is entity reference and Query Expression ? Please give some simple examples

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;

Undefined property $id for users. But user has ID

I'm attempting to build up a query in Laravel.
I have two tables, with the following attirbuteds
User
id (auto)
name
email
userType
password
TenantPreferance
id (auto)
county
type
user_id (this is the user who created their
preference)
I'm trying to get a collection of data from users of a certain type where id of the user, matches the user_id in preferences.
But I get this error
Undefined property: Illuminate\Database\Eloquent\Builder::$id
$tenants = User::where('userType', 'tenant');
$Prefereances = TenantPreferance::where($tenants->id, $Prefereances->user_id);
$users = $Prefereances->get();
for : $tenants = User::where('userType', 'tenant');
must add:
first()
$tenants = User::where('userType', 'tenant')->first();
or : get()
$tenants = User::where('userType', 'tenant')->get();
You need to actually run the built sql. You also need to get a singular event of it out. So im using first() in this instance. Like this
$tenants = User::where('userType', 'tenant')->first();
$tentants = User::with('TenantPreferance')->where('userType', 'tenant')->get();
where TenantPreferance is the relationship name

Retrieve Customer Entity in Dynamics CRM 2016

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;

How to simple bind joined tables from Entity Data Model 4 (.edmx) to Gridview thru c#?

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();

How to get object collection from another collection?

Suppose I have a collection defined as:
IEnumerable<Employee> Employees;
Entity Employee has property Person.
I have loaded Employees from Ria service including Person with eager-loading.
Now I want to get the collection of Person from Employees, something like
IEnumerable<Person> People = Employees.Person;
How to use Linq to get all Person? any other solution for this case?
Unless I'm missing something, it should be as easy as (assuming Person isn't another collection):
var persons = Employees.Select(e => e.Person);
Try the following
IEnumerable<Employee> collection = ...;
IEnumerable<Person> persons = collection.Select(x => x.Person);

Resources