Friday, June 7, 2013

Remove duplicates from DataSet in .Net

da.Fill(ds,"Details");
DataTable dt = ds.Tables["Name"];
RemoveDuplicateRows(dt, "Name"); // Here Name is Column name of table
gvDetails.DataSource = ds;
gvDetails.DataBind();

// This method is used to delete duplicate rows of table
public DataTable RemoveDuplicateRows(DataTable dTable, string colName)
{
Hashtable hTable = new Hashtable();//hash table is used to store different types of data.It will take two parameters as input[Value and Name].
ArrayList duplicateList = new ArrayList();
foreach (DataRow dtRow in dTable.Rows)
{
if (hTable.Contains(dtRow[colName]))
duplicateList.Add(dtRow);
else
hTable.Add(dtRow[colName], string.Empty);
}
foreach (DataRow dtRow in duplicateList)
dTable.Rows.Remove(dtRow);
return dTable;
}

Maintain the state of Checkbox values in GridView

Write the below two methods[PageIndexChanging] to save the checkbox values in gridview when paging is set to true.
        private void SaveCheckedValues()
        {
            ArrayList userdetails = new ArrayList();
            int index = -1;
            foreach (GridViewRow gvrow in gvdetails.Rows)
            {
                index = (int)gvdetails.DataKeys[gvrow.RowIndex].Value;
                bool result = ((CheckBox)gvrow.FindControl("chkSelect")).Checked;

                // Check in the Session
                if (Session["CHECKED_ITEMS"] != null)
                    userdetails = (ArrayList)Session["CHECKED_ITEMS"];
                if (result)
                {
                    if (!userdetails.Contains(index))
                        userdetails.Add(index);
                }
                else
                    userdetails.Remove(index);
            }
            if (userdetails != null && userdetails.Count > 0)
                Session["CHECKED_ITEMS"] = userdetails;
        }
        private void PopulateCheckedValues()
        {
            ArrayList userdetails = (ArrayList)Session["CHECKED_ITEMS"];
            if (userdetails != null && userdetails.Count > 0)
            {
                foreach (GridViewRow gvrow in gvdetails.Rows)
                {
                    int index = (int)gvdetails.DataKeys[gvrow.RowIndex].Value;
                    if (userdetails.Contains(index))
                    {
                        CheckBox myCheckBox = (CheckBox)gvrow.FindControl("chkSelect");
                        myCheckBox.Checked = true;
                    }
                }
            }
        }

Thursday, June 6, 2013

To show the GridView with no rows/data

Add the below code before binding the data to GridView
 if (ds.Tables[0].Rows.Count == 0)
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            grdview.DataSource = ds;
            grdview.DataBind();
            int columncount = grdview.Rows[0].Cells.Count;
            grdview.Rows[0].Cells.Clear();
            grdview.Rows[0].Cells.Add(new TableCell());
            grdview.Rows[0].Cells[0].ColumnSpan = columncount;
            grdview.Rows[0].Cells[0].Text = "No Records Found";
        }
        else
        {
            grdview.DataSource = ds.Tables[0];
            grdview.DataBind();
        }

Friday, May 10, 2013

Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive

I'm trying to up load my site and I'm getting this error message:
    Unrecognized attribute 'targetFramework'. Note that attribute names are case-sensitive.

<compilation debug="true" targetFramework="4.0">

The site works fine on my local pc but won't open when I loaded it to my host at tried to view it on line.

Registering the framework with IIS is what worked for me:
C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319>aspnet_regiis -i
Type the above statement in command prompt

Wednesday, May 1, 2013

CRM 2011 JScript Syntaxes

XRM Page Model.

The Xrm.Page object serves as a namespace object to consolidate three properties on the form:

Xrm.Page.context
Xrm.Page.context provides methods to retrieve information specific to an organization, a user, or parameters that were passed to the form in a query string.

Xrm.Page.data.entity

Xrm.Page.data provides an entity object that provides collections and methods to manage data within the entity form.

Xrm.Page.ui
Xrm.Page.ui provides collections and methods to manage the user interface of the form.




CRM Enity Properties
  •  Xrm.Page.data.entity.getEntityName()   //Entity Name
  •  Xrm.Page.data.entity.getId()                     //Record GUID
  •  Xrm.Page.ui.getFormType()                      //CRM Form Type (Integer)
    **Create (1), Update (2), Read Only (3), Disabled (4), Bulk Edit (6)
Get & Set Text Field
  • Xrm.Page.data.entity.attributes.get(“fld_name”).getValue()
  • Xrm.Page.data.entity.attributes.get(“fld_name”).setValue(“First”)
Get Lookup
  •      Xrm.Page.data.entity.attributes.get(“primarycontactid”)
  •     Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].id
  •     Xrm.Page.data.entity.attributes.get(“primarycontactid”).getValue()[0].name
Set Lookup
  •  var lookUpValue = new Array();
    lookUpValue[0] = new Object();
    lookUpValue[0].id = idValue;
    lookUpValue[0].name = textValue;
    lookUpValue[0].entityType = ‘{Entity_Name}’;
  • Xrm.Page.getAttribute(“primarycontactid”).setValue(lookUpValue);
Option Set Properties
  •  Xrm.Page.data.entity.attributes.get(“new_country”)
  • Xrm.Page.data.entity.attributes.get(“new_country”).getOptions()   // All Options
  •  Xrm.Page.data.entity.attributes.get(“new_country”).getText()         // Option Label value
  •  Xrm.Page.data.entity.attributes.get(“new_country”).getValue()       // Selected option value
Hide/Show controls
  • Xrm.Page.ui.controls.get(“name”).setVisible(true/false)
Disable/Enable controls
  •  Xrm.Page.ui.controls.get(“name”).setDisabled(true/false)
Set Focus
  • Xrm.Page.ui.controls.get(“name”).setFocus();
Set field’s Requirement Level
  • Xrm.Page.getAttribute(“name”).setRequiredLevel(“none”);
  • Xrm.Page.getAttribute(“name”).setRequiredLevel(“required”);
  •  Xrm.Page.getAttribute(“name”).setRequiredLevel(“recommended”);
Set the label for the control
  •  Xrm.Page.ui.controls.get(“name”).setLabel(“Label Text”);
Set URL for IFrams/WebResource
  •  Xrm.Page.ui.controls.get(“IFrame/WebResource Name”).setSrc(“URL”);
Get Parent of current control
  •  Xrm.Page.ui.controls.get(“name”).getParent();
“Current User” & “Organization” details
  • Xrm.Page.context.getUserId()                     //Current User ID
  •  Xrm.Page.context.getOrgUniqueName() //Organization Name
  •  Xrm.Page.context.getServerUrl()              //Server URL
  •  Xrm.Page.context.getUserRoles()             //Role ID’s of current user
Check isDirty
  • Xrm.Page.data.entity.getIsDirty()  // Form is IsDirty
  • Xrm.Page.data.entity.attributes.get(“new_name”).getIsDirty()  //Field is IsDirty
Save Form
  • function save(){Xrm.Page.data.entity.save();}
Save&close Form
  • function saveandclose(){Xrm.Page.data.entity.save(“saveandclose”);}
Close Form
  • function close(){Xrm.Page.ui.close();}
Expand/Collapse Tabs
  • var myTab = Xrm.Page.ui.tabs.get(“{tab_name}”);
  • myTab.setDisplayState(“expanded”); // To expand tab
  • myTab.setDisplayState(“collapsed”); // To collapse tab
Hide/Show Tabs & Sections
  • var myTab = Xrm.Page.ui.tabs.get(“{tab_name}”);
  • myTab.setVisible(True/False);
  • var mySec = myTab.sections.get(“{sec_name}”);
  • mySec.setVisible(True/False);

Tuesday, April 30, 2013

Dynamics CRM 2011 - Trigger a workflow from JavaScript

Recently I had a requirement to trigger a workflow on click of ribbon button when it meets certain criteria. Refer below link for calling JavaScript function from custom ribbon button.
http://ankit.inkeysolutions.com/2012/01/crm-2011-how-to-use-visual-ribbon.html
The criteria values are checked using JavaScript and if all criteria are satisfied then the workflow is to be triggered. The function below will take two inputs where entityId is the id of the record for which the workflow will execute and workflowProcessId is the id of the workflow which we want to execute. 




function startWorkflow(entityId, workflowProcessId) {
  var xml = "" +    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
 "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +  Xrm.Page.context.getAuthenticationHeader() +  "<soap:Body>" +"<Execute xmlns=\"http://schemas.microsoft.com/crm/2007/WebServices\">" +"<Request xsi:type=\"ExecuteWorkflowRequest\">" +"<EntityId>" + entityId + "</EntityId>" +  "<WorkflowId>" + workflowProcessId + "</WorkflowId>" + "</Request>" +"</Execute>" +"</soap:Body>" +"</soap:Envelope>";
var xmlHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
xmlHttpRequest.Open("POST", "/mscrmservices/2007/CrmService.asmx", false);
xmlHttpRequest.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/crm/2007/WebServices/Execute");
xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
  xmlHttpRequest.send(xml);
}
 

How to retrieve the Selected Record Ids of a Sub Grid in CRM 2011 using javascript

Retrieve the Selected Record Ids of a Sub Grid in CRM 2011 using javascript
Code Snippet:
var gridControl = document.getElementById("yourSubGridName").control;
var ids = gridControl.get_selectedIds();