Showing posts with label Dynamics CRM 2011. Show all posts
Showing posts with label Dynamics CRM 2011. Show all posts

Get UnCached version form Ajax file request

While loading external javascript there is problem of caching the general solutionfor this is appending a url parameter like "?rnd=Date.Now()" at the end of the url, so that the url is always unique while making the call. The other way around is to set the IIS ouput caching with FIle change notifications. But the same could be achieve without much effort by setting the response header. We can set the response header to get the modified version from the server instead of the cached one using the following script:
 

/* Ajax with Un Cached output in MSCRM 4 */
function _LoadIsvScript(execute) {
var JsScriptUrl = "/ISV/customscript.js";
var strServerURL = SERVER_URL.split(ORG_UNIQUE_NAME);
var script_uri = strServerURL[0] + JsScriptUrl;
var ajax = new ActiveXObject("Msxml2.XMLHTTP");
ajax.open('GET', script_uri, false);
//GetUnCachedVersion
ajax.setRequestHeader("If-Modified-Since", "Sat, 01 Jan 2000 00:00:00 GMT");
ajax.setRequestHeader('Cache-Control', 'no-cache');
ajax.send('');
return ajax.responseText;
}


Converting a JavaScript Date object into the proper XML string


function SwConvertDateTimeToXml(dateTime) {

var offset = (ORG_TIMEZONE_OFFSET < 0) ? -ORG_TIMEZONE_OFFSET : ORG_TIMEZONE_OFFSET;
var timezoneOffsetHours = Math.floor(offset / 60);
var timezoneOffsetMinutes = offset - timezoneOffsetHours * 60;

var s =
dateTime.getYear().toString() + "-" +
SwGetFormattedDatePart(dateTime.getMonth() + 1) + "-" +
SwGetFormattedDatePart(dateTime.getDate()) + "T" +
SwGetFormattedDatePart(dateTime.getHours()) + ":" +
SwGetFormattedDatePart(dateTime.getMinutes()) + ":" +
SwGetFormattedDatePart(dateTime.getSeconds()) +
((ORG_TIMEZONE_OFFSET < 0) ? "-" : "+") +
SwGetFormattedDatePart(timezoneOffsetHours) + ":" +
SwGetFormattedDatePart(timezoneOffsetMinutes);

return s;
}

function SwGetFormattedDatePart(value) {
return (value < 10) ? ("0" + value.toString()) : value.toString();
}

Converting an XML date string to a JavaScript Date object


function SwConvertXmlToDateTime(crmDateTimeString) {
var dateTimeParts = crmDateTimeString.split("T");
var dateString = dateTimeParts[0];
var timeString = dateTimeParts[1];
var dateParts = dateString.split("-");
var timeZoneSeparator = (timeString.indexOf("-") != -1) ? "-" : "+";
var timeZoneParts = timeString.split(timeZoneSeparator);
var timeParts = timeZoneParts[0].split(":");

var date = new Date(SwParseInt(dateParts[0]), SwParseInt(dateParts[1]) - 1, SwParseInt(dateParts[2]), SwParseInt(timeParts[0]), SwParseInt(timeParts[1]), SwParseInt(timeParts[2]));
return date;
}

function SwParseInt(value) {
if ((value.length == 2) && (value.substr(0, 1) == "0")) {
value = value.substr(1, 1);
}

return parseInt(value);
}

CrmService proxy for plug-ins that execute in the child pipeline.

We always come across creating a CrmService Proxy for plug-ins that execute in the child pipeline. In a Child pipeline, you must instantiate the CrmService or MetadataService manually. We just need to check the InvocationSource Property (Child = 1, Parent = 0) of the IPluginExecutionContext. Pass the InvocationSource Property value to the below method.

Below is very simple code which creates a proxy.




public class CRM4_ServiceProx
{


/// The execution context that was passed to the plug-in's Execute method.
/// Set to True to use impersonation.
/// A CrmServce instance.
private CrmService CreateCrmService(IPluginExecutionContext context, Boolean flag)
{
CrmAuthenticationToken authToken = new CrmAuthenticationToken();
authToken.AuthenticationType = 0;
authToken.OrganizationName = context.OrganizationName;

// Include support for impersonation.
if (flag)
authToken.CallerId = context.UserId;
else
authToken.CallerId = context.InitiatingUserId;

CrmService service = new CrmService();
service.CrmAuthenticationTokenValue = authToken;
service.UseDefaultCredentials = true;

// Include support for infinite loop detection.
CorrelationToken corToken = new CorrelationToken();
corToken.CorrelationId = context.CorrelationId;
corToken.CorrelationUpdatedTime = context.CorrelationUpdatedTime;
corToken.Depth = context.Depth;
//Get the server name form registry
RegistryKey regkey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\MSCRM");

service.Url = String.Concat(regkey.GetValue("ServerUrl").ToString(), "/2007/crmservice.asmx");
service.CorrelationTokenValue = corToken;

return service;
}

}


List of Webservices in Dynamics CRM 2011

Here is the list of services avaialable in CRM 2011. May sametimes you make use of some of it http:// crmserver:5555/AppWebServices/ActivitiesWebService.asmx http:// crmserver:5555/AppWebServices/AdvancedFind.asmx http:// crmserver:5555/AppWebServices/Annotation.asmx http:// crmserver:5555/AppWebServices/AppGridWebService.asmx http:// crmserver:5555/AppWebServices/AssociateRecords.asmx http:// crmserver:5555/AppWebServices/BulkDelete.asmx http:// crmserver:5555/AppWebServices/Connection.asmx http:// crmserver:5555/AppWebServices/Currency.asmx http:// crmserver:5555/AppWebServices/CustomerService.asmx http:// crmserver:5555/AppWebServices/DashboardWebService.asmx http:// crmserver:5555/AppWebServices/DateTimeService.asmx http:// crmserver:5555/AppWebServices/DialogList.asmx http:// crmserver:5555/AppWebServices/DocumentManagementWebService.asmx http:// crmserver:5555/AppWebServices/DuplicateDetection.asmx http:// crmserver:5555/AppWebServices/EmailTemplateService.asmx http:// crmserver:5555/AppWebServices/FormEditorWebService.asmx http:// crmserver:5555/AppWebServices/GanttControlWebService.asmx http:// crmserver:5555/AppWebServices/GoalManagement.asmx http:// crmserver:5555/AppWebServices/ImportJob.asmx http:// crmserver:5555/AppWebServices/ImportWebService.asmx http:// crmserver:5555/AppWebServices/InteractiveWorkflowWebService.asmx http:// crmserver:5555/AppWebServices/LookupMruWebService.asmx http:// crmserver:5555/AppWebServices/LookupService.asmx http:// crmserver:5555/AppWebServices/MailMerge.asmx http:// crmserver:5555/AppWebServices/MarketingAutomation.asmx http:// crmserver:5555/AppWebServices/MergeRecords.asmx http:// crmserver:5555/AppWebServices/MessageBar.asmx http:// crmserver:5555/AppWebServices/MruWebService.asmx http:// crmserver:5555/AppWebServices/OwnerManager.asmx http:// crmserver:5555/AppWebServices/PaneWebService.asmx http:// crmserver:5555/AppWebServices/PresenceService.asmx http:// crmserver:5555/AppWebServices/QueueItem.asmx http:// crmserver:5555/AppWebServices/RecentlyViewedWebService.asmx http:// crmserver:5555/AppWebServices/RegionalOptions.asmx http:// crmserver:5555/AppWebServices/RelatedInformation.asmx http:// crmserver:5555/AppWebServices/RelationshipRolePicklist.asmx http:// crmserver:5555/AppWebServices/reports.asmx http:// crmserver:5555/AppWebServices/ResourceGroupUI.asmx http:// crmserver:5555/AppWebServices/ResourceSpecTree.asmx http:// crmserver:5555/AppWebServices/Ribbon.asmx http:// crmserver:5555/AppWebServices/SalesForceAutomation.asmx http:// crmserver:5555/AppWebServices/SavedQuerySelectorWebService.asmx http:// crmserver:5555/AppWebServices/SchedulePlanning.asmx http:// crmserver:5555/AppWebServices/ScheduleService.asmx http:// crmserver:5555/AppWebServices/ScriptError.asmx http:// crmserver:5555/AppWebServices/Service.asmx http:// crmserver:5555/AppWebServices/Solution.asmx http:// crmserver:5555/AppWebServices/SubjectManager.asmx http:// crmserver:5555/AppWebServices/SystemCustomization.asmx http:// crmserver:5555/AppWebServices/TransactionCurrencyWebService.asmx http:// crmserver:5555/AppWebServices/UserEntityUISettings.asmx http:// crmserver:5555/AppWebServices/UserManager.asmx http:// crmserver:5555/AppWebServices/UserQuery.asmx http:// crmserver:5555/AppWebServices/View.asmx http:// crmserver:5555/AppWebServices/Workflow.asmx http:// crmserver:5555/MSCRMServices/Metadata.asmx http:// crmserver:5555/MSCRMServices/2007/CrmService.asmx http:// crmserver:5555/MSCRMServices/2007/MetadataService.asmx http:// crmserver:5555/MSCRMServices/2007/AD/CrmDiscoveryService.asmx

Reusing code in OnLoad and OnChange event handlers

Somtimes we need to execute the same set of twice once in OnLoad and in OnChange which calls for maintaining two set of codes or copying the same code for other. Again if there is some change in the first one we need to change the second one also.

Here is a simple implementation to avoid it. We will make use of the HTML DOM and include a function it, this function lives until the window is open accessable form all over the form.



var Hello = Function(){
alert("Say hello");
};


// now you can use somthng like this
window.Greet = Hello;

//or

window.Greet = function(){ alert("say hello"); };


To use it you can call this function from any location, like

// calling the function
window.Greet();

Logging JScript Errors to windows event log

Writing Jscript for Dynamics CRM is a tough task, there were no errors is the data is in proper format, but somtimes i wish to log all those information & exception in some place to review/analyse. But since Jscript is not meant for this. Still there is something that you can do with Windows Event Logger. Here is a small script that uses activeXObject to write the log to eventviewer. Later you can view the log by running "eventvwer" in the cmd prompt.

Here is the script



// Creates a log object
Log.prototype = {
// adds functions to it
Err:function(Error){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(1,Error);
},
Success:function(Message){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(0,Message);
},
Warn:function(Warning){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(2,Message);
},
Info:function(Information){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(4,Information);
},
AuditSuccess:function(Message){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(8,Message);
},
AuditFailure:function(Message){
var WshShell = new ActiveXObject("WScript.shell");
WshShell.LogEvent(16,Message);
}
}

// Calling the log to write error
Log.Err("error in script");

Formatting international phone numbers

The CRM SDK contains a sample to format US phone numbers and it works pretty well. However, there are customers outside the US and the sample doesn't work with international phone numbers. An easy formatting rule is replacing any occurrence of '(', ')' or a space with a dash. People can then enter the phone number in their preferred way, but get the same output.


var originalPhoneNumber = "+49 (89) 12345678";
var formattedPhoneNumber = originalPhoneNumber.replace(/[^0-9,+]/g, "-");
formattedPhoneNumber = formattedPhoneNumber.replace(/-+/g, "-");
alert(formattedPhoneNumber);


The first call to the replace method changes every character in the input string that is not a digit and not the plus sign (which is used for international
numbers) to the dash symbol. However, the output is +49--89--12345678, so the second call replaces all occurrences of multiple dashes with a single
one, giving a final result of +49-89-12345678.

Removing a navigation bar entry at runtime

To remove a navigation bar entry dynamically, you can use the following code:

var navigationBarEntry = document.getElementById("navProds");

if (navigationBarEntry != null) {
var lbArea = navigationBarEntry.parentNode;
if (lbArea != null) {
lbArea.removeChild(navigationBarEntry);
}
}

If you haven't already done it, download and install the Internet Explorer Developer Toolbar to find the name of the navigation bar entry.