Sample code to parse JSON Date Format


Hi,

Just sharing a sample code to parse JSON Date Format (e.g. “\/Date(628318530718)\/”) to Date.


entity = JSON.parse(retrieveReq.responseText).d;

if (entity['BirthDate'] != null) {

var jdate = parseJsonDate(entity['BirthDate']);
 var year = jdate.getFullYear();
 var month = jdate.getMonth() + 1 < 10 ? '0' + (jdate.getMonth() + 1) : (jdate.getMonth() + 1);
 var date = jdate.getDate() < 10 ? '0' + jdate.getDate() : jdate.getDate();
 var dateString = month + '/' + date + '/' + year;

 alert(dateString);
 }

function parseJsonDate(jsonDate) {
 var offset = new Date().getTimezoneOffset() * 60000;
 var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate);

if (parts[2] == undefined)
 parts[2] = 0;

if (parts[3] == undefined)
 parts[3] = 0;

return new Date(+parts[1] + offset + parts[2] * 3600000 + parts[3] * 60000);
 }

}

Bye.

Using Client HTTP Stack with oData in Silverlight (CRM 2011)


Hi,

Just sharing a simple example that uses Client HTTP Stack while developing Silverlight web resource.

The good thing with this is that we don’t have to upload and publish the web resource for every change. However if the Siliverlight component is running in the context of the form and interacting with it then in that case we will have to upload and publish it.

Steps : –

Create a Silverlight application and add service reference to the oData service of CRM 2011.

Go to Settings |Customizations | Developer Resources

Get the url of the OrganizationData service

Add Service Reference to the URL

The context would be named as OrganizationNameContext.

Now in the MainPage.xaml set the url of the OrganizationData and Set up the context in the following manner.

 public partial class MainPage : UserControl
 {

private GGContext context;
 private String orgDataServerUrl;

public MainPage()
 {
 InitializeComponent();

// set the server url
 orgDataServerUrl = "http://server:port/GG/XRMServices/2011/OrganizationData.svc";

// set the context
 context = new GGContext(new Uri(orgDataServerUrl));

// set up the Client Http Stack
 context.HttpStack = System.Data.Services.Client.HttpStack.ClientHttp;
 context.UseDefaultCredentials = false;
 context.Credentials = new NetworkCredential("administrator", "password", "domain");
 }
 . . . .

Put the following clientaccesspolicy.xml in the following location

“..\Program Files\Microsoft Dynamics CRM\CRMWeb”

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
 <cross-domain-access>
 <policy>
 <allow-from http-request-headers="*">
 <domain uri="*"/>
 </allow-from>
 <grant-to>
 <resource path="/" include-subpaths="true"/>
 </grant-to>
 </policy>
 </cross-domain-access>
</access-policy>

Once we are done with development we can set up the context in the following manner

 public partial class MainPage : UserControl
 {

private GGContext context;
 private String orgDataServerUrl;

public MainPage()
 {
 InitializeComponent();

// get the Server Url
 orgDataServerUrl = this.GetServerUrlFromContext();

// setup Context
 context = new GGContext(
 new Uri(String.Format("{0}/xrmservices/2011/organizationdata.svc/",
 orgDataServerUrl), UriKind.Absolute));

//This is important because if the entity has new
 //attributes added the code will fail.
 context.IgnoreMissingProperties = true;

}

private string GetServerUrlFromContext()
 {
 try
 {
 // If the Silverlight is in a form, this will get the server url.
 ScriptObject xrm = (ScriptObject)HtmlPage.Window.GetProperty("Xrm");
 ScriptObject page = (ScriptObject)xrm.GetProperty("Page");
 ScriptObject pageContext = (ScriptObject)page.GetProperty("context");

String serverUrl = (String)pageContext.Invoke("getServerUrl");

// The trailing forward slash character from CRM Online needs to be
 // removed.
 if (serverUrl.EndsWith("/"))
 {
 serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
 }

return serverUrl;
 }
 catch
 {
 return String.Empty;
 }
 }
 }

Hope this helps.

HTML Bridge in Silverlight


Hi,

Just sharing simple example.

Suppose we have the following HTML page having a Silverlight control (blue background) in it.

When we click on “Pass to Silverlight” button we want to pass the values specified in the textboxes in our HTML page to the text boxes in the Silverlight control.

To do this we can create a function inside our Silverlight page (MainPage.xaml.cs) and decorate it with ScriptableMember attribute and also use RegisterScriptableObject method.

 [System.Windows.Browser.ScriptableMember]
 public void SetLabelFromHtml(string firstName, string lastName)
 {
 txtFirstName.Text = firstName;
 txtLastName.Text = lastName;
 }
 
public MainPage()</pre>
 {
 InitializeComponent();
 // RegisterScriptableObject Method
 // Registers a managed object for scriptable access by JavaScript code
 System.Windows.Browser.HtmlPage.RegisterScriptableObject("myMethod", this);
 }
<pre> 

To call this function from our html page, we can make use of following JavaScript.

function btnPass_onclick() {

// id of the silverlight control in object tag
var silverlightPlugin = document.getElementById("mySilverLightControl");
// get the values from the text boxes in the html page
var firstName = document.getElementById("txtFirstName").value;
var lastName = document.getElementById("txtLastName").value;

silverlightPlugin.content.myMethod.SetLabelFromHtml(firstName, lastName);

}

Similarly if we want to pass the values for the text boxes from Silverlight page to our HTML page, we need to do the following.

First create a JavaScript function in out HTML page

function SetValueFromSilverlight(firstName, lastName) {

document.getElementById("txtFirstName").value = firstName;
 document.getElementById("txtLastName").value = lastName;

}

To call this JavaScript function from our Silverlight page we can make use of following line of code

HtmlPage.Window.CreateInstance("SetValueFromSilverlight",new string[] { txtFirstName.Text,txtLastName.Text});

Apart from calling the JavaScript function inside our html, we can directly access the fields through DOM and set their values in Silverlight

HtmlDocument document = HtmlPage.Document;

// get the text boxes
HtmlElement firstName = document.GetElementById("txtFirstName");
HtmlElement lastName = document.GetElementById("txtLastName");

// get the value in html text boxes
MessageBox.Show("First Name is " + firstName.GetProperty("value").ToString());
MessageBox.Show("Last Name is " + lastName.GetProperty("value").ToString());

// set the html text boxes
firstName.SetProperty("value", txtFirstName.Text);
lastName.SetProperty("value", txtLastName.Text);

Bye.

An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail.


We were getting the above error while testing one of our WCF service. On syncing the clock of our client and server machine we were able to resolve the issue.

This helped us resolve the issue

http://social.msdn.microsoft.com/Forums/eu/wcf/thread/eccf2b09-6ea1-4663-9ddf-af29901ea60a

Bye.

 

Sample Code to Cancel All the Open Activities for a particular record in CRM 2011.


Hi,

Just sharing a sample code that could be used to cancel all the open activities for a particular record.

 private void CancelChildActivities(Guid entityRecordGuid, IOrganizationService service)
 {
 QueryExpression qe = new QueryExpression();
 qe.ColumnSet = new ColumnSet(new string[] { "activityid", "activitytypecode" });
 qe.EntityName = "activitypointer";

FilterExpression filterStateCode = new FilterExpression();
 filterStateCode.FilterOperator = LogicalOperator.Or;
 filterStateCode.AddCondition("statecode", ConditionOperator.Equal, "Open");
 filterStateCode.AddCondition("statecode", ConditionOperator.Equal, "Scheduled");

FilterExpression filter = new FilterExpression();
 filter.FilterOperator = LogicalOperator.And;
 filter.AddCondition("regardingobjectid", ConditionOperator.Equal, entityRecordGuid);
 filter.AddFilter(filterStateCode);

qe.Criteria = filter;

RetrieveMultipleRequest request = new RetrieveMultipleRequest();
 request.Query = qe;
 RetrieveMultipleResponse response = (RetrieveMultipleResponse)service.Execute(request);

foreach (Entity activity in response.EntityCollection.Entities)
 {
 CancelActivity(activity, service);
 }

}

 private void CancelActivity(Entity entity, IOrganizationService service)
 {
 EntityReference moniker = new EntityReference();

if (entity.LogicalName == "activitypointer")
 {
 if (entity.Attributes.Contains("activityid") & entity.Attributes.Contains("activitytypecode"))
 {
 moniker.LogicalName = entity.Attributes["activitytypecode"].ToString();
 moniker.Id = (Guid)entity.Attributes["activityid"];

SetStateRequest request = new SetStateRequest();
 request.EntityMoniker = moniker;
 request.State = new OptionSetValue(2);
 request.Status = new OptionSetValue(-1);
 SetStateResponse response = (SetStateResponse)service.Execute(request);
 }
 }

}

Bye.

Customization (Unsupported) of Lookup in CRM 2011.


Hi,

For disabling view button, disabling “look for” option or hiding Button in lookup we can make use of JavaScript.

We need to refer to our lookup through DOM in our onload event and set its attributes (unsupported).

Suppose our lookup id is customerid

document.getElementById(“customerid”).setAttribute(“disableViewPicker“, “1”);


document.getElementById(“customerid”).setAttribute(“disableQuickFind“, “1”); 


document.getElementById(“customerid”).setAttribute(“_lookupbrowse“, “1”); 

Hiding New Button

function myHideLookupButton()

{

crmForm.all.customerid.attachEvent(“setadditionalparams“,HideButton);

}

function HideButton()

{

crmForm.all.customerid.AddParam(“ShowNewButton“, 0);

}

Bye.