Tuesday, February 10, 2009

How to call javascript functions from a custom menuitem

In the previous post we learned how to add custom menus. Now sometimes we need to call some custom functions for some information processing. So for that we can write a javascript function and call that javascript function from the MenuItem click.

for example: we added a MenuItem "Custom JavaScript function" at our desired entity location and wish to call our custom function Myfunction on MenuItem click
so we need to modify the ISV.config as mentioned below:


<MenuItem JavaScript="Myfunction()">
<Titles>
<Title LCID="1033" Text="Custom JavaScript function" />
</Titles>
</MenuItem>


Now place your function in the entity onload event and expose the Myfunction function to the page level e.g. this.Myfunction = Myfunction;


function Myfunction()
{
//Place Your logic Here...
}

this.Myfunction = Myfunction;




Now when you click on the MenuItem "Custom JavaScript function" the function Myfunction will be called.

Adding Custom Menu items

MSCRM provides liberty to add your own CustomMenu to cater your customized needs.
for that purpose all you need to do is to edit the ISV.config file located at path /_Resources/.
This is an xml file so we can say what you all need to do is to customize this xml to suit your needs and you are done.

To add a custom menu MSCRM provides CustomMenu tag that can be introduced in the ISV.config at the following locations:
  1. The MenuBar element inside the Grid can contain an ActionMenu element and one or more Button elements.
  2. The MenuBar element inside the Root or entity element can contain a CustomMenus element.



Adding Action Menu Links to Grid:


<grid>
<menubar>
<actionsmenu>
<menuitem title="required link 2" url="http://www.yourdomain.com/desiredfilenew.aspx" winmode="1">
<menuspacer>
</actionsmenu>
<buttons>
<button title="Test" tooltip="custom button" url="http://www.yourdomain.com/desiredActionfile.aspx" winmode="2" icon="/_imgs/ico_18_debug.gif">

<toolbarspacer>
</buttons>
</menubar>
</grid>




Adding Menu Links to Entities:


<entities>
<entity name="account">
<menubar>
<custommenus>
<menu title="Custom Menu">
<menuitem title="my file" url="http://www.yourdomain.com/desiredfile.aspx" validforupdate="1" validforcreate="0" availableoffline="false">
</menu>
</custommenus>
</menubar>
</entity>
</entities>



Adding Menu Links to Root

<!-- The main Global Menu Bar located at the top of all root level areas -->
<MenuBar>
<!-- Custom Menus that appear between the Goto Menu and the Help Menu -->
<CustomMenus>
<Menu>
<Titles>
<Title LCID="1033" Text="ISV" />
</Titles>
<MenuItem Url="http://www.microsoft.com">
<Titles>
<Title LCID="1033" Text="New Window" />
</Titles>
</MenuItem>
<MenuSpacer />
<!-- A horizontal drop down menu spacer -->
<SubMenu>
<Titles>
<Title LCID="1033" Text="ISV Tests" />
</Titles>
<MenuItem Url="http://www.microsoft.com" WinMode="2">
<Titles>
<Title LCID="1033" Text="Modeless Dialog" />
</Titles>
</MenuItem>
<MenuSpacer />
<MenuItem Url="http://www.microsoft.com" WinMode="1">
<Titles>
<Title LCID="1033" Text="Modal Dialog" />
</Titles>
</MenuItem>
<MenuSpacer />
<MenuItem JavaScript="alert('test');">
<Titles>
<Title LCID="1033" Text="Custom JavaScript" />
</Titles>
</MenuItem>
</SubMenu>
</Menu>
</CustomMenus>
</MenuBar>

Monday, February 2, 2009

Error messages in CRM

Most of us while working on CRM, faces error codes returns by CRM. CRM does give you error messages every now and then. The message is usally in the format of "0x12345678". The numbers do not tell much, but the corresponding error message do help you definitely. These error messages are given in the SDK and on a lot of places on the internet, but there the "0x" has been removed from the error message. Here is the same list as which is listed in the SDK, but now with the included 0x, posted by Ronald in his blog.

Do you wanna get rid of "Do you want to close this window?"

I always got this annoying question whenever i opened CRM untill I found the below mentioned code posted by Ronald Lemmen on his blog post here , but its no more now, cheers to Ronald.
You might get a question everytime you open CRM in Internet Explorer.
The question is:
The webpage you are viewing is trying to close the window.
Do you want to close this window?
yes no
This message does appear 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 anoying message which you can get away!To get rid of this
message:
open the default.aspx file which resides in the root of the CRM website.
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 now will not have the message anymore.
Note:Keep in mind that any update or migration might remove this change, but you should be able to reapply the change easily again.

Metadata Browser

The Metadata Browser is a set of web pages which lets you view an Organization’s Entities, Attributes and Relationships.
You can see a quick overview of the metadata for your organization by using the metadata browser. This live view of the metadata, which shows only published information, is found here:

http://yourservername[:port]/yourorganizationname/sdk/list.aspx

You can use this to view the definition for all entities and attributes in your organization, both system and custom.
If you click an icon in the Definition column, you will see a view of all the attributes in the selected entity. At the top of this view you can view the ownership type of the entity, its type code, whether it is customizable, and if it is valid for Advanced Find.
For each attribute you can view its type, whether it is valid for create, update, and retrieve operations, and so on.

Retrieving the Web Service Version

The Web service version is needed to support ISV applications that can easily detect and work with multiple versions of Microsoft Dynamics CRM. The RetrieveVersion message provides support for determining the version, or build number, of the Microsoft Dynamics CRM software installed on the server.
Example : The following code retrieves the version of the server software and produces output like this: "4.0.7283.0"



// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";
CrmService service = new CrmService();
service.Url = "http://<servername>:<port>/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

RetrieveVersionRequest versionRequest = new RetrieveVersionRequest();
RetrieveVersionResponse versionResponse = (RetrieveVersionResponse)service.Execute(versionRequest);
Console.WriteLine(versionResponse.Version.ToString());

CRM Web Services

Microsoft Dynamics CRM 4.0 provides a dynamic Web service interface for applications to use to access and manipulate platform data as well as interact with platform services. These services allow ISVs to write applications using Microsoft Visual Studio or other developer tools by simply referencing the platform Web service. The Web services exposed by the platform are WSI BP 1.1 compliant. This compliance support makes the Web services interoperable with non-Microsoft platforms.
The Microsoft Dynamics CRM SDK includes the following Web services:
1. Discovery Web Service (DiscoveryService.asmx)
2. CRM Web Service (CrmService.asmx)
3. Metadata Web Service (MetadataService.asmx)
The Discovery Web service is a mechanism to find the correct CrmService endpoint for your organization or to obtain a CRM ticket for Internet-facing deployment (IFD) or for Microsoft Dynamics CRM Online.
The CRM Web service provides strongly typed access to all entities in Microsoft Dynamics CRM, including custom entities and attributes. This Web service also allows execution of all supported operations, including those with built-in business logic as well as specialized operations. It provides a valid Web Services Description Language (WSDL) that is dynamically generated on the server to include the latest customizations and provides a single endpoint for your code.
The Metadata Web service provides methods to read and write the metadata for an organization. This includes the definitions for entities, attributes, and relationships.
In Microsoft Visual Studio 2005, you use the Add Web Reference command to point to the Web services. After the Web reference has been added to your project, all of the Web methods and classes in the Web service's namespace are available in your project.
For more information about Web services, see The Microsoft Web Services Developer Center msdn.microsoft.com/webservices/. You can also read the article "XML Web Services Basics" here

Custom Entities

A custom entity is a business entity whose schema is created through the customization tools in the Microsoft Dynamics CRM Web application or by using the Metadata Web service. This feature enables you to provide important extensions to support vertical business requirements. It also makes building on the Microsoft Dynamics CRM platform easier, enabling ISVs, for example, to produce solutions that integrate seamlessly with Microsoft Dynamics CRM.
Custom Entity Life Cycle
Create
– The schema is created and an empty form is published.
Update - Schema changes are saved to the metadata but UI changes remain unpublished.
Publish - UI changes are published and are visible in the application.
Delete - Schema changes and the UI is changed immediately, no publish step is needed.
When you create a custom entity using the Web application, you enter the schema name for the entity. This name is used to create the logical name and the filtered view name.

Custom entities also support the following functionality:

You can create forms, views, and reports to expose your new entities in Microsoft Dynamics CRM.
You can publish, export, and import these new entities and their associated forms and views.
You can include these new entities in the dataset that Microsoft Dynamics CRM for Microsoft Office Outlook users can take offline.
You can secure these new entities in the same manner as other Microsoft Dynamics CRM entities.
The security privileges available for organization-owned custom entities are as follows: create, read, write, delete, append, and append to.
The security privileges available for user-owned custom entities are as follows: create, read, write, delete, append, append to, assign, and share.
You can define relationships for custom entities.
Callouts for custom entities function in the same manner as for system entities.
Client-side events function in the same manner as those for system entities.
Custom entities can be extended using the ISV.config file in the same manner as system entities.
The filtered views for custom entities are available to be used in the same manner as for system entities.

Accessing the Server Using the Web Service

Accessing the Server Using the Web Service
Methods are called by first creating an instance of the Web service. In Microsoft Visual Studio 2005, Microsoft IntelliSense support is provided on the Web service methods. To access the server, you may need to specify the URL as shown here for the CrmService.
Example
The following is a simple example accessing the Web service. For a complete set of samples for different authentication scenarios, see Web Service Security and Impersonation.



// Set up the CRM Service.
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = "AdventureWorksCycle";

CrmService service = new CrmService();
service.Url = "http://<servername>[:<port>]/mscrmservices/2007/crmservice.asmx";
service.CrmAuthenticationTokenValue = token;
service.Credentials = System.Net.CredentialCache.DefaultCredentials;

The Web references to the Microsoft Dynamics CRM Web services define a default value for the service's URL property. These default URL values contain the Web service addresses for the Microsoft Dynamics CRM server that was used when you created the Web references. If you need to point your application at a different Microsoft Dynamics CRM server at run time, you must set the CrmService.Url property to the address of the Web services on that server.
Note As a best practice, you can store the URL of the Microsoft Dynamics CRM server in the application configuration file for your custom code.
If you wish to run your code against a Microsoft Dynamics CRM server on a different domain, you cannot use the default network credentials. You must define valid domain credentials for the domain that has Microsoft Dynamics CRM access rights. Alternatively, you can establish a trust between the two domains.

Important You must assign a CrmAuthenticationToken value to the Microsoft Dynamics CRM Web service instance before calling Web service methods. Failure to do so results in an HTTP 401 error when the Web service methods are called.

GenerateAuthenticationHeader

The GenerateAuthenticationHeader function retrieves a properly formed Microsoft Dynamics CRM authentication header. This authentication header automatically determines the appropriate authentication type to use for each deployment type: on-premise, IFD, or Microsoft Dynamics CRM Online. Note that the SOAP header for IFD does not include information about the CrmTicket because a session-based ticketing system is used. For more information, see Authentication.

Each XMLHttp request requires a <soap:Header>
Example
You can generate this header for your code by using the following function:
GenerateAuthenticationHeader();
Without this function you would need to define a SOAP header in your code like the one here for the on-premise version:

<soap:Header>
<CrmAuthenticationToken xmlns="http://schemas.microsoft.com/crm/2007/WebServices">
<AuthenticationType xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
0
</AuthenticationType>
<OrganizationName xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
AdventureWorksCycle
</OrganizationName>
<CallerId xmlns="http://schemas.microsoft.com/crm/2007/CoreTypes">
00000000-0000-0000-0000-000000000000
</CallerId>
</CrmAuthenticationToken>
</soap:Header>

CRM SDK Assemblies

The Microsoft Dynamics CRM SDK includes the following assemblies:
Microsoft.Crm.Sdk.dll
This assembly contains the base classes needed to develop custom workflows and workflow activities. It also contains helper classes which can be used when you develop plug-ins and workflows.
Microsoft.Crm.SdkTypeProxy.dll
This assembly contains types needed when you develop plug-ins and custom workflows. These include the request/response classes and a set of methods that are used for instantiating CRM types can be found in Microsoft.Crm.SdkTypeProxy.CrmTypes.
This assembly also includes the same set of entity classes, such as account class. However, these are for internal use only. When you work with entity instances, you should use the Microsoft.Crm.Sdk.DynamicEntity class or add the CrmService WSDL to your code and use the entity classes found there.
See Microsoft.Crm.SdkTypeProxy Namespace for reference documentation on this assembly.
Microsoft.Crm.Outlook.Sdk.dll
This assembly contains methods and types needed for customizing Microsoft Dynamics CRM for Microsoft Office Outlook.
See Microsoft.Crm.Outlook.Sdk Namespace for reference documentation on this assembly.
Microsoft.Crm.Tools.EmailProviders.dll
This assembly contains methods and types needed for developing a custom E-mail Provider component for the Microsoft Dynamics CRM E-mail Router.
See Microsoft.Crm.Tools.Email.Providers Namespace for reference documentation on this assembly.
Both 32-bit and 64-bit versions of these assemblies can be found in the SDK\Bin folder of the SDK download. The download is located at go.microsoft.com/fwlink/?LinkID=102966.
If you are running in a 32-bit process, use the 32-bit assemblies. If you are running in a 64-bit process, use the 64-bit assemblies.
For synchronous plug-ins, use the version that matches the Microsoft Dynamics CRM server.
For custom workflow activities and asynchronous plug-ins, use the version that matches the Asynchronous service.
For client applications and add-ins, use the version that matches the Web application or Microsoft Dynamics CRM for Outlook.
For Microsoft Dynamics CRM Online, use the version that matches the Web application or Microsoft Dynamics CRM for Outlook.