Monday, February 17, 2014

CRM - Passing data between Plugins

When working with Plugins its quite natural to come across the scenarios where a data needs to be shared by two different plugins. This can be achieved using the shared variables. Below is how it can be done. In the plugin which is going to set the variable write the code as follows,
 
context.SharedVariables["UniqueName"] = "Data to be shared";
 
context is the plugin execution context. Now the data that needs to be shared is available in the shared variable UniqueName.
 
Next step is to consume the data available in the other plugin. For that first check whether the shared variable has the respective UniqueName. If available consume the data in it using the below code.
 
string uniqueName = (string)context.SharedVariables["PrimaryContact"];
 
All done. We have the data from first plugin in second one now. Enjoy !!!

Monday, February 10, 2014

WPF - Supressing Script Errors in WPF WebBrowser control

When Web sites hosted in Webbrowser controls are throwing JScript errors then we have to hide them. WebBrowser controls don't have the property to hide script errors so we have to handle it explicitly or we have to use WinForms WebBrowser control.
 
For the first approach we can use the below method in the browsers navigated event.

private void HideScriptErrors(WebBrowser webBrowser, bool hide)
{FieldInfo fieldInfo = typeof(WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo == null)
return;
object objectWebBrowser = fieldInfo.GetValue(webBrowser);
if (objectWebBrowser == null)
return;
objectWebBrowser.GetType().InvokeMember("silent", BindingFlags.SetProperty, null, objectWebBrowser, new object[] { hide });
}

You can look at the original post here.

This worked for me. Hope it will for you as well !!! :)

Thursday, February 6, 2014

CRM 2013 - Create a SDK code project in Visual Studio

As a developer, there can be scenarios where we have to use CRM SDK in our code so as to connect to CRM and use the messages from SDK. To make sure our VS project is ready to use we have to follow some steps.

Even though the title says 2013 this is applicable to 2011 as well.
 

Prerequisites

Below are the prerequisites for the same.
  • Any Visual Studio (even the express edition works): CRM SDK is built on .NET 4.0 so even VS 2010 will work in this case.
  • CRM SDK: If CRM SDK is not installed on your computer, it can be downloaded from here (CRM 2011, CRM 2013).
  • Windows Identity Foundation: If you are using windows 8 or Windows server 2012, then we have to just enable already existing WIF. If you are using windows 7 or windows server 2008, we have install the WIF 3.5. For installing, download it from here.
 Once all the prerequisites are in place we can start creating a project which will interact with CRM from our code. Follow the below steps for achieving our target,
  1. Create a console application for either C# or VB according to our comfort level.
  2. Make sure the target framework is set to MS .NET framework 4.0. By default it will be client profile.
  3. Add all the required reference to our project. This includes below references.
    1. System.Data.Linq
    2. System.DirectoryServices.AccountManagement
    3. System.Runtime.Serialization
    4. System.Security
    5. System.ServiceModel
  4. Add the required SDK assembly reference as per our requirement. This is available from the bin folder of respective CRM version SDK.
  5. Add the required identity reference file. We can get this file from the file location in the system at Program Files\Reference Assemblies\Microsoft\Windows Identity Foundation\v3.5.
We are done with adding the references and now we are ready to go with our development activity.
 
For every project we have to do this. So its better if we can save this as a project template for our future references.
 
For detailed Microsoft documentation please refer the original site here.
Hope this helped everyone. :) !!!
 
 

Tuesday, January 21, 2014

CRM 2011 - Reflexive relationship must specify direction using ReflexiveManyToManyRelationship

When trying to retrieve data for reflexive relationship I was getting the exception which is the title of this post. How I was able to fix the same was by just using once line of code as below which will mention whether the role is referencing or referenced.
 
relationship.PrimaryEntityRole = EntityRole.Referencing;
 
where relationship is the object Relationship.
 
Hope this helped someone. !!!

Friday, January 3, 2014

Visual C# - Check operator

Check operator tells the runtime to generate an OverflowException rather than overflowing  silently when an integral expression or statement exceeds the arithmetic limits of that type.
The checked operator affects expressions with the ++,--,+,-,*,/ and explicit conversion operators between integral types. Checked can be used around either an expression or a statement block. For example:
int a = 1000000;
int b = 1000000;
int c = checked(a*b); //Checks just the expression.
checked  //Checks all expressions in statement block.
{
 ...
 c = a * b;
 ...
}
If we are using a compiler with the /checked command line then this will check all the expressions in the program.
In such a case if we need to disable overflow checking just for specific expressions or statements, we can do so with the unchecked operator. For example:
int x = int.MaxValue;
int y = unchecked (x + 1); //Leaves the expression from checking.
unchecked  //Leaves all the expressions inside the block from checking.
{
 int z = x + 1;
}
Against all the above statements let's say we have a constant expression then the overflow will be evaluated at the compile time itself unless we are applying the unchecked operator.
int x = int.MaxValue + 1; //Compile time error.
int y = unchecked (int.MaxValue + 1); //No errors.

Friday, December 27, 2013

Visual C# - Avoiding Conflicts with Keywords

As a programmer, we all should be aware that keywords of a programming language cannot be used as identifiers. That is the case for Visual C# as well. We cannot use a keyword as an identifier. As we are aware that .NET framework is multilingual such conflicts can happen when we are referring libraries written in another language. An identifier in the external library can be conflicting with C# language.
The way to avoid this is by using the '@' symbol as a prefix to such identifiers. Ideally we cannot use 'class' as an identifier in C#. But this can be made possible by using it as '@class'. This is legal and is same as 'class'.

This is not the case for contextual keywords. They can be used as identifiers without '@' symbol. Ambiguity cannot arise within the context in which they are used.
Hope this helps !!!

Visual C# - Compiler Options

Below are the list of commands/options available for the Visual C# compiler. These options can be used through the csc.exe executable available along with the .NET framework.
In general case, the executable will be under %SystemRoot%\Microsoft.NET\Framework\<Framework-version>. In this path %SystemRoot% is the windows directory.

OUTPUT FILES

  • /out:<file> - Specify output file name (default: base name of file with main class or first file)
  • /target:exe - Build a console executable (default) (Short form: /t:exe)
  • /target:winexe - Build a Windows executable (Short form: /t:winexe)
  • /target:library - Build a library (Short form: /t:library)
  • /target:module - Build a module that can be added to another assembly (Short form: /t:module)
  • /target:appcontainerexe - Build an Appcontainer executable (Short form: /t:appcontainerexe)
  • /target:winmdobj - Build a Windows Runtime intermediate file that is consumed by WinMDExp (Short form: /t:winmdobj)
  • /doc:<file> - XML Documentation file to generate
  • /platform:<string> - Limit which platforms this code can run on: x86, Itanium, x64, arm, anycpu32bitpreferred, or anycpu. The default is anycpu.

INPUT FILES

  • /recurse:<wildcard> - Include all files in the current directory and subdirectories according to the wildcard specifications
  • /reference:<alias>=<file> - Reference metadata from the specified assembly file using the given alias (Short form: /r)
  • /reference:<file list> - Reference metadata from the specified assembly files (Short form: /r)
  • /addmodule:<file list> - Link the specified modules into this assembly
  • /link:<file list> - Embed metadata from the specified interop assembly files (Short form: /l)

RESOURCES

  • /win32res:<file> - Specify a Win32 resource file (.res)
  • /win32icon:<file> - Use this icon for the output
  • /win32manifest:<file> - Specify a Win32 manifest file (.xml)
  • /nowin32manifest - Do not include the default Win32 manifest
  • /resource:<resinfo> - Embed the specified resource (Short form: /res)
  • /linkresource:<resinfo> - Link the specified resource to this assembly (Short form: /linkres), Where the resinfo format is <file>[,<string name>[,public|private]]

CODE GENERATION

  • /debug[+|-] - Emit debugging information
  • /debug:{full|pdbonly} - Specify debugging type ('full' is default, and enables attaching a debugger to a running program)
  • /optimize[+|-] - Enable optimizations (Short form: /o)

ERRORS AND WARNINGS

  • /warnaserror[+|-] - Report all warnings as errors
  • /warnaserror[+|-]:<warn list> - Report specific warnings as errors
  • /warn:<n> - Set warning level (0-4) (Short form: /w)
  • /nowarn:<warn list> - Disable specific warning messages

LANGUAGE

  • /checked[+|-] - Generate overflow checks
  • /unsafe[+|-] - Allow 'unsafe' code
  • /define:<symbol list> - Define conditional compilation symbol(s) (Short form: /d)
  • /langversion:<string> - Specify language version mode: ISO-1, ISO-2, 3, 4, 5, or Default

SECURITY

  • /delaysign[+|-] - Delay-sign the assembly using only the public portion of the strong name key
  • /keyfile:<file> - Specify a strong name key file
  • /keycontainer:<string> - Specify a strong name key container
  • /highentropyva[+|-] - Enable high-entropy ASLR

MISCELLANEOUS

  • @<file> - Read response file for more options
  • /help - Display this usage message (Short form: /?)
  • /nologo - Suppress compiler copyright message
  • /noconfig - Do not auto include CSC.RSP file

ADVANCED

  • /baseaddress:<address> - Base address for the library to be built
  • /bugreport:<file> - Create a 'Bug Report' file
  • /codepage:<n> - Specify the codepage to use when opening source files
  • /utf8output - Output compiler messages in UTF-8 encoding
  • /main:<type> - Specify the type that contains the entry point (ignore all other possible entry points) (Short form: /m)
  • /fullpaths - Compiler generates fully qualified paths
  • /filealign:<n> - Specify the alignment used for output file sections
  • /pdb:<file> - Specify debug information file name (default: output file name with .pdb extension)
  • /errorendlocation - Output line and column of the end location of each error
  • /preferreduilang - Specify the preferred output language name.
  • /nostdlib[+|-] - Do not reference standard library (mscorlib.dll)
  • /subsystemversion:<string> - Specify subsystem version of this assembly
  • /lib:<file list> - Specify additional directories to search in for references
  • /errorreport:<string> - Specify how to handle internal compiler errors: prompt, send, queue, or none. The default is queue.
  • /appconfig:<file> - Specify an application configuration file containing assembly binding settings
  • /moduleassemblyname:<string> - Name of the assembly which this module will be a part of

Configuration for CRM Plugins

CRM plugins are always great feature where we can automate many functionality which we cannot automate from user interface. But almost eve...