Updating value in CRM entity in Callout written for Update event


 

Well we had a requriement in our case where we wanted to update a particular attribute value in our Crm’s opportunity entity in our update callout.

While looking for the same I came to know that if we are updating any attribute’s value in the entity against which we have attached our update callout, we will be lost in infinite loop.

Only way to do this is in the PreUpdate and there is a sample code for the same in sdk as well.(Unsupported)

public override PreCalloutReturnValue PreUpdate(CalloutUserContext userContext,CalloutEntityContext entityContext, ref string entityXml,ref string errorMessage)

Here we can see that the entityXml is passed to us as a reference. So any modification made to the entityXml would reflect back in that entity’s form as well.

Suppose this is the entityXml which we receive in our PreUpdate event

<?xml version=1.0 encoding=utf-16?>

<BusinessEntity xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema xsi:type=DynamicEntity Name=opportunity xmlns=http://schemas.microsoft.com/crm/2006/WebServices>

<Properties>

<Property xsi:type=KeyProperty Name=opportunityid>

<Value>57330ec3-6f5d-dd11-acd2-00164145e126</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_estimatedvalue>

<Value>1</Value>

</Property>

<Property xsi:type=CrmMoneyProperty Name=estimatedvalue>

<Value>77.5</Value>

</Property>

</Properties>

</BusinessEntity>

Now after running this code in our preUpdate event handler we can add a new property tag itself, which would be refering to the attribute we would like to update

// creating an xmlDocument

XmlDocument entityDoc = new XmlDocument();

// loading the entityXml in it

entityDoc.LoadXml(entityXml);

// the attribute which we would like to update

XmlElement totatForecastedValue = null;

// get the list of properties

XmlNodeList propertyList = entityDoc.GetElementsByTagName(“Property”);

XmlElement properties = (XmlElement)entityDoc.GetElementsByTagName(“Properties”)[0];

// creating an element with property tag

XmlElement totalForecastedElement = entityDoc.CreateElement(“Property”);

// creating an attribute type for the property tag

XmlAttribute typeAttrib = entityDoc.CreateAttribute(“type”);

totalForecastedElement.SetAttribute(“type”, http://www.w3.org/2001/XMLSchema-instance&#8221;, CrmFloatProperty”);

totalForecastedElement.SetAttribute(“Name”, “new_totalbilling”);

totatForecastedValue = entityDoc.CreateElement(“Value”);

// setting the value for the attribute

totatForecastedValue.InnerText = “10”;

totalForecastedElement.AppendChild(totatForecastedValue);

properties.AppendChild(totalForecastedElement);

 

//saving the output

StringWriter output = new StringWriter();

entityDoc.Save(output);

 

// assigning the same output to the entityXml

entityXml = output.ToString();

 

// Remove extra XML that will confuse CRM.

entityXml = entityXml.Replace(“xmlns=\”\””, “”);

entityXml = entityXml.Replace(“<?xml version=\”1.0\” encoding=\”utf-16\”?>”, “”);

The entityXml gets modified with the above code in this manner

<BusinessEntity xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema xsi:type=DynamicEntity Name=opportunity xmlns=http://schemas.microsoft.com/crm/2006/WebServices>

<Properties>

<Property xsi:type=KeyProperty Name=opportunityid>

<Value>57330ec3-6f5d-dd11-acd2-00164145e126</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_estimatedvalue>

<Value>1</Value>

</Property>

<Property xsi:type=CrmMoneyProperty Name=estimatedvalue>

<Value>77.5</Value>

</Property>

<Property xsi:type=CrmFloatProperty Name=new_totalbilling >

<Value>10</Value>

</Property>

</Properties>

</BusinessEntity>

 

And this way our entity gets updated as well.

And the same code in CRM 3.0 callout  works properly in case of upgrading the CRM 3.0 server to CRM 4.0 .

 

Bye..

 

 

Delegate, Anonymous Methods and Lambda Expressions in C#


Delegate acts as a function pointer.

We define the function to which the delegate can point to in this manner

// Delegate which will point to a method which has no parameter and returns nothing

public delegate void MySimpleDelegate();

// Delegate which will point to a method which takes string as parameter and returns string as well

public delegate string MyComplexDelegate(string message);

 

Now comes our two function to which delegates would be pointing 

public void SimpleDelegate(){

MessageBox.Show(“My Simple Delegate”);

}  

public string ComplexDelegate(string name){

MessageBox.Show(name);

return “Hi “ + name;

} 

// We have created an instance of our MySimpleDelegate

// which would be referencing SimpleDelegate function

MySimpleDelegate myD = SimpleDelegate;

// when we call the delegate it calls the method which it is pointing to

myD();

// Same thing with our MyComplexDelegate

MyComplexDelegate myAD = AnotherDelegate;

myAD(“Hello”);

 

// If we know that our function SimpleDelegate() and ComplexDelegate(string name)

// wouldn’t be called by any other code other than the delegate itself we can

// make use of anonymous method i.e. method with no name

// Here again we have done the same thing but used anonymous method

// because we aren’t going to use it anywhere else

MySimpleDelegate myD = delegate

{

MessageBox.Show(“Simple Delegate”);

};

myD();

MyComplexDelegate myAD = delegate(String name)

{

MessageBox.Show(name);

return “Hi “ + name;

};

myAD(“Hello”);

// Now let’s shorten our code further using lambda expression

// () -> no input parameter

// => -> lambda operator

// MessageBox.Show(“Simple delegate”) -> statement block

MySimpleDelegate myD = () => MessageBox.Show(“Simple delegate”);

myD();

//(String name) -> explicitly typed parameter

MyComplexDelegate myAD = (String name) => { MessageBox.Show(name); return name; };

myAD(“Hello”);

 

Bye.

 

Modifying the Convert Lead Web dialog box in CRM


Hi,

We had a requirement of not allowing the user to convert the lead to an account or contact through the convert lead dialog box.

That required change the soucre code of the convert lead dialog box’s web page. (This is not supported)

I found the page at the following location

“C:\Program Files\Microsoft Dynamics CRM\CRMWeb\SFA\leads\dialogs\conv_lead.aspx”

Opened it in Visual Studio 2005

and used the div tag for hiding the check boxes for account and contact.

<tr >

<td>

<div style=”display:none;” >

<input class=”checkbox” type=”checkbox” id=”cbAddAccount” onclick=”updateUIState()” <%= (Microsoft.Crm.Security.User.GetPrivilege(CurrentUser, Privileges.CreateAccount )) ? “editable” : “disabled” %>>

</div>

</td>

<td>

<div style=”display:none;” >

<label for=”cbAddAccount”><% =Microsoft.Crm.CrmEncodeDecode.CrmHtmlEncode(Util.GetFmtObjName(Util.Account, Util.NameFormatStyle.Singular)) %></label>

</div>

</td>

</tr>

<tr >

<td>

<div style=”display:none;” >

<input class=”checkbox” type=”checkbox” id=”cbAddContact” onclick=”updateUIState()” <%= (Microsoft.Crm.Security.User.GetPrivilege(CurrentUser, Privileges.CreateContact )) ? “editable” : “disabled” %>>

</div>

</td>

<td>

<div style=”display:none;” >

<label for=”cbAddContact”><% =Microsoft.Crm.CrmEncodeDecode.CrmHtmlEncode(Util.GetFmtObjName(Util.Contact, Util.NameFormatStyle.Singular)) %></label>

</div>

</td>

</tr>

It worked !!

Bye..

Customization Import failed. Error: OrganizationUI With Id =…… Does Not Exist or Failure: new_…: The requested record was not found or you do not have sufficient permissions to view it.


I was getting this error when i was trying to import customization for one of our custom entity from our development server to test server.

The way i resolved was

1) Opened the customization.xml file i was trying to import.

2) Search for the organization id which was mentioned in the event log as one not existing. (OrganizationUI With Id = 42467170-73ec-4b39-8eca-309ab0daece9 Does Not Exist)

3) I found the id in the  <FormXml id=”{42467170-73ec-4b39-8eca-309ab0daece9}”> tag.

4) I exported the customization for the same entity from my test server.

5) In the customization file(test server) i found out it had different id for FormXml.

6) Replace the id value for FormXml in the development server’s customization with id i found at the test server for the same entity.

7) Than i tried importing this newly modified file and it imported without giving any error.

Bye…

Setting Built In and Custom Document Properties using C#


Create a new windows application project and add a button to it.

On click of that button, we will open a document and add values for a built in and custom document properties.

Than add reference to (Word 10.0 or 11.0 object library) and Microsoft.Office.Core.dll within COM tab of Add reference dialog box.

After adding reference, add this directive

using Microsoft.Office.Interop.Word

using System.Reflection;

Put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename which is the file to be opened

object fileName = @”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing);

 

// setting the document built in properties

object oDocBuiltInProps = adoc.BuiltInDocumentProperties;

Type typeDocBuiltInProps = oDocBuiltInProps.GetType();

// setting the Title property with value “My Proposal”

string strIndex = “Title”;

string strValue = “My Proposal”;

typeDocBuiltInProps.InvokeMember(“Item”,

BindingFlags.Default |

BindingFlags.SetProperty,

null, oDocBuiltInProps,

new object[] { strIndex, strValue });

 

 

// setting the custome document properties

object oDocCustomProps = adoc.CustomDocumentProperties;

Type typeDocCustomProps = oDocCustomProps.GetType();

// setting the ProposalSentDate custom date property with current date time

string strIndex1 = “ProposalSentDate”;

string strValue1 = DateTime.Now.ToShortDateString();

object[] oArg = { strIndex1, false, Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeDate, strValue1 };

typeDocCustomProps.InvokeMember(“Add”,

BindingFlags.Default |

BindingFlags.InvokeMethod, null,

oDocCustomProps,oArg);

 

WordApp.Visible = true;

 

Byee…

Creating and Updating table of contents in word document using C#


Create a new windows application project and add a button to it.

On click of that button, we will open a document and first add a heading 1 programmatically and then insert a table of content programmatically and update it

Than we will add reference to (Word 10.0 or 11.0 object library) within COM tab of Add reference dialog box.

After adding reference, we’ll add this directive using Microsoft.Office.Interop.Word than we’ll put the following code in the button click event handler

// For optional parameters create a missing object

object missing = System.Reflection.Missing.Value;

// Create an object for filename which is the file to be opened

object fileName = @”C:\MySecond.doc”;

// Create an object of application class

ApplicationClass WordApp = new ApplicationClass();

// open the document specified in the fileName variable

Document adoc = WordApp.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,

ref missing, ref missing, ref missing);

Range myRange = adoc.Range(ref missing, ref missing);

myRange.InsertAfter(“Hello Mickey Mouse GG “);

object oStyleName = “Heading 1”;

myRange.set_Style(ref oStyleName);

object start=WordApp.ActiveDocument.Content.End – 1;

Range rangeForTOC = adoc.Range(ref start, ref missing);

TableOfContents toc=adoc.TablesOfContents.Add(rangeForTOC, ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

toc.Update();

Range rngTOC = toc.Range;

rngTOC.Font.Size = 10;

rngTOC.Font.Name = Georgia;

WordApp.Visible = true;

Bye…

Nishant Rana's Weblog

Everything related to Microsoft .NET Technology

Skip to content ↓