Thursday, March 29, 2012

Remove duplicate records from Generic List

Following code snippet give you a distinct values from the Generic int List. Here I'm using LINQ query to retrieve Distinct items

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
class Program
static void Main(string[] args)
List listOfItems = new List();


var duplicates = listOfItems
.GroupBy(i => i)
.Select(g => g.Key);

foreach (var d in duplicates)

Out put :


Tuesday, March 20, 2012

CRM 2011 - Update user time zone from SQL query

Recently I wanted to find out users who are not in the correct time zone in CRM System. To change the time zone or user related settings we need to log in to the system by using that particular user's login details (see the below screen shot 1 and 2).

But if you system is up and running it is not possible to ask there login details. so here it the solution. login to the system as System admin, then go to the File , Option and "General" Tab Set the time zone and click "OK".

In CRM we have separate table "[UserSettingsBase] " , which contains all the user related setting information. for an example time zone, currency, language and ..... Use the following script to get the admin users time zone details from the database.

select TimeZoneBias, TimeZoneCode from [UserSettingsBase]
where SystemUserId In (select distinct SystemUserId from SystemUserBase where DomainName like '%DOMAIN\USERNAME%')

Find out which users are not in the correct time zone. use following script.
select FullName , CreatedByName, CreatedOn, DomainName, ModifiedByName from SystemUser
where SystemUserId In (
SELECT SystemUserId
FROM [UserSettingsBase]
where [TimeZoneBias] != -480
and [TimeZoneCode] != 215)
and IsDisabled = 0

If you want to update these users time zone with correct time zone you can simpy use following script. Don't forget to give an iisreset to view your changes.
update [UserSettingsBase]
set [TimeZoneBias] = -330, TimeZoneCode = 200
where SystemUserId IN (select SystemUserId from SystemUser
where SystemUserId In (select SystemUserId
from [PRM_MSCRM].[dbo].[UserSettingsBase]
where [TimeZoneBias] != -480
and [TimeZoneCode] != 215)
and IsDisabled = 0)

Wednesday, March 7, 2012

Working with Visual Ribbon Editor - Adding Button and passing parameters

Hope you remember my previous post. As I explained to you visual ribbon editor is a great tool which we can use easily to create new ribbon button and actions. here I'm explaining how to create a Ribbon button using visual ribbon editor, and pass selected records guids as a parameter to update those records from the back end.

First will see how to add a Ribbon button.

1. Connecting to the CRM server from visual ribbon editor.

2. Click on "Open" button and select the Entity which you want to create a button.

3. Now you have 3 kind of Ribbon types (Form, Homepage and Sub-Grid) in this example I'm creating button against the Homepage.

4. Click on 'New Group' and add a Group to your button

5. Select the Group and Click on "New Button" Update Lable and Id and Select the icon as well.

Ok Now you ready to Go.

Click on Save button. it will save and publish your changes to the CRM.

Now we need to add action to that button click.

Create a web resource and Add a javascript function ex: updateRecordStatus

what this function does is it simply call WCF method to update the record status. Here I'm not going to focus on WCF method.

As you can see in the below image we can specify CRM parameters. Here I'm selecting "Selec

tedControlSelectedItemIds" parameter. This will pass whatever the selected records to the javascript method. Then we can use AJAX to send those IDs to the WCF web service and show the response to the user.

function UpdateRecordStatus(recordIds) {
if (recordIds!= null && recordIds!= "") {
var _serviceUrl = '/ISV/SERVICE/Service.svc/json/UpdateRecordStatus';
var _data = '{"Ids": "' + recordIds + '"}';
type: "POST",
url: _serviceUrl,
data: _data,
contentType: "application/json",
dataType: "json",
cache: false,
async: false,
success: function (response) {
alert("Selected records are updated successfully ")
error: function (response) {

Tuesday, March 6, 2012

How to pass current view id to Custom web page through ribbon button in CRM 2011

In my recent project I wanted to get all the records which are in current view and do some processing and display those records in a custom web page.

My Approach

1. Get the selected View id
2. pass it to the custom web page as query string through ribbon button.
function LoadRecords() {
var selectedViewId = document.getElementsByTagName("span");

var el = getElementsByAttribute("span", "currentview");
var viewId = el[0].id;

var UserID = GetCurrentUserID();
var sUrl = '/ISV/myCustomPage.aspx?crmuserid=' + UserID + '&viewid=' + viewId;
var sWin =, '', 'height=750 ,width=900, left=75, top=50 ,toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,modal=yes');

function getElementsByAttribute(strTagName, strAttributeName) {
var arrElements = document.getElementsByTagName(strTagName);

var arrReturnElements = new Array();
var oCurrent;
var oAttribute;
for (var i = 0; i < arrElements.length; i++) {
oCurrent = arrElements[i];

oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
if (typeof oAttribute == "string" && oAttribute.length > 0) {
return arrReturnElements;

3. get the selected view from SavedQuery table
public SavedQuery GetSavedQueryById(Guid viewId)
SavedQuery savedQuery = null;
if (!viewId.Equals(Guid.Empty))
savedQuery = (SavedQuery)service.Retrieve(SavedQuery.EntityLogicalName, viewId, new ColumnSet(true));
return savedQuery;

4. extract the conditions for selected view from FetchXml column

private Dictionary<string, string> getCoditionListForSelectedView(string selectedView)
Dictionary<string, string; condList = new Dictionary<string, string>();
commonFacadecs = new CommonFacade();
string fetchXML = commonFacadecs.GetSavedQueryById(viewId).FetchXml.ToString();

string formattedXML = XElement.Parse(fetchXML).ToString();

XmlDocument xmlDoc = new XmlDocument();
XmlNodeList nodeList = xmlDoc.SelectNodes("/entity");
XmlNodeList nodeList2 = xmlDoc.GetElementsByTagName("condition");

for (int i = 0; i &lt; nodeList2.Count; i++)
XmlAttributeCollection xmlAttrc = nodeList2[i].Attributes;
string name = xmlAttrc[0].Value;
string oper = xmlAttrc[1].Value;
string Value = xmlAttrc[2].Value;
condList.Add(oper, Value);
return condList;
catch (Exception exception)

5.Retrieve records according to the filter condition


Above approach will give you the all the records which are coming from the selected view
then you can do whatever the processing and display it in custom web page.

MS CRM 2011 KB Article customization Issue.

Recently I have encountered some issue while customizing Kb Article Entity. I was doing following configuration in Article form. 1. Add Ba...