How to write into the eventlog custom journal with impersonalization in code

private const string LOG_NAME = "MSCRM.Common";

/// Write message in log

public static void WriteLog(string message, Exception exception)
System.Security.Principal.WindowsImpersonationContext ctx = null;
ctx = System.Security.Principal.WindowsIdentity.Impersonate(IntPtr.Zero);
string logSource = "MSCRM.Common.Web.Controls.Grid";

if (EventLog.SourceExists(logSource))
if (!EventLog.LogNameFromSourceName(logSource, ".").Equals(LOG_NAME))
EventLog.DeleteEventSource(logSource, ".");
EventLog.CreateEventSource(logSource, LOG_NAME);
EventLog.CreateEventSource(logSource, LOG_NAME);
EventLog el = new EventLog();
el.Log = LOG_NAME;
el.Source = logSource;
el.WriteEntry(String.Format("{0}\nDetails:\n{1}", message, GetLogInfo(exception)), EventLogEntryType.Error);
catch (Exception ex)
throw ex;

Get selected item in CRM grid

The following snippet will allow you to retrieve an array containing the Id values of the selected records in the grid:

// get array of selected records
var a = document.all['crmGrid'].InnerGrid.SelectedRecords;
var selectedItems = new Array(a.length);
for (var i=0; i < a.length; i++)
selectedItems[i] = a[i][0];

To get all of the records in the grid (ie. “All Records on Current Page”):

// array of all records on current page
var iTotal = document.all['crmGrid'].InnerGrid.NumberOfRecords;
var o = document.all['crmGrid'].InnerGrid;
var allItems = new Array;
var ii = 0;
for (var i=0; i < iTotal; i++)
allItems[ii] = o.rows[i].oid;

The source: http://blog.customereffective.com/blog/2009/02/javascript-snippets-for-working-with-grids-in-crm.html


How to programmatically launch an application under a specific user

ProcessStartInfo psi = new ProcessStartInfo("cmd", @"/C c:\SomeApp.exe");
psi.UserName = "UserName";
psi.Domain = "DomaiName";
psi.Password = ReadPassword("password");
psi.UseShellExecute = false;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
using (Process p = Process.Start(psi))
// if you need to wait the end of the process

public System.Security.SecureString ReadPassword(string password)
System.Security.SecureString secPass = new System.Security.SecureString();
for (int i = 0; i < password.Length; i++)
return secPass;


MSCRM, SQL and Localization

If you need to look at localized name of some attribute thru the SQL then check out next code

select l.*
from MetadataSchema.LocalizedLabel l
join MetadataSchema.Attribute a on l.objectid=a.AttributeId
join MetadataSchema.Entity e on a.EntityId=e.EntityId
where a.name= 'address1_city' and e.name='account'