Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.ServiceModel.Description;
namespace MelTools
{
class getAllUsersPersonalViewsContainingField
{
static void Main(string[] args)
{
ClientCredentials clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "xxx";
clientCredentials.UserName.Password = "xxx";
OrganizationServiceProxy organizationService = new OrganizationServiceProxy(new Uri("https://yourOrganization.crm4.dynamics.com/XRMServices/2011/Organization.svc"), null, clientCredentials, null);
// Instantiate QueryExpression QEsystemuser
var QEsystemuser = new QueryExpression("systemuser");
// Add all columns to QEsystemuser.ColumnSet
QEsystemuser.ColumnSet.AllColumns = true;
EntityCollection users = organizationService.RetrieveMultiple(QEsystemuser);
foreach (var user in users.Entities)
{
try
{
organizationService.CallerId = user.Id;
// Define Condition Values
var QEuserquery_fetchxml_entity = "%<entity name=\"contact\">%";
var QEuserquery_fetchxml_field= "%<attribute name=\"mel_contactnumber\"/>%";
// Instantiate QueryExpression QEuserquery
var QEuserquery = new QueryExpression("userquery");
// Add all columns to QEuserquery.ColumnSet
QEuserquery.ColumnSet.AllColumns = true;
// Define filter QEuserquery.Criteria
QEuserquery.Criteria.AddCondition("fetchxml", ConditionOperator.Like, QEuserquery_fetchxml_entity);
QEuserquery.Criteria.AddCondition("fetchxml", ConditionOperator.Like, QEuserquery_fetchxml_field);
EntityCollection personalViews = organizationService.RetrieveMultiple(QEuserquery);
if (personalViews.Entities.Count > 0)
{
foreach (var personalView in personalViews.Entities)
{
Console.WriteLine("view name: " + personalView.GetAttributeValue<string>("name") + "| owner: " + personalView.GetAttributeValue<EntityReference>("ownerid").Name);
}
}
}
catch (Exception e) { }
}
Console.WriteLine("click to exit");
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment