Friday, November 23, 2012

Update owner in Plugin MS CRM 2011

This code should run in a POST plug-in:

SecurityPrincipal assignee = new SecurityPrincipal();
assignee.Type = SecurityPrincipalType.User;

// PrincipalId is some known Guid belonging to the user or team that will own this record.
assignee.PrincipalId = new Guid("guid");

// Create the target object for the request.
TargetOwnedAccount target = new TargetOwnedAccount();

//use the below code for custom entity
//TargetOwnedDynamic dyn = new TargetOwnedDynamic();
//dyn.EntityName = "new_book";
//dyn.EntityId = id;

// Set the properties of the target object.
// EntityId is some known Guid belonging to the account that is being assigned to the user.
target.EntityId = id;

// Create the request object.
AssignRequest assign = new AssignRequest();

// Set the properties of the request object.
assign.Assignee = assignee;
assign.Target = target;

// Execute the request.
ICrmService service = context.CreateCrmService(true);
try
{
AssignResponse assignResponse = (AssignResponse)service.Execute(assign);
}
catch (Exception ex)
{
//TODO: Exceptionhandling
}

Thursday, November 22, 2012

Get rid of "Do you want to close this window?" in CRM

You might get this question when opening CRM in Internet Explorer:

The webpage you are viewing is trying to close the window.
Do you want to close this window?
yes no

This message appears in CRM 4.0 only when you are using Internet Explorer 7.0 and you have enabled the application mode setting.Nevertheless, it is an annoying message which you can get rid of!

To do so, open the default.aspx file which resides in the root of the CRM Web site. In this file there are these three lines of code:

var oMe = window.self;
oMe.opener = window.self;
oMe.close();
Modify the second line of this snippet and end up with these three lines:
var oMe = window.self;
oMe.open('','_self','');
oMe.close();

You will no longer receive that message.  Keep in mind that following any update or migration, you might need to reapply the change.

Multiple Column Sort in CRM

Have you ever wondered how to sort multiple columns on an Entity Grid
view?  It's quite simple.
1. Click on the first column header
2. Hold down "shift" key
3. Click on the second column header
Now you will have a second column as the sub-group of the first.

Thursday, November 8, 2012

Retreive parent record values from child entity lookup


function retrieverecord() {
  if (Xrm.Page.getAttribute("childlookupid").getValue() != null) {
    //acount guid no
    var parentcustomerID = Xrm.Page.data.entity.attributes.get("childlookupid").getValue()[0].id;
    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'>" +
    GenerateAuthenticationHeader() +
    "<soap:Body>" +
    "<Retrieve xmlns='http://schemas.microsoft.com/crm/2007/WebServices'>" +
    "<entityName>parentEntity</entityName>" +
    "<id>" + parentcustomerID + "</id>" +
    "<columnSet xmlns:q1='http://schemas.microsoft.com/crm/2006/Query' xsi:type='q1:ColumnSet'>" +
        "<q1:Attributes>" +
            "<q1:Attribute>parentattributevalue[attributenames]</q1:Attribute>" +            
        "</q1:Attributes>" +
    "</columnSet>" +
    "</Retrieve>" +
    "</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/Retrieve");
    xmlHttpRequest.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    xmlHttpRequest.setRequestHeader("Content-Length", xml.length);
    xmlHttpRequest.send(xml);
    var resultXml = xmlHttpRequest.responseXML;

    var errorCount = resultXml.selectNodes('//error').length;

    if (errorCount != 0) {
        var msg = resultXml.selectSingleNode('//description').nodeTypedValue;
        alert(msg);
    }
    //Display the retrieved value.
    else {
        alert(resultXml.selectSingleNode("//q1:parentattributevalue").nodeTypedValue);
        var cost = resultXml.selectSingleNode('//q1:parentattributevalue').nodeTypedValue;
         
       Xrm.Page.getAttribute("childattribute").setValue(parseInt(cost));
//for retreiving the option set text
//resultXml.selectSingleNode('//q1:parentattributevalue').attributes[0].text;
    }
}
}

Thursday, November 1, 2012

Connecting to CRM Online from an outside caller


Open Visual Studio
Add the following references from the SDK\bin folder.
Microsoft.Xrm.Client.dll
Microsoft.Xrm.Sdk.dll

Add the following references from .NET.
System.Data.Services.dll
System.Data.Services.Client.dll
System.Runtime.Serialization.dll

DEVICE REGISTRATION
You can skip this next step and jump to CREATE EARLY BOUND TYPES if you follow the enhancement in the Eliminating manual device registration post.

There’s a section titled “To Generate your individual device ID and password” in the Create Early Bound Entity Classes with the Code Generation Tool (CrmSvcUtil.exe), but it’s easy to miss.  Follow the instructions from that section:

Open and build the DeviceRegistration project: SDK\Tools\DeviceRegistration\DeviceRegistration.csproj.
Run the executable file from the command line. To register your device, set the /operation parameter to Register.
C:\deviceregistration.exe /operation:Register
Copy the displayed device ID and password values and use them as the deviceid and devicepassword parameter values when you run the CrmSvcUtil tool.

Generate the Class by using below command
crmsvcutil.exe /url:https://demo1.api.crm5.dynamics.com/XRMServices/2011/Organization.svc /o:demo1.cs /n:demo1namespace /u:"emailid" /p:"password" /di: DeviceID /dp: DevicePassword /serviceContextName:demo1context

Add the webform
Add the GridView
goto--> Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using demo1namespace;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Client;
using Microsoft.Crm.Sdk;
using System.ServiceModel;
using System.ServiceModel.Description;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Metadata;


public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClientCredentials cc = new ClientCredentials();
        cc.UserName.UserName = "";
        cc.UserName.Password = "";
        ClientCredentials dc = new ClientCredentials();
        dc.UserName.UserName = "DeviceID";
        dc.UserName.Password = "DevicePassword";
        Uri url = new Uri("https://demo.crm5.dynamics.com/XRMServices/2011/Organization.svc");
        using (OrganizationServiceProxy proxy = new OrganizationServiceProxy(url, null, cc, dc))
        {
            proxy.EnableProxyTypes();
            IOrganizationService orgserivce = (IOrganizationService)proxy;
           demo1context context = new demo1context(proxy);
            var query = from con in context.ContactSet
                        where con.LastName.Contains("sample")
                        select new
                        {
                            con.MiddleName,
                            con.LastName,
                            con.FirstName,
                            con.CreatedOn,
                            con.CreatedBy,
                        };
            GridView1.DataSource = query;
            GridView1.DataBind();
        }
    }
}


Monday, October 22, 2012

how to hide marketing module for some user roles in ms crm 2011?

We have sale, marketing and service modules in the CRM. We want to show the sale module only to the sales user,  marketing module only to marketing users, service module to service users.

Create a custom Entity named “HideModule”.

Export the site map[Keep a backup].

Edit the sitemap.
In sales for all the SubArea add the Privilege only to “create” on the custom entity “HideModule”.  
<Privilege Entity="new_HideModule" Privilege="Create" />
In Marketing for all the SubArea add the Privilege only to “Read” for the custom entity “HideModule”.  
<Privilege Entity="new_HideModule" Privilege="Read" />
In Settings for all the SubArea add the Privilege only to “Write” for the custom entity “HideModule”.  
<Privilege Entity="new_HideModule" Privilege="Write" />

Save and import the soluntion.

In security roles give the Create Privilege on “HideModule” to all sales roles, Read Privilege on “HideModule” to all marketing roles, Write Privilege on “HideModule” to all service roles.  

Wednesday, October 3, 2012

query to kill all the process [used when restoring the database]


use  master
DECLARE @dbname sysname
SET @dbname = 'DataBase Name'

DECLARE @spid int
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname)
WHILE @spid IS NOT NULL
BEGIN
EXECUTE ('KILL ' + @spid)
SELECT @spid = min(spid) from master.dbo.sysprocesses where dbid = db_id(@dbname) AND spid > @spid
END