If it’s plugin, you can query the Language
entity using service.RetrieveMultiple
by filtering with Name = “Spanish”
ie Name = rawStudent["Language"]
to get the GUID. You can use fetchxml or QueryExpression. Read more
private static EntityCollection GetEntityCollection(IOrganizationService service, string entityName, string attributeName, string attributeValue, ColumnSet cols)
{
QueryExpression query = new QueryExpression
{
EntityName = entityName,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions =
{
new ConditionExpression
{
AttributeName = attributeName,
Operator = ConditionOperator.Equal,
Values = { attributeValue }
}
}
}
};
return service.RetrieveMultiple(query);
}
Call the above reusable method by passing parameters: (make sure the schema name is modified per your need & parameterize the Spanish
filter)
var language = GetEntityCollection(_orgService, "Language_entityschemaname", "new_name", "Spanish", new ColumnSet("languageid", "new_name"));
Guid languageid = (Guid)language[0].Id;
If it’s data import job, and the languages are limited data like two entries viz. English & Spanish, I would recommend you to maintain an enum
, basically a key:value pair to switch it inline instead of querying for every row in batch operation.
In either case, code looks like:
studentObj.Language = new EntityReference("Language_entityschemaname", new Guid(languageid));