#CRM2016 – Get all teams by user in C#

public Microsoft.Xrm.Sdk.EntityCollection UserTeams(Guid UserID)
{

    QueryExpression query = new QueryExpression("team");
    query.ColumnSet = new ColumnSet(true);
    LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
    link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, UserID));

    try
    {
      return service.RetrieveMultiple(query);
    }
    catch (Exception ex)
    {
       // Do your Error Handling here
       throw ex;
    }
}

CRM2016 – Check if User is a mamber of a team C#

The code is the following:

public static bool IsTeamMember(Guid teamID, Guid userID, IOrganizationService service)
{
QueryExpression query = new QueryExpression("team");
query.ColumnSet = new ColumnSet(true);
query.Criteria.AddCondition(new ConditionExpression("teamid", ConditionOperator.Equal, teamID));
LinkEntity link = query.AddLink("teammembership", "teamid", "teamid");
link.LinkCriteria.AddCondition(new ConditionExpression("systemuserid", ConditionOperator.Equal, userID));
var results = service.RetrieveMultiple(query);
 
if (results.Entities.Count > 0)
{
return true;
}
else
{
return false;
}
}

CRM2016 – Getting use of PublishXmlRequest SDK message to publish an Entity or a Web Resource

Today I had a timeout error on my application when I tried to use PublishAllXmlRequest .I resolved to use a single PublishXmlRequest in this way:

#region Publish FormXMl
//Publish entity for which formxml has been changed 
PublishXmlRequest publishRequest = new PublishXmlRequest();
publishRequest.ParameterXml ="<importexportxml>" +
"    <entities>" +
"        <entity>" + entityName + "</entity>" +
"    </entities>" +
"    <nodes/>" +
"    <securityroles/>  " +
"    <settings/>" +
"    <workflows/>" +
"</importexportxml>";

try
{
organizationService.Execute(publishRequest);
}
catch (Exception ex)
{
    throw new ex
}
#endregion

CRM2011 – Plugin Registration Error: A proxy type with the name account has been defined by another assembly

It’s second time that I have this error but everytime I forgot the solution and I need to search again…. 🙂 Now I want to save the solution here on my blog.

Problem:

So I ran into a very tricky error when building a custom tool to register CRM 2011 Plug-ins.  I wouldn’t be surprised if other people run into the same issue when registering plug-ins programmatically. And its such a strange problem I thought it deserved an explanation.

Here is the exception I got when retrieving a list of records from CRM:

<strong>A proxy type with the name account has been defined by another assembly. Current type: Plugins1.Entities.Account, App_Code.zzq-u5xe, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null, Existing type: Plugins1.Entities.Account, vender portal, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
Parameter name: account</strong>

I trace the problem what it is? It is actually problem from Organization Service i used,It gets definition of account entity definition from two assembly and confuse to process which assembly should be executed for actual objects.

Solution:

Issue resolved by specify assembly for execution or creating objects for entities:
just have to add a new ProxyTypesBehavior:

ClientCredentials credentials = new ClientCredentials();
credentials.Windows.ClientCredential = new NetworkCredential(username, password, domain);
credentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
IServiceConfiguration<IOrganizationService> orgConfigInfo = ServiceConfigurationFactory.CreateConfiguration<IOrganizationService>(new Uri(@"[...]/XRMServices/2011/Organization.svc"));
OrganizationServiceProxy client = new OrganizationServiceProxy(orgConfigInfo, credentials);
<strong>client.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior(Assembly.GetExecutingAssembly()));</strong>
CRM 2011 – How to get the current user from within a worflow

CRM 2011 – How to get the current user from within a worflow

Today I needed to get the Current User inside a workflow activity. In CRM 2001 process it is not possible to get current user and in some cases a reference can be necessary in order to set the right user.
A Custom Workflow Activity can be created for this purpose, taking advantage of the InitiatingUserId property.

public class GetCurrentUser : CodeActivity
    {
        [Output("Current User")]
        [ReferenceTarget("systemuser")]
        public OutArgument<EntityReference> CurrentUser { get; set; }
        protected override void Execute(CodeActivityContext context)
        {
            IWorkflowContext workflowContext = context.GetExtension<IWorkflowContext>();
            CurrentUser.Set(context, new EntityReference("systemuser", workflowContext.InitiatingUserId));

        }
    }

Now from our workflow activity we have the current user.

CRM2011 – Search what is the database associated at Organization

Sometimes I need to search what is a database associated at an existing organization. It is simple: From CRM Server, we need to ope SQL Server tools and execuete this query:

By running this query within MSCRM_CONFIG database we should be able to easily identify each organization:

SELECT [DatabaseName]
      ,[FriendlyName]
      ,[SqlServerName]
      ,[SrsUrl]
      ,[State]
      ,[UniqueName]
      ,[UrlName]
      ,[IsDeleted]
FROM [MSCRM_CONFIG].[dbo].[Organization]

That’s all…