Often we keep getting requirements to do one time update or to schedule an update on some entities and the criteria for updating the set of entities is not sure. So, I wanted to devise a configurable way of doing it. The way is to have a personal view, share it with client and utilize it for fetching the entities. Following function block takes a personal view name as input and returns you the Entity Collection:
/// <summary>
/// Fetches the view collection.
/// </summary>
/// <param name=”service”>The service.</param>
/// <param name=”viewName”>Name of the view.</param>
/// <returns></returns>
private static EntityCollection FetchViewCollection(IOrganizationService service, string viewName)
{
QueryExpression query = new QueryExpression(“userquery”);
query.ColumnSet = new ColumnSet(true);
query.Criteria.Conditions.Add(new ConditionExpression(“name”, ConditionOperator.Equal, viewName));
EntityCollection viewCollection = service.RetrieveMultiple(query);
if (viewCollection.Entities.Count == 1 && viewCollection.Entities[0].Contains(“fetchxml”))
{
var fetchExpression = new FetchExpression((string)viewCollection.Entities[0].Attributes[“fetchxml”]);
return ((EntityCollection)service.RetrieveMultiple(fetchExpression));
}
else
{
return null;
}
}
Just plug it to start of your console application for batch job and you can make it configurable for updates. Hope it helps!