Answered by:
Is it possible to search CRM if user exist in the system programmatically?

Question
-
Can someone help me to write code to see if the user exist in the system or not.
I need to supply shortname of the user and check if the user is found in the database using CRMService.Wednesday, March 3, 2010 5:52 PM
Answers
-
Something like that will work
ConditionExpression condition = new ConditionExpression();
condition.Operator = ConditionOperator.Equal;
condition.AttributeName = "domainname";
string domainname = TBDomainName.Text + @"\" + TBUsername.Text;
condition.Values = new string[] { domainname };FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] { condition };QueryExpression query = new QueryExpression();
query.EntityName = EntityName.systemuser.ToString();
query.ColumnSet = new AllColumns();
query.Criteria = filter;RetrieveMultipleRequest retrievequery = new RetrieveMultipleRequest();
retrievequery.Query = query;RetrieveMultipleResponse retrievedSystemUser = (RetrieveMultipleResponse)myCrm.Execute(retrievequery);
if (retrievedSystemUser.BusinessEntityCollection.BusinessEntities.Length > 0)
{
status = true;
}
else
{
MessageBox.Show("Incorrect username and password. Please enter correct username and password and try again.");
status = false;
}
Mariusz- Marked as answer by shiil Monday, March 8, 2010 8:23 PM
Thursday, March 4, 2010 12:56 AM
All replies
-
use QueryExpression or QueryByAttribute.
chek these links.
// Create the query object.
QueryByAttribute query = new QueryByAttribute();
query.ColumnSet = new AllColumns();
query.EntityName = EntityName.systemuser.ToString();
// The query will retrieve all system whose fullname= 'name of the system user'
query.Attributes = new string [] {"fullname"};
query.Values = new string [] {"name of the systemuser here"};
// Execute the retrieval.
BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
if(retrieved.BussinessEntities.Count==0)
{
MessageBox.Show("User doesn't exits");
}
else
{
MessageBox.Show("user exits, count= " reterived.Business.Count);
}
http://technet.microsoft.com/en-us/library/aa685880.aspx
http://technet.microsoft.com/en-us/library/aa685881.aspx
examples are for CRM 3 but it will work for CRM 4 as well.
Muhammad Ali Khan
My MS CRM blog- Edited by Muhammad Ali Khan Wednesday, March 3, 2010 5:59 PM more info added
- Proposed as answer by Muhammad Ali Khan Wednesday, March 3, 2010 6:54 PM
Wednesday, March 3, 2010 5:57 PM -
Thanks for your help. I get error on the if condition. I used the following:
if(retrieved.BusinessEntities.Count == 0)
On this line the error message is "Operator '==' cannot be applied to operants of type 'method grouop' and 'int'"Wednesday, March 3, 2010 7:37 PM -
I am doing something similar and I do the check on the "Length" property instead. Maybe you want to try that?
if(retrieved.BusinessEntities.Length == 0)Wednesday, March 3, 2010 9:33 PM -
Something like that will work
ConditionExpression condition = new ConditionExpression();
condition.Operator = ConditionOperator.Equal;
condition.AttributeName = "domainname";
string domainname = TBDomainName.Text + @"\" + TBUsername.Text;
condition.Values = new string[] { domainname };FilterExpression filter = new FilterExpression();
filter.Conditions = new ConditionExpression[] { condition };QueryExpression query = new QueryExpression();
query.EntityName = EntityName.systemuser.ToString();
query.ColumnSet = new AllColumns();
query.Criteria = filter;RetrieveMultipleRequest retrievequery = new RetrieveMultipleRequest();
retrievequery.Query = query;RetrieveMultipleResponse retrievedSystemUser = (RetrieveMultipleResponse)myCrm.Execute(retrievequery);
if (retrievedSystemUser.BusinessEntityCollection.BusinessEntities.Length > 0)
{
status = true;
}
else
{
MessageBox.Show("Incorrect username and password. Please enter correct username and password and try again.");
status = false;
}
Mariusz- Marked as answer by shiil Monday, March 8, 2010 8:23 PM
Thursday, March 4, 2010 12:56 AM -
Yeah
if(retrieved.BusinessEntities.Length == 0)
the above one is correct, just typo.
Muhammad Ali Khan
My MS CRM blogThursday, March 4, 2010 3:57 AM