Tuesday, May 24, 2011

Encrypt appsettings in web.config


Below are the steps to do this

1)   Encrypt appsettings in web.config using Aspnet_regiis.exe
using Aspnet_regiis.exe you can encrypt whole section only not a particular key.so when you need to encrypt a particular key in appSettings things get complicated. Here is a roundabout way of doing this. Modify your web.config

2)   <configsections>
<section type="System.Configuration.NameValueSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="secureAppSettings" />

<appsettings>......</appsettings>

<secureappsettings>
<add key="ipwd" value="secure-password"/>
</secureappsettings>
</configsections>

3)   Under secureAppSettings you can add any key value pairs.

Also secureAppSettings is the name i had given. You can give any. Save this web.config file and go to visual studio command prompt

4)   Type

aspnet_regiis -pef secureAppSettings  “ur web.config path”  -prov DataProtectionConfigurationProvider


5)   In code you can access it as usual

NameValueCollection secureAppSettings = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings")
String str= secureAppSettings["ipwd"];

Saturday, May 21, 2011

SQL Timeouts in CRM 4.0 - import of customizations fail


When importing customizations to CRM 4.0, the import fails with a message "generic SQL error". Digging a bit deeper the error message is really that a timeout has occured. The same error occurs when trying to create a new entity.
I increased the timeout as suggested in the link below
Increasing the timeout:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM\OLEDBTimeout
This value does not exist by default, and if it does not exist then the default value will be 30 seconds. To change it, add the registry value (of type DWORD), and put in the value you want (measured in seconds). Then you have to recycle the CrmAppPool application pool for the change to take effect

By Increasing the time out in the registry will solve the problem. Number of seconds depends on the amount of data in the database.


Tuesday, May 10, 2011

Set Lookup Field While Updating/Creating Record from Jscript Using CRM REST End Point


    var newaccount= new Object();
 
    newaccount.neu_name = "test";
    var contact_ref = new Object();
    contact_ref .Id = contactid;
    contact_ref .LogicalName = "contact";
    contact_ref .Name = "test contact";

    newaccount.primarycontactid = neumodelyear_ref;

//createRecord Method is available in jqueryrestdataoperationfunctions.js in SDK
    createRecord(newaccount, "accountSet", createnewaccountcompleted, null);

References:
http://technet.microsoft.com/es-ar/library/gg334767(en-us).aspx
http://technet.microsoft.com/en-us/library/gg309638.aspx
http://technet.microsoft.com/en-us/library/gg309558.aspx
http://msdn.microsoft.com/en-us/library/gg334427(MSDN.10).aspx

Wednesday, May 4, 2011

Generating EarlyBound CRM Entities using crmsvcutil.exe

To generate XRM.cs navigate to the sdk bin folder from the command prompt and use the below command
NOTE: This command works for crm 2011 online environment

CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration"   /url:https://crmtest.api.crm.dynamics.com/XRMServices/2011/Organization.svc   /out:Xrm.cs   /username:liveid   /password:password   /deviceid: deviceid /devicepassword: devicepassword   /namespace:Xrm   /servicecontextname:XrmServiceContext  /servicecontextprefix:Xrm


References:
http://msdn.microsoft.com/en-us/library/gg695782.aspx

Friday, April 29, 2011

Fetch XML 5000 Records Limitation


Have you ever tried to write a code which will get you all records from a specific entity? It's harder then you think it is! Everybody who is a bit aware of the CRM SDK thinks it should be a fetch statement like this:

<fetch mapping='logical'><entity name='account'><attribute name='accountid'/></entity>

WRONG!
This would only give you the first 5000 records in the database! It is written down in the SDK with small letters, but it could drive you crazy..

There are two solutions for this issue.
1) Add a registery setting to specify not to implement MaxRowsPerPage
2) Modify the fetch statement and merge several results

Here are the details for each solution
1st solution
Search in the SDK for the word "TurnOffFetchThrottling". You should add this as DWORD registery setting to HKLM\Software\Microsoft\MSCRM. Set the value to 1. You will now not have the 5000 records limit.

2nd solution
Modify your fetch statement to include paging and count numbers. Store all the data in an DataSet and perform that series of code over and over again as long as there is data coming.

Here's the script you should use to get all accountid's (for clarity and the ease of use I have added a function called "FetchDataSet").


private DataSet FetchAllAccountIds(){
int i=1;
bool bFinished = false;
DataSet dsAllData = new DataSet();
while (bFinished == false)
{
StringBuilder sbFetch = new StringBuilder();
sbFetch.AppendFormat("<fetch mapping='logical' page='{0}' count='5000'>", i);
sbFetch.Append("<entity name='account'>");
sbFetch.Append("<attribute name='accountid'/>");
sbFetch.Append("<attribute name='new_12_accountid'/>");
sbFetch.Append("</entity>");
sbFetch.Append("</fetch>");
DataSet dsTempResult = FetchDataSet(sbFetch.ToString());
dsAllData.Merge(dsTempResult);
if (dsTempResult.Tables[0].Rows[0]["morerecords"].ToString() == "0")
{
bFinished = true;
}
else
{
i++;
}
}
return dsAllData;
}

private DataSet FetchDataSet(string fetchXml)
{
string strResult = service.Fetch(fetchXml);
DataSet ds = new DataSet();
System.IO.StringReader reader = new System.IO.StringReader(strResult);
ds.ReadXml(reader);
return ds;
}


I hope this saves you some time!

Tuesday, April 26, 2011

Update Entity Record Using Rest EndPoint

Updating Complex Attribute types using rest end point in Jscript

          
            var lineitem = new Object();

             //Money Type Attribute
             var neu_amountchange_ref = new Object();
             neu_amountchange_ref.Value = neu_amountchange;
             lineitem.neu_AmountChange = neu_amountchange_ref;

             lineitem.neu_PercentChange = neu_percentchange;
             //updateRecord exists in JQueryRESTDataOperationFunctions.js
             updateRecord(id, lineitem, "neu_whatifmodellineitemSet", updateWhatifLineItmeCompleted, null);

Wednesday, April 20, 2011

Parse Json Date


 function getDateStringFromJson(jsondate) {

     var date = new Date(parseInt(jsondate.replace(/^\/Date\((\d+)\)\/$/gi, '$1'), 10));
     var curr_date = date.getDate();
     var curr_month = date.getMonth();
     curr_month = curr_month + 1;
     var curr_year = date.getFullYear();
     date= curr_month + '/'+ curr_date + '/'+ curr_year;
     return date;
 }