serving the solutions day and night

Pages

Showing posts with label LINQ. Show all posts
Showing posts with label LINQ. Show all posts

Tuesday, May 13, 2014

MS Dynamics CRM - Create Email

List<ActivityParty> ccUsers = CCUsers(string ccEmails);
List<ActivityParty> toUsers = DNSTeamUsers();

CreateEmail(toUsers, ccUsers, subject, body);

//Create Email address, email will be in Draft format.

private Guid CreateEmail(List<ActivityParty> toUsers, List<ActivityParty> ccUsers, string subject, string body)
{
    Guid orgid, userid;
    SystemUserEntity.WhoAmIResponse(out orgid, out userid);

    ActivityParty fromUser = new ActivityParty
    {
    PartyId = new EntityReference(SystemUser.EntityLogicalName, userid)
    };

    Email email = new Email();
    email.From = new ActivityParty[] { fromUser };
  
    if (toUsers!=null)
    email.To = toUsers.ToArray();
  
    if (ccUsers!=null)
    email.Bcc = ccUsers.ToArray();
 
    email.Subject = subject;

    email.Description = body;

    //Assign regarding    
    email.RegardingObjectId = new EntityReference(contact.EntityLogicalName, contactid);
   
    //connect OrganizationServiceProxy
    Guid emailGUID = CRMServiceProxy.Create(email);
    return emailGUID;
}


Tuesday, April 15, 2014

Migrating selected entities data from UAT MS Dynamics 2011 to PROD MS Dynamics CRM 2011

Migrating selected entities data from UAT MS Dynamics 2011 to PROD MS Dynamics CRM 2011

Recently i engaged a new project to migrating data from one CRM server to another CRM server for selected entities.
Challenging work is moving email related entity data to another CRM server.
Here i going to discuss only email related entity code work.

I did this work using both CRM service and direct sql update.

1) i created 2 service, let assume one is UAT and another is PROD
public static OrganizationServiceProxy U_CRMServiceProxy
{
    get
    {
    var uri = new Uri(U_URI);

    //Authenticate using credentials of the logged in user;      
    var cntCredentials = new ClientCredentials();
    cntCredentials.Windows.ClientCredential.Domain = Domain;
    cntCredentials.Windows.ClientCredential.UserName = U_User;
    cntCredentials.Windows.ClientCredential.Password = U_Pass;
    //cntCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

    var serviceProxy = new OrganizationServiceProxy(uri, null, cntCredentials, null);
    //serviceProxy.ServiceConfiguration.CurrentServiceEndpoint.Behaviors.Add(new ProxyTypesBehavior());
    serviceProxy.EnableProxyTypes();

    return serviceProxy;
    }
}


Monday, April 22, 2013

Linq

For getting exact one instance it can be FirstOrDefault() method or SignleOrDefault() or just First() or Single().

The only difference is that methods without "OrDefault()" will throw an exception if enumeration will not satisfy their expectations, and methods with "OrDefault()" will just return null.

The dirrerence between Single and First is that Single expects exact one element in the collection and First expects at least one element.

How to get top 1 records from a query Just use FirstOrDefault() instead:

return (from a in dc.Applications
        where a.UserId == userId && a.chr_Version == version
        select a).FirstOrDefault<Application>();
SingleOrDefault() will throw an exception if there is more than one record, FirstOrDefault() will just take the first one.

Also you shouldn't have to cast to Application - your record already is of type Application.
For the first record you can try:
return (from a in dc.Applications where a.UserId == userId && a.chr_Version == version select a).FirstOrDefault();

For the first N use:
return (from a in dc.Applications where a.UserId == userId && a.chr_Version == version select a).Take(N);
Left Join SQL
select * from Employees e inner join States j on e.StateID = j.StateID left join Lookup c on (c.LookupValue = EmpTypeCode and c.LookupType ='TEMP') left join Lookup c1 on (c1.LookupValue = GovTypeCode and c1.LookupType ='PERM')

Linq
from el in db.Employees
join j in db.States on el.StateID equals j.StateID
join c1 in db.Lookup on new { etc = el.EmpTypeCode, ct1 = "TEMP" } equals new { etc = c1.LookupValue, ct1 = c1.LookupType }
into LeftJoinEmpLookup1 from c1 in LeftJoinEmpLookup1.DefaultIfEmpty()
join c2 in db.Lookup on new { gtc = el.GovTypeCode, ct2 = "PERM" } equals new { gtc = c2.LookupValue, ct2 = c2.LookupType }
into LeftJoinEmpLookup2 from c2 in LeftJoinEmpLookup2.DefaultIfEmpty()

Right Join SQL
select e.Name, d.Name from Employee e right join Department d on e.DeptID = d.ID
Linq
from dept in d
join employee in e on d.ID equals e.DeptID
into RightJoinDeptEmp from e in RightJoinDeptEmp.DefaultIfEmpty()

CRM Linq

//PickList values
private Dictionary<String, String> PickList(String entityName, String attID, string attName)
{
    Dictionary<String, String> dic = new Dictionary<String, String>();
    IOrganizationService service = (IOrganizationService)serviceProxy;
    QueryExpression qryExp = new QueryExpression(entityName);
    qryExp.ColumnSet = new ColumnSet(attName);
    EntityCollection entCollection = service.RetrieveMultiple(qryExp);
    dic = entCollection.Entities
        .Where(e => (e.Attributes.ContainsKey(attName)))
        .ToDictionary(e => e.Attributes[attID].ToString(), e => e.Attributes[attName].ToString());
    /*int i=1;
    foreach (var c in entCollection.Entities)
    {
        if (c.Attributes.ContainsKey(attName))
        {
        dic.Add(c.Attributes[attID].ToString(), c.Attributes[attName].ToString());
        }
    }*/
}

Tuesday, February 12, 2013

Get CRM PickList & Global OptionSet using c#

using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Metadata;

Uri OrganizationUri = new Uri(System.Configuration.ConfigurationManager.AppSettings["CRM_Organization_URI"].ToString());
Uri HomeRealmUri = null;
Dictionary<String, String> dicState = new Dictionary<String, String>();
Dictionary<String, String> dicSuffix = new Dictionary<String, String>();