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:
/// Fetches the view collection.
/// <param name=”service”>The service.</param>
/// <param name=”viewName”>Name of the view.</param>
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.Contains(“fetchxml”))
var fetchExpression = new FetchExpression((string)viewCollection.Entities.Attributes[“fetchxml”]);
Just plug it to start of your console application for batch job and you can make it configurable for updates. Hope it helps!