Many to many relationship in CRM 4.0 and Fetch XML


We have a custom entity called line of business (lob) for different line of business’s in our organization.

In CRM 3.0 there was no way we could have associated the lob custom entity with the system entity Team as there was no way of creating relationship between them.

But in CRM 4.0 we can now create a many to many relationship between the two.

Creating a many to many relationship between the two creates a new entity

new_team_new_lineofbusiness

Now say we want to write a custom code to retrieve all the teams associated with a given line of business, we can write something like below ( If we use query expression and retrieve multiple request it gives the error message saying that RetrieveMultiple is not supported on this entity)

This is the custom code for that

// setting the authentication token

CrmAuthenticationToken token = new CrmAuthenticationToken();

// 0- refers active directory

token.AuthenticationType = 0;

token.OrganizationName = “organizationname”;

 

CrmService crmService = new CrmService();

crmService.Url = http://servername/mscrmservices/2007/CrmService.asmx”;

crmService.Credentials = System.Net.CredentialCache.DefaultCredentials;

crmService.CrmAuthenticationTokenValue = token;

 

string fetch2= @”<fetch mapping=’logical’>

<entity name=’new_team_new_lineofbusiness’>

<attribute name=’teamid’ />

<filter>

<condition attribute=’new_lineofbusinessid’ operator=’eq’ value=’C4BE129E-9B3C-DB11-8CBA-001185E68627′ />

</filter>

</entity>

</fetch>”;

 

// Fetch the results.

try

{

String result2 = crmService.Fetch(fetch2);

MessageBox.Show(result2);

XmlDocument doc = new XmlDocument();

doc.LoadXml(result2);

XmlNodeList xnodlist = doc.GetElementsByTagName(“teamid”);

string teamID = “”;

if (xnodlist.Count != 0)

{

for (int i = 0; i < xnodlist.Count; i++)

{

XmlNode xnodRoot = xnodlist.Item(i);

teamID = xnodRoot.InnerText;

}

}

}

catch (SoapException ex)

{

MessageBox.Show(ex.Detail.InnerText);

}

Bye

Only the owner of an object can revoke the owner’s access to that object


Hi,

We had written a callout for auto sharing and unsharing of opportunity’s record for postassign event.

On creation of the record we were auto sharing the record with the owner’s manager.

On change of the owner we are first unsharing the record with the owner’s manager and sharing it with new owner’s manager.

While doing this the error we are receiving  is  this

“Only the owner of an object can revoke the owner’s access to that object “

This error is  coming in this particular scenario

say we have a record shared with a particular user say A

now when we are trying to set the new owner as A what the code is trying to do is that,

it is trying to unshare the record with A  this is the point where we receiving this  error!!!

For e.g.

Lead record is owned by User A and is Shared with User A.

Now the Revoke code is running in context of User B and tries to run Revoke for User A who is the owner. We will get this error message.

Bye..

Microsoft Dynamics CRM 3.0 and Microsoft Dynamics CRM 4.0 differences


Multiple organizations can now be hosted and WSDL APIs are now unique per organization.

 

The metadata API service has been extended to retrieve language information.

 

Plug-ins (callouts) and workflow now use the same event framework, allowing for even more extensibility.

 

The SDK has been expanded to include offline access.

 

Now we can programmatically create, read, update and delete the metadata such as entities, attributes and relationship.

 

There are three services instead of two which we used to have in previous version

 

CrmService – http://<crmserver>/mscrmservices/2007/crmservice.asmx

MetadataService – http://<crmserver>/mscrmservices/2007/metadataservice.asmx

DiscoveryService – http://<crmserver>/mscrmservices/2007/ad/crmdiscoveryservice.asmx

 

Previously to write callout we had to reference the follwing assembly

 

Microsoft.Crm.Platform.Callout.Base.dll

 

Instead now we have to reference these assemblies

 

  • Micorsoft.Crm.Sdk.dll
  • Micorsoft.Crm.SdkTypeProxy.dll
  • Micorsoft.Crm.Outlook.Sdk.dll

 

 

Now we can to settings and can then select customizations there we have the option of

Download Web Service Description File.

 

There we can find wsdl files for both CrmService and MetadataService which we can download to our machine and can the simply add web reference to them.

 

 

Previously to access and use the CrmService following lines of code were enough

 

CrmService service=new CrmService();

service.Url=// url for the CrmService

service.Credentials=System.Net.CredentialCache.DefaultCredentials;

 

But now because of the multiple organization support we need to write the following lines of code to identify the organization for which we are writing our custom solution.

 

CrmAuthenticationToken token = new CrmAuthenticationToken();

token.AuthenticationType = 0;

            // 0- Active Directory

            // 1- Microsoft Dynamics CRM Live

            // 2- Internet-Facing deployment (IFD)

token.OrganizationName=“”;// name of the organization

 

CrmService service=new CrmService();

service .Url=// url for the CrmService            service.Credentials=System.Net.CredentialCache.DefaultCredentials;

service.CrmAuthenticationTokenValue = token;

 

 

We have to access the Metadataservice in the similar manner i.e. creating CrmAuthenticationToken and assigning it to CrmMetadataService’s CrmAuthenticationTokenValue.

 

In 3.0 version we could use Metadataservice to access metadata information about any specific entity however in the new version following things are possible

 

  • Creating a customentity.
  • Add or update an attribute for an entity.
  • Create or delete a relationship between two entities.
  • Add or remove an option from a picklist attribute and few others.

 

 

  // CrmDiscoveryService Web service can provide a list of organizations and their corresponding Web service

 // endpoints URL’s. We will use it to configure the CrmService and MetadataService Web service proxies.

           

  CrmDiscoveryService discService = new CrmDiscoveryService();

  discService.UseDefaultCredentials = true;

  discService.Url = http://localhost/MSCRMServices/2007/AD/CrmDiscoveryService.asmx&#8221;;

 

  // Retrieve the list of organization

RetrieveOrganizationsRequest orgRequest = new    RetrieveOrganizationsRequest();

RetrieveOrganizationsResponse orgResponse =(RetrieveOrganizationsResponse) discService.Execute(orgRequest);

 

// Loop through the list to locate the target organization

 

OrganizationDetail orgInfo = null;

foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)

{

if (orgDetail.OrganizationName == “OurOrganizationName”)

    {

          orgInfo = orgDetail;

          break;

    }

}

 

After obtaining the organization details, we can then access the CrmService and MetadataService Web Services to perform our business logic.

 

 

References :

Working with Microsoft Dynamics CRM 4.0 (Microsoft Press)

Good site for understanding System.Net.Mail Namespace


Hi,

Plzz do check out this site if you need to write code for sending mail in asp.net 2.0

http://www.systemnetmail.com/default.aspx

Bye

Creating a simple Callout in Microsoft Dynamics CRM 3.0


Create a class library project in Visual Studio 2003.

 

Add reference to the following assembly Microsoft.Crm.Platform.Callout.Base.dll.

 

Inherit the Microsoft.Crm.Callout.CrmCalloutBase class.

 

Now override the event against which you want to put your business logic.

 

 

Add web reference to CrmService

http://servername:port/mscrmservices/2006/crmservice.asmx

 

Set up the CrmService properties like url, credentials and CallerIdValue

 

CrmService service = new CrmService();

            service.Url = “http://servername: port /mscrmservices/2006/CrmServiceWsdl.aspx”;

            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            service.CallerIdValue = new CallerId();           

            service.CallerIdValue.CallerGuid = userContext.UserId;

 

For our PostUpdate event handler we can see that the values for preImageEntityXml as well as postImageEntityXml are passed as a string which essentialy is a xml.

 

public override void PostUpdate(

                                                CalloutUserContext  userContext,

                                                CalloutEntityContext  entityContext,

                                                string  preImageEntityXml,

                                                string  postImageEntityXml

                                                )

 

So first we will convert it into a dynamic entity

 

DynamicEntity entityPost=ConvertToDynamicEntity(postImageEntityXml);

 

The definition of the ConvertToDynamicEntity function is

 

private static DynamicEntity ConvertToDynamicEntity(string xml)

                                {

 

                                                TextReader sr = new StringReader(xml);

 

                                                XmlRootAttribute root = new XmlRootAttribute(“BusinessEntity”);

                                                root.Namespace = http://schemas.microsoft.com/crm/2006/WebServices&#8221;;

                                               

 

                                                XmlSerializer xmlSerializer = new XmlSerializer(typeof(BusinessEntity), root);

                                                BusinessEntity entity = (BusinessEntity)xmlSerializer.Deserialize(sr);

 

                                                return (DynamicEntity) entity;

                                }

 

We’ll deserialize the xml output to get an instance of DynamicEntity.

 

To make it more easy to work with the properties we can make use of the following class PropertiyDictionary in our callout.

 

https://nishantrana.me/wp-content/uploads/2009/02/propertydictionary.doc

 

 

We’ll pass the instance of the newly created dynamic entity to the PropertyDictionary’s constructor.

 

PropertyDictionary properties=new PropertyDictionary(entityPost);

 

Now to work with properties we can do something like this

 

// For CrmDateTimeProperty

            if (properties.Contains(“actualclosedate”))

            {

                CrmDateTimeProperty acd = (CrmDateTimeProperty)properties[“actualclosedate”];

                actualclosedate = acd.Value;

            }

 

            // For CrmMoneyProperty

            if (properties.Contains(“new_estimatedrevenue”))

            {

 

                CrmMoneyProperty ner = (CrmMoneyProperty)properties[“new_estimatedrevenue”];

                new_estimatedrevenue = ner.Value;

            }

            // For PicklistProperty

            if (properties.Contains(“salesstagecode”))

            {

 

 

                PicklistProperty salesstagecode = (PicklistProperty)properties[“salesstagecode”];               

                salesstage = salesstagecode.Value.Value.ToString();

            }

           

 

That’s it…

JavaScript and Microsoft Dynamics CRM


I was thinking that it would have been nice if some expert would have written some article or post about how to use JavaScript within CRM.

And today only i came to know about such an article , it is written by none other than MichaelHohne, the CRM guru, the creator of stunnware site( the most helpful site for Microsoft CRM Developer)

For different things we can do by making use of JavaScript in Microsoft Dynamics CRM,

Plzzzz check and bookmark this url

http://www.stunnware.com/crm2/topic.aspx?id=JS13

Bye