Tuesday, April 26, 2011

ISV Button On Click To Process Selected Items in an Aspx page

I was looking through the net for code to get all selected items from the CRM grid view and couldn't find the complete solution. Here I got the code for implementing the solution for an ISV button calling aspx page and getting all selected items from the grid view in Dynamics CRM.

// ISV button added to the customisation and the javascript method calls your aspx page...

< Button Icon="/_imgs/ico/16_send.gif" Url="" JavaScript="function post_to_url(path,param){var myForm = document.createElement('form');myForm.setAttribute('method', 'post'); myForm.setAttribute('action', path); myForm.setAttribute('target','SomePage'); var pInput = document.createElement('input'); pInput.setAttribute('name', 'guids'); pInput.setAttribute('value', param); myForm.appendChild(pInput); document.body.appendChild(myForm);var lValue= 50; var tValue=50; window.open(path,'SomePage','scrollbars=yes,menubar=no,height=750,width=800,resizable=no,toolbar=no,location=no,status=no,'+'top='+tValue+',left='+lValue); myForm.submit(); document.body.removeChild(myForm);}; var aList = document.all['crmGrid'].InnerGrid.SelectedRecords; if (aList.length > 0) { var guidIds=''; for (j=0; j < aList.length; j++ ) { guidIds += aList[j][0]+','+aList[j][1]+':'; }; post_to_url('/ISV/WebPages/MyCustomPage.aspx',guidIds);};" WinParams="dialogHeight: 2px; dialogWidth: 5px;" WinMode="2" PassParams="1" >


// ASPX page OnLoad
// =================================================
if (Context != null && Context.Request.InputStream.Length > 0)
{
string[] selectedEntities = GetSelectedEntities(Context);
string guidValue = null;
string[] selectedids = null;
StringBuilder emailList = new StringBuilder();
ViewState["Guids"] = selectedEntities;
foreach (string ids in selectedEntities)
{
selectedids = ids.Split(new char[] { ',' });
guidValue = selectedids[0];

if (guidValue != null && guidValue != String.Empty)
{
// Write your code with the guid.

}
}
}

The following code will return you the string array of selected records guid from the CRM Grid.

///
/// Extract Selected Entities from Context
///

/// Context
///
private string[] GetSelectedEntities(HttpContext context)
{

// Read the Xml data that is passed in the HttpRequest stream.
StreamReader sr = new StreamReader(context.Request.InputStream);
string recordIds = sr.ReadToEnd();
if (string.IsNullOrEmpty(recordIds))
return new string[0];

recordIds = recordIds.Replace("%7B", "{").Replace("%7D", "}").Replace("%3A", ":").Replace("%2C", ",");
return recordIds.Substring(6).Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
}


Hope this helps. Post me any questions...

Thanks

No comments: