<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Janne Mattila's blog : Programming</title><link>http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx</link><description>Tags: Programming</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Modifying application behavior with Detours (for Application Compatibility reasons)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/12/03/modifying-application-behavior-with-detours-for-application-compatibility-reasons.aspx</link><pubDate>Thu, 03 Dec 2009 21:06:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9932200</guid><dc:creator>jannemattila</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9932200.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9932200</wfw:commentRss><description>&lt;p&gt;My &lt;a href="http://blogs.msdn.com/jannemattila/archive/2009/11/26/tombo-in-windows-7.aspx" target="_blank"&gt;previous post&lt;/a&gt; was about Application Compatibility Toolkit and shims. But what if you have a bit different situation... &lt;/p&gt;  &lt;p&gt;Imagine that I have my good old &lt;em&gt;My Windows App&lt;/em&gt; application that has been working correctly in the past. Now it’s not maintained anymore and it seems that no one has created similar application that fulfills my needs... And now something has come up that makes this application useless in your Windows 7. Let’s take a look.&lt;/p&gt;  &lt;p&gt;Here’s my &lt;em&gt;My Windows App&lt;/em&gt; application that has very interesting check before you can do anything with it (notice the longest menu item text ever!):&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App" border="0" alt="My Windows App" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_9.png" width="368" height="166" /&gt; &lt;/p&gt;  &lt;p&gt;And when you click the menu item you’ll get this kind of error and application exits:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App issue" border="0" alt="My Windows App issue" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_12.png" width="368" height="246" /&gt; &lt;/p&gt;  &lt;p&gt;Clearly this is not something want. You want to continue using your application despite it weird flaws etc. But since it’s not maintained anymore there’s nothing you can do with it... No support, no hot fixes... no help whatsoever... And for this particular case there isn’t readymade shim that could fix the application. So you’re left without your nice application... &lt;strong&gt;&lt;em&gt;Unless&lt;/em&gt;&lt;/strong&gt; you’re ready to do some engineering &lt;strong&gt;:-) &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Let’s do some digging for the application itself before rushing to the solution. First I’ll do &lt;em&gt;dumpbin /imports “My Windows App.exe”&lt;/em&gt; to see what the application actually imports. I can also use &lt;em&gt;dumpbin /disasm&lt;/em&gt; to take closer look of it but &lt;em&gt;&lt;a href="http://www.smidgeonsoft.com/" target="_blank"&gt;PEBrowse&lt;/a&gt;&lt;/em&gt; does both jobs better than &lt;em&gt;dumpbin&lt;/em&gt; so I’ll use that instead.     &lt;br /&gt;And since I suspect my applications “insane sanity check” to have something to do with date time functions I’ll identify &lt;em&gt;GetSystemTime&lt;/em&gt; as potential candidate from &lt;em&gt;Import Address Table &lt;/em&gt;(IAT). After some browsing at the &lt;em&gt;PEBrowse&lt;/em&gt; I’m able to find this kind of disassembly:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="PEBrowse and disassembled output" border="0" alt="PEBrowse and disassembled output" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_2da19b79-32a0-45df-8eae-e19573dcdfec.png" width="480" height="409" /&gt;     &lt;br /&gt;Here is same output in text format:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;CALL    DWORD PTR [&lt;u&gt;&lt;strong&gt;KERNEL32.DLL!GetSystemTime&lt;/strong&gt;&lt;/u&gt;]; (0x41A2AC) 
CMP     ESI,ESP      
CALL    0x40159F      
MOVZX    EAX,WORD PTR [EBP-0x84]
CMP     EAX,&lt;strong&gt;&lt;u&gt;0x7D0&lt;/u&gt;&lt;/strong&gt;    ; NOTE: 0x7D0 = 2000 
JE     0x404545      ; (*+0x34) 
MOV     ESI,ESP      
PUSH    0x10        
PUSH    &lt;font color="#a31515"&gt;'Error'&lt;/font&gt;      ; (0x416C2C) 
PUSH    &lt;font color="#a31515"&gt;'This application works in year 2000 only!'&lt;/font&gt;; (0x4177B8) 
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!MessageBoxW]; (0x41A3E8) 
CMP     ESI,ESP      
CALL    0x40159F      
MOV     ESI,ESP      
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!DestroyWindow]; (0x41A3A4) 
CMP     ESI,ESP      
CALL    0x40159F      
PUSH    0x0        
PUSH    &lt;font color="#a31515"&gt;'Do something'&lt;/font&gt;   ; (0x416F10) 
PUSH    &lt;font color="#a31515"&gt;'Whoo-hoo! This works correctly.'&lt;/font&gt;; (0x417778) 
MOV     EAX,DWORD PTR [EBP+0x8]
PUSH    EAX        
CALL    DWORD PTR [USER32.DLL!MessageBoxW]; (0x41A3E8)&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;Line 1 shows that &lt;em&gt;GetSystemTime &lt;/em&gt;gets called and it fills &lt;em&gt;SYSTEMTIME&lt;/em&gt; structure. Value from &lt;em&gt;SYSTEMTIME&lt;/em&gt; structure (wYear) is then compared to the value 2000 on line 5. And if that value isn’t 2000 then user is displayed error message (see line 8 to 13). After error message application is closed (line 19). &lt;/p&gt;

&lt;p&gt;I could do nasty fix (that would be easy in this example)... which would be editing the executable to bypass that check but I’m not going to do that. I don’t want to touch the executable itself. &lt;/p&gt;

&lt;p&gt;But now we know that in order to fix this exact issue we can just _&lt;em&gt;handle&lt;/em&gt;_ returned structure from &lt;em&gt;GetSystemTime&lt;/em&gt; function. We need to change it to return 2000 instead of current year.&lt;/p&gt;

&lt;p&gt;Now it’s time to bring in &lt;a href="http://research.microsoft.com/en-us/projects/detours/" target="_blank"&gt;Detours&lt;/a&gt; created by &lt;a href="http://research.microsoft.com" target="_blank"&gt;Microsoft Research&lt;/a&gt;. Detours allows you to do exactly what we want in this case. It allows you to _&lt;em&gt;detour&lt;/em&gt;_ function calls so that your component gets called when application tries to call that function. In order to use Detours you just need to download it and run &lt;em&gt;nmake&lt;/em&gt; on the installation directory to build the detours itself and all the samples too. Then we’re ready to proceed to next stage.&lt;/p&gt;

&lt;p&gt;So I’ll quickly create new Win32 project with application type DLL and name the project “&lt;em&gt;My Getsystemtime”&lt;/em&gt; (using &lt;em&gt;simple.cpp&lt;/em&gt; as an example from the &lt;em&gt;samples\simple&lt;/em&gt; –folder). Then I’ll add following code to it: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;#define&lt;/font&gt; WIN32_LEAN_AND_MEAN       &lt;font color="#008000"&gt;// Exclude rarely-used stuff from Windows headers
&lt;/font&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;lt;windows.h&amp;gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;#include&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;detours.h&amp;quot;

&lt;/font&gt;&lt;font color="#0000ff"&gt;static&lt;/font&gt; VOID (WINAPI * TrueGetSystemTime)(LPSYSTEMTIME lpSystemTime) = GetSystemTime;

VOID WINAPI MyGetSystemTime(LPSYSTEMTIME lpSystemTime)
{
  OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;'My GetSystemTime' called (faking year 2000 :-)!&amp;quot;&lt;/font&gt;));
  TrueGetSystemTime(lpSystemTime);
  lpSystemTime-&amp;gt;wYear = 2000;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call,LPVOID lpReserved)
{
  LONG error;
&lt;font color="#0000ff"&gt;  switch&lt;/font&gt; (ul_reason_for_call)
  {
&lt;font color="#0000ff"&gt;  case&lt;/font&gt; DLL_PROCESS_ATTACH:
    OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'...&amp;quot;&lt;/font&gt;));

    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&amp;amp;(PVOID&amp;amp;)TrueGetSystemTime, MyGetSystemTime);
    error = DetourTransactionCommit();

    &lt;font color="#0000ff"&gt;if&lt;/font&gt; (error == NO_ERROR) 
    {
      OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'... Done!&amp;quot;&lt;/font&gt;));
    }
    &lt;font color="#0000ff"&gt;else&lt;/font&gt; 
    {
      OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Attaching 'Fake GetSystemtime'... Failed!&amp;quot;&lt;/font&gt;));
    }
&lt;font color="#0000ff"&gt;    break&lt;/font&gt;;
&lt;font color="#0000ff"&gt;  case&lt;/font&gt; DLL_PROCESS_DETACH:
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&amp;amp;(PVOID&amp;amp;)TrueGetSystemTime, MyGetSystemTime);
    error = DetourTransactionCommit();

    OutputDebugString(TEXT(&lt;font color="#a31515"&gt;&amp;quot;Detaching 'Fake GetSystemtime'.&amp;quot;&lt;/font&gt;));
&lt;font color="#0000ff"&gt;    break&lt;/font&gt;;
  }
&lt;font color="#0000ff"&gt;  return&lt;/font&gt; TRUE;
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;And in order this code to be build correctly I’ll also need to add &lt;em&gt;include&lt;/em&gt; folder that points to the detours installation folder and include folder underneath it. I also need to add &lt;em&gt;detours&lt;/em&gt;.&lt;em&gt;lib&lt;/em&gt; and &lt;em&gt;detoured.lib&lt;/em&gt; so that linker is happy too.&lt;/p&gt;

&lt;p&gt;Now I have my newly built DLL that I use to modify behavior of &lt;em&gt;My Windows App&lt;/em&gt;. Detours installation package also contains command line tool &lt;em&gt;withdll.exe&lt;/em&gt; so that you can easily test you detour DLL. I’ll just launch my application using following command line and then all should be fine (assuming that everything is at the same folder):&lt;/p&gt;

&lt;p&gt;&lt;em&gt;withdll.exe /d:&amp;quot;My GetSystemtime.dll&amp;quot;&amp;#160; &amp;quot;My Windows App.exe&amp;quot;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And if I now click the interesting menu item I’ll see this (since my application changes the year to be 2000 on line 11 in above code sample):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Windows App Detoured!" border="0" alt="My Windows App Detoured!" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_fde29e22-dce4-4694-8d71-36dac5d0eabd.png" width="393" height="265" /&gt; &lt;/p&gt;

&lt;p&gt;And since I used &lt;em&gt;OutputDebugString&lt;/em&gt; for demonstration purposes I’m able to verify the behavior using &lt;em&gt;DebugView&lt;/em&gt;: 

  &lt;br /&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="DebugView displays our messages" border="0" alt="DebugView displays our messages" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/7a145c9287fc_D752/image_881189dc-2315-46a3-a0b0-3aee04c75af5.png" width="571" height="188" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;
  &lt;br /&gt;So we’re able to modify functionality of the application &lt;em&gt;without&lt;/em&gt; touching the executable itself. This example was a bit different than my previous application compatibility one. This is much more &lt;em&gt;engineering&lt;/em&gt; kind of solution than to use readymade tools for fixing applications. But both have it’s time and it’s place. I’ll probably continue posting more information to this category so stay tuned!&lt;/p&gt;

&lt;p&gt;Anyways... Happy hacking! 
  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9932200" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Application+Compatibility/default.aspx">Application Compatibility</category></item><item><title>Live Mesh + Visual SourceSafe = Code everywhere!</title><link>http://blogs.msdn.com/jannemattila/archive/2009/11/06/live-mesh-visual-sourcesafe-code-everywhere.aspx</link><pubDate>Fri, 06 Nov 2009 12:32:12 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9918543</guid><dc:creator>jannemattila</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9918543.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9918543</wfw:commentRss><description>&lt;p&gt;For long time I have wanted have my code _&lt;em&gt;everywhere&lt;/em&gt;_. Just because I have 3 different computers that I use to write my own stuff. Obviously it would be nice to have version control system (such as Team Foundation Server) but for my own use it would be quite heavy solution. And many times when I have had inspiration to start development of my old project I haven’t had network connectivity at that moment &lt;strong&gt;:-(&lt;/strong&gt; So I thought that I try untraditional solution: &lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Use Visual SourceSafe for source control &lt;/li&gt;    &lt;li&gt;Synchronize VSS database across all my computers with &lt;a href="http://www.mesh.com/" target="_blank"&gt;Live Mesh&lt;/a&gt; &lt;/li&gt;    &lt;li&gt;Use local VSS database and “get latest” from that in each computer&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I know that this sounds &lt;strong&gt;*weird*&lt;/strong&gt; but it turns out to be good and working solution! Now I can take my source from local VSS database whenever I need and it’s always up to date (since Live Mesh is constantly running on the background and it does all the synchronization stuff). &lt;/p&gt;  &lt;p&gt;Future will show if I manage to destroy my VSS database using this this approach &lt;strong&gt;:-)&lt;/strong&gt;     &lt;br /&gt;    &lt;br /&gt;Anyways... Happy hacking!     &lt;br /&gt;    &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9918543" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category></item><item><title>My Chess (another chess application)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/10/22/my-chess-another-chess-application.aspx</link><pubDate>Thu, 22 Oct 2009 14:43:04 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9911395</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9911395.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9911395</wfw:commentRss><description>&lt;p&gt;I came up with the idea of &lt;em&gt;My Chess&lt;/em&gt; when I wanted to play games with my friends and I noticed that I don’t have time to do that &lt;strong&gt;:-)&lt;/strong&gt; So I decided to write an application that allows me to play chess with my friends just by using small amount of time every now and then (approach is pretty different compared to the “&lt;em&gt;normal&lt;/em&gt;” chess). Small amount of time means something like 10 seconds per move so and not more... Of course if you WANT to use more time you’re allowed to do that.&lt;/p&gt;  &lt;p&gt;Here is list of other &lt;em&gt;features&lt;/em&gt; that I wanted from &lt;em&gt;My Chess&lt;/em&gt;:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Possibility to play offline. In another words I don’t want to think about the transport at all. I would just like to pass the transport related stuff to someone else. &lt;/li&gt;    &lt;li&gt;Mobile version of the application (HTC Touch Pro since I have one) &lt;/li&gt;    &lt;li&gt;“Social chess”: I wanted to have possibility to add comment when making move. Just because I want to tell to my opponents how good move I have made and remind them about their bad ones &lt;strong&gt;:-)&lt;/strong&gt; &lt;/li&gt;    &lt;li&gt;Game should be fast and easy. You should be able to make your move with just few clicks. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;I decided to go with C++ since I didn’t want to take start up delay of .NET application on my mobile device. This one was easy decision :-)&lt;/p&gt;  &lt;p&gt;Then I thought that I could use email as my transport because it automatically solves issues with the connectivity. On desktop I could rely on Outlook and mobile device I could rely on Pocket Outlook. More about technical details later in this post. &lt;/p&gt;  &lt;p&gt;But then I was forced to think proper way to solve how to transmit the game state. I mean that how could I pass the current state of the game to my opponent so that it would be easy for him/her to continue from that state.&amp;#160; Use of attachment at the email was one option. But the story for the end user on mobile device with attachments isn’t that good so I didn’t use effort on this option. I took another approach which was to define URL format that I could use to serialize the game state. Here is one example game state:    &lt;br /&gt;&lt;em&gt;my://chess/MczrFh7z?w=player1@contoso.com%26b=player2@contoso.com&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Game state uses &lt;em&gt;my&lt;/em&gt; protocol which I have mapped to application (&lt;a href="http://msdn.microsoft.com/en-us/library/aa767914%28VS.85%29.aspx" target="_blank"&gt;Registering an Application to a URL Protocol&lt;/a&gt; on MSDN). Of course &lt;em&gt;My Chess&lt;/em&gt; is configured according to the article so that it will called when &lt;em&gt;my &lt;/em&gt;protocol is launched. Here is example &lt;em&gt;.reg&lt;/em&gt; how custom application can be mapped to protocol:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\my]
@=&amp;quot;URL:My Chess Protocol&amp;quot;
&amp;quot;URL Protocol&amp;quot;=&amp;quot;&amp;quot;

[HKEY_CLASSES_ROOT\my\shell]

[HKEY_CLASSES_ROOT\my\shell\open]

[HKEY_CLASSES_ROOT\my\shell\open\command]
@=&amp;quot;\&amp;quot;C:\\path\\My Chess (x64).exe\&amp;quot; %1&amp;quot;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;So it really is that simple. Just define own key under HKEY_CLASSES_ROOT and set path to your application and it will be called automatically by the OS.&lt;/p&gt;

&lt;p&gt;If you look the same with &lt;em&gt;regedit&lt;/em&gt; it looks like this: &lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Registry" border="0" alt="Registry" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Registry_6.png" width="806" height="381" /&gt;&lt;/p&gt;

&lt;p&gt;If you now open up links starting with &lt;em&gt;my&lt;/em&gt; protocol they will eventually take you to the configured application. But you probably see this kind of security dialogs before your own application starts (at least for the first time): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="IE Confirmation dialog" border="0" alt="IE Confirmation dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/IE%20Confirmation_1.png" width="599" height="377" /&gt;&amp;#160; &lt;br /&gt;Just uncheck “&lt;em&gt;Always ask before opening this type of address&lt;/em&gt;” and press &lt;em&gt;Allow&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess Security Warning" border="0" alt="My Chess Security Warning" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_15.png" width="486" height="302" /&gt;&lt;/p&gt;

&lt;p&gt;Same goes with this dialog. Just check “&lt;em&gt;Do not show me the warning for this program again”&lt;/em&gt; and press &lt;em&gt;Allow&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt;&lt;em&gt;&lt;strong&gt; &lt;/strong&gt;Make sure you understand the consequences of allowing protocol to map to application. If you’re application doesn’t validate data correctly it may lead to security issues when someone passes malicious data to your application. Remember you have been warned!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;After that you should finally be at your application and you can use the information passed on the command line to execute the required actions.&lt;/p&gt;

&lt;p&gt;So basically now I have tools to build the application... But I haven’t yet solved the “&lt;em&gt;creating the email message&lt;/em&gt;” part of this solution. Remember I have both desktop application and mobile application to built.&lt;/p&gt;

&lt;p&gt;On desktop I’m using Outlook as my mail transport and it has pretty good instructions in here: 
  &lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/259298" target="_blank"&gt;How to use an Outlook Object Model from Visual C++ by using a #import statement&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://support.microsoft.com/kb/199870" target="_blank"&gt;How To Send a Message by Outlook Object Model with VC++&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Using Outlook on desktop with C++ is almost as easy as using it from .NET. It’s straight forward thing. So far so good right!&lt;/p&gt;

&lt;p&gt;On mobile device things aren’t quite that easy since you need to use MAPI for sending the mail message. Here are few examples for that: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/windowsmobile/archive/2007/03/21/getting-started-with-mapi.aspx" target="_blank"&gt;Getting Started with MAPI&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/windowsmobile/archive/2007/04/23/practical-use-of-mapi.aspx" target="_blank"&gt;Practical Use of MAPI&lt;/a&gt; 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/raffael/archive/2008/09/08/mapi-on-windows-mobile-6-programmatically-retrieve-mail-body-sample-code.aspx" target="_blank"&gt;MAPI on Windows Mobile 6: Programmatically retrieve mail BODY (sample code)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But as always things aren’t as easy as they first seem to be. Since even if I did register &lt;em&gt;my&lt;/em&gt; protocol at the mobile device it didn’t work as I expected. The problem was that Pocket Outlook always launched the URL to the default browser even if I had registration done to my own protocol. After some digging I found out that there are many security features that prevent that from happening. So in order to do it the &lt;em&gt;right way&lt;/em&gt; I should have to edit/configure some files to allow protocol to be launched correctly. But since this is more &lt;em&gt;concept&lt;/em&gt; than &lt;em&gt;production kind-of-application&lt;/em&gt; I took another route... I chose to &lt;strong&gt;*hijack*&lt;/strong&gt; MMS protocol for my custom application :-) I did that because it’s one of those protocols that are configured properly to the system. It normally launches Windows Media Player but I changed that to launch &lt;em&gt;My Chess&lt;/em&gt; instead. This is the reason why you see two links at the emails underneath. The URL structure is exactly the same... Only the protocol part is either &lt;em&gt;my&lt;/em&gt; or &lt;em&gt;mms&lt;/em&gt; depending on the link. I admit that this one was dirty hack...&lt;/p&gt;

&lt;p&gt;Now we have solved pretty much everything. But what about graphics then? Well I used images from Wikipedia at the &lt;a href="http://en.wikipedia.org/wiki/Chess_piece" target="_blank"&gt;Chess piece&lt;/a&gt; article (example &lt;a href="http://en.wikipedia.org/wiki/File:Chess_kdt45.svg" target="_blank"&gt;King&lt;/a&gt; where you can see the licenses as well). Graphics is another interesting difference between desktop application and mobile application since GDI+ headers and libraries aren’t available for the mobile environment :-( But I wanted to use GDI+ on my desktop application even if I was forced to use GDI at the mobile application. Yeah I know... someone else would have built them using the same codebase but not me.&lt;/p&gt;

&lt;p&gt;I used following functionalities from GDI+: Bitmap, Graphics (DrawImage, DrawCachedBitmap, DrawRectangle, FillRectangle), Graphics::FromImage, FontFamily, Font, SolidBrush, Pen, CachedBitmap etc.&lt;/p&gt;

&lt;p&gt;I used following functionalities from GDI: DeleteDC, DeleteObject, CreateCompatibleDC, CreateCompatibleBitmap, SelectObject, Rectangle, TransparentBlt, BitBlt etc.&lt;/p&gt;

&lt;p&gt;Are you already eager to see the final outcome? Well here it goes:&lt;/p&gt;

&lt;p&gt;Desktop version:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess" border="0" alt="My Chess" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/My%20Chess_2.png" width="609" height="610" /&gt; &lt;/p&gt;

&lt;p&gt;Mobile version (screenshot from emulator. It looks better on real device):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess (Mobile)" border="0" alt="My Chess (Mobile)" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/My%20Chess%20Mobile_3.png" width="373" height="570" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So how does it work in real life? Here is small example:&lt;/p&gt;

&lt;p&gt;First if start &lt;em&gt;My Chess&lt;/em&gt; without parameters so it creates automatically new game (in this example I play against myself): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess new game dialog" border="0" alt="My Chess new game dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email0_1.png" width="502" height="204" /&gt;&lt;/p&gt;

&lt;p&gt;In order to update the registry correctly for the protocol &lt;em&gt;My Chess&lt;/em&gt; needs to be run for the first time in the “&lt;em&gt;Run as administrator”&lt;/em&gt; –mode:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Run as administrator in order to register the protocol" border="0" alt="Run as administrator in order to register the protocol" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_6.png" width="365" height="79" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;So now you have new game and you can use mouse to make the move (see simple coloring to indicate the move): 
  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Coloring of the move" border="0" alt="Coloring of the move" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/image_9.png" width="145" height="318" /&gt; &lt;/p&gt;

&lt;p&gt;When you have done that you can press &lt;em&gt;Send&lt;/em&gt; menu which then creates email message for you where you can add your own comment to your move (everything else is generated.. you just need to add last line): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="My Chess created email message to Outlook" border="0" alt="My Chess created email message to Outlook" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email_1.png" width="448" height="398" /&gt; 

  &lt;br /&gt;Now the game state is passed to the opponent as just normal email. When opponent receives it and presses the link following security prompt is displayed (Note: on mobile device application is launched directly without any dialogs): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Outlook Security Notice on my protocol link" border="0" alt="Outlook Security Notice on my protocol link" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email2_1.png" width="570" height="499" /&gt; 

  &lt;br /&gt;After that &lt;em&gt;My Chess&lt;/em&gt; is opened and it’s opens the current game state directly: 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: block; float: none; border-top-width: 0px; border-bottom-width: 0px; margin-left: auto; border-left-width: 0px; margin-right: auto" title="Email3" border="0" alt="Email3" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyChess_87EA/Email3_1.png" width="630" height="628" /&gt;&amp;#160;&amp;#160; &lt;br /&gt;And now you’re ready to make your move and then send it to the opponent with some nasty comment of course. &lt;/p&gt;

&lt;p&gt;Current status of the &lt;em&gt;My Chess&lt;/em&gt; is pretty much &lt;em&gt;experimental&lt;/em&gt; / &lt;em&gt;concept state&lt;/em&gt; since I haven’t yet implemented all the necessary functions to make it &lt;em&gt;_real_&lt;/em&gt; chess application:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Move validation is currently missing and all special moves haven’t been implemented (promotion is only special move that has been implemented) &lt;/li&gt;

  &lt;li&gt;Currently there isn’t way to see previous move or move history (it’s stored behind the covers but it’s not visible). I’m going to implement animations so that when user opens &lt;em&gt;My Chess&lt;/em&gt; something like last 3 moves of the game would be animated. And user could browse the move history (with animations of course!) to see what has happened previously on the game. &lt;/li&gt;

  &lt;li&gt;I &lt;em&gt;could&lt;/em&gt; CC the email message to email box that could be read by machine. It then could insert the game state to database, create some statistics about the games, history of games etc. This brings more possibilities to extend the game in the future. But I probably leave this to someone else...&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the way... you might have noticed that I used &lt;em&gt;chess&lt;/em&gt; as in my URL to define which game should be launched from the &lt;em&gt;my&lt;/em&gt; protocol. This indicates that something else might come in the future. That remains to be seen! &lt;/p&gt;

&lt;p&gt;How could you use information provided in this blog entry for your own needs? Well here’s one: I have many times heard that customers need to “Send shortcuts” from their existing applications to each other. This could be way to do that. Just add functionality to “copy shortcut” from applications current state and then allow user to send it. Then at the receiving end you could create “proxy application” that interprets received information and opens the application into same state that the other user had. It could be pretty good improvement in many cases. 
  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9911395" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Fun/default.aspx">Fun</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Mobile/default.aspx">Mobile</category></item><item><title>My Notes (a small application between notepad and OneNote)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/04/30/my-notes-a-small-application-between-notepad-and-onenote.aspx</link><pubDate>Thu, 30 Apr 2009 10:20:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9578916</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9578916.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9578916</wfw:commentRss><description>&lt;p&gt;I have been using Windows 7 and&amp;#160; Windows Server 2008 R2 since the first beta. And I mean on my “&lt;em&gt;production&lt;/em&gt;” laptops. And I have to say that I haven’t had any major issues with them. In fact I’ve used to them so much that I don’t want to use older versions anymore &lt;strong&gt;:-) &lt;/strong&gt;And there are few improvements in them that I specially enjoy. Of course the biggest one is the new taskbar... But surprisingly I have also found new application that I have “always needed”: &lt;strong&gt;Sticky Notes&lt;/strong&gt;!&lt;/p&gt;  &lt;p&gt;Sticky Notes is small application where you can write small notes to yourself. I call it “&lt;em&gt;something between notepad and OneNote&lt;/em&gt;”. Reason for that is the fact that notepad is lightweight but it doesn’t contain &lt;u&gt;any&lt;/u&gt; features. OneNote is full-blown application with &lt;u&gt;tons&lt;/u&gt; of features. Sticky Notes lives between those too... It contains just &lt;u&gt;enough&lt;/u&gt; features that make it useful and it also looks nice! The only issue I’m having with the Sticky Notes is the fact that it comes with Windows 7 only &lt;strong&gt;:-(&lt;/strong&gt; So in my main “&lt;em&gt;production&lt;/em&gt;” laptop (which is R2) I don’t have sticky notes.&lt;/p&gt;  &lt;p&gt;For me it was a bit disappointing but I still understood the logic for that. Of course Sticky Notes isn’t “something that you would expect on the server” but since I really need this kind of application... I was forced to create one &lt;strong&gt;:-) &lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Normally when I create application I’ll just create new .NET project and start writing the application. But now I don’t want to suffer this penalty (screenshot taken from “&lt;em&gt;File –&amp;gt; New Project: Windows Forms Application –&amp;gt; Run&lt;/em&gt;”):&lt;/p&gt;  &lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title=".NET Windows Forms application" border="0" alt=".NET Windows Forms application" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_12.png" width="463" height="218" /&gt;&amp;#160; &lt;br /&gt;Downside of .NET applications is the memory usage. Normally I don’t care about it but since now I’m going to create application that will be “always on” I just have to care about it. I don’t want to waste too much memory (I only have 8 GB of it :-). Therefore I decided to go with the native way (Win32, C/C++ or whatever you like to call it). It gives me possibility to be in control of all the used resources.&lt;/p&gt;  &lt;p&gt;And here is the result “&lt;em&gt;My Notes&lt;/em&gt;” application:&lt;/p&gt;  &lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="My Notes - Example" border="0" alt="My Notes - Example" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_3.png" width="546" height="272" /&gt; &lt;/p&gt;  &lt;p&gt;I’ve hidden the menus and tried to make the borders as small as possible. I don’t want to waste space as much the normal Windows application does:    &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Standard windows application" border="0" alt="Standard windows application" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_9.png" width="380" height="233" /&gt;&amp;#160; &lt;br /&gt;“Standard” windows application doesn’t look that good either. So therefore I wanted to give it a bit more “fresh and appealing looks”. I used GDI+ and &lt;em&gt;LinearGradientBrush&lt;/em&gt; to make it fancier. In case your wondering how to add GDI+ to your native application then it’s just this simple:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;

GdiplusStartup(&amp;amp;gdiplusToken, &amp;amp;gdiplusStartupInput, NULL);
&lt;font color="#008000"&gt;// ...
&lt;/font&gt;GdiplusShutdown(gdiplusToken);&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;See more about GDI+ on MSDN if your &lt;a href="http://msdn.microsoft.com/en-us/library/ms534077(VS.85).aspx" target="_blank"&gt;interested&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Even if I wanted the application to small borders I didn’t want to completely remove the menus. I used same thing as in IE for example... pressing &lt;em&gt;Alt&lt;/em&gt; brings in the menu:&lt;/p&gt;

&lt;p&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="My Notes - Menu visible" border="0" alt="My Notes - Menu visible" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_6.png" width="217" height="70" /&gt; &lt;/p&gt;

&lt;p&gt;I also wanted to give myself possibility to extend the application little bit more and I also added context menu to the edit surface. I don’t know yet what to put there but it’s nice to have if I need it someday &lt;strong&gt;:-)
    &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="My Notes - Content menu" border="0" alt="My Notes - Content menu" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_27.png" width="322" height="122" /&gt; &lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I had to add “graphical menu” for basic operations like New and Close (just for fun):&lt;/p&gt;

&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="My Notes - Graphical menu" border="0" alt="My Notes - Graphical menu" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_15.png" width="545" height="40" /&gt; &lt;/p&gt;

&lt;p&gt;Graphical menu is only visible if mouse is on top of the so called caption bar (as in screenshot).&lt;/p&gt;

&lt;p&gt;And of course this should be in x64 since I’m running my R2 on x64:
  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="VS Configuration Manager dialog" border="0" alt="VS Configuration Manager dialog" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_18.png" width="717" height="451" /&gt; &lt;/p&gt;

&lt;p&gt;If your wondering why you don’t have the option to use &lt;em&gt;x64&lt;/em&gt; then you haven’t most likely checked the option in the Visual Studio setup. Just go to &lt;em&gt;Control Panel&lt;/em&gt; and locate VS and select option to change the setup:

  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="Control panel - Visual Studio" border="0" alt="Control panel - Visual Studio" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_21.png" width="427" height="53" /&gt;&amp;#160; &lt;br /&gt;Then just check the &lt;em&gt;x64&lt;/em&gt; if that’s what you want:

  &lt;br /&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="VS setup" border="0" alt="VS setup" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/MyNotes_1444F/image_24.png" width="332" height="320" /&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;And last but not least... I’ve put my &lt;em&gt;My Notes&lt;/em&gt; to the &lt;a href="https://www.mesh.com/welcome/default.aspx" target="_blank"&gt;Live Mesh&lt;/a&gt; folder and synchronized my content to other devices. It’s simple and since I’m saving my note content to Rich Text Format (RTF) I can open up my notes even in my Windows Mobile. So now I have pretty good solution for my notes.

  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking!

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9578916" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Fun/default.aspx">Fun</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/C_2F00_C_2B002B00_/default.aspx">C/C++</category></item><item><title>Visual Studio 2008: Track Active Item in Solution Explorer</title><link>http://blogs.msdn.com/jannemattila/archive/2009/02/21/visual-studio-2008-track-active-item-in-solution-explorer.aspx</link><pubDate>Sat, 21 Feb 2009 21:58:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9437915</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9437915.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9437915</wfw:commentRss><description>&lt;p&gt;If you’re working on solution that has many projects and many project items and &lt;u&gt;you tend to get lost between your files&lt;/u&gt;... You might want to go to &lt;em&gt;Tools –&amp;gt; Options –&amp;gt; Projects and Solutions&lt;/em&gt; and set &lt;em&gt;Track Active Item in Solution Explorer&lt;/em&gt; on. For me it was a big relief that I found it. I’m working on project that has quite many projects under the solution and I have found myself constantly “searching” for files that are at the same location as the currently open file. And this is the painkiller for that pain:&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Visual Studio 2008 - Track Active Item in Solution Explorer" border="0" alt="Visual Studio 2008 - Track Active Item in Solution Explorer" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/VisualStudio2008TrackActiveIteminSolutio_123FB/image_6.png" width="510" height="251" /&gt;     &lt;br /&gt;    &lt;br /&gt;I’m not sure but if I have to guess... this was on by default on previous version of VS but not anymore on 2008. Because I know that I have enjoyed this feature in the past &lt;strong&gt;:-)&lt;/strong&gt; But I might be wrong and I have manually set that on in the past too.     &lt;br /&gt;    &lt;br /&gt;Anyways... Happy hacking!     &lt;br /&gt;    &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9437915" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Creating Excel Game (or something similar for fun)</title><link>http://blogs.msdn.com/jannemattila/archive/2009/01/29/creating-excel-game-or-something-similar-for-fun.aspx</link><pubDate>Thu, 29 Jan 2009 22:47:33 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9383568</guid><dc:creator>jannemattila</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9383568.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9383568</wfw:commentRss><description>&lt;p&gt;Awhile back I saw really interesting article: &lt;a href=" http://www.gamasutra.com/view/feature/3563/microsoft_excel_revolutionary_3d_.php" target="_blank"&gt;Microsoft Excel: Revolutionary 3D Game Engine?&lt;/a&gt;     &lt;br /&gt;After that I was forced to do small test on that and that of course resulted to this post &lt;strong&gt;:-)&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;I just grabbed the idea and made small “car game” (but in reality it just vaguely reminds of car game) on top of Excel. And here is the result: &lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingExcelGame_1378A/image_11.png" target="_blank"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Car Game example" border="0" alt="Car Game example" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingExcelGame_1378A/image_thumb_3.png" width="639" height="480" /&gt;&lt;/a&gt;     &lt;br /&gt;My car (or just blue box if you wish) can be controlled with mouse. If you press left mouse button it increases the speed of the car (a.k.a. gas pedal). I was too lazy to “invent” brake so car just slowly slows down if user isn’t pressing the left mouse button. User can control the “car” by moving mouse from left to right. Right mouse button quits the game.&lt;/p&gt;  &lt;h4&gt;Click here for the &lt;a href="http://cid-64286c13c6bd5d77.skydrive.live.com/self.aspx/Julkinen/Blog%20files/ExcelCarGame.wmv" target="_blank"&gt;video&lt;/a&gt;.&lt;/h4&gt; Since I just wanted to test basic input and drawing mechanisms I didn’t even bother to think of creating any physics. I’ll leave that to you! But here’s the source code for my example:   &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;&lt;font color="#008000"&gt;' WinAPI stuff:
&lt;/font&gt;&lt;font color="#0000ff"&gt;Declare&lt;/font&gt; &lt;font color="#0000ff"&gt;Sub&lt;/font&gt; Sleep &lt;font color="#0000ff"&gt;Lib&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;kernel32&amp;quot;&lt;/font&gt; (&lt;font color="#0000ff"&gt;ByVal&lt;/font&gt; dwMilliseconds &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long&lt;/font&gt;)
&lt;font color="#0000ff"&gt;Declare&lt;/font&gt; &lt;font color="#0000ff"&gt;Function&lt;/font&gt; GetAsyncKeyState &lt;font color="#0000ff"&gt;Lib&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;user32&amp;quot;&lt;/font&gt; (&lt;font color="#0000ff"&gt;ByVal&lt;/font&gt; vKey &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long&lt;/font&gt;) &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Integer
Declare&lt;/font&gt; &lt;font color="#0000ff"&gt;Sub&lt;/font&gt; GetCursorPos &lt;font color="#0000ff"&gt;Lib&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;user32&amp;quot;&lt;/font&gt; (&lt;font color="#0000ff"&gt;ByRef&lt;/font&gt; lpoint &lt;font color="#0000ff"&gt;As&lt;/font&gt; POINTAPI)
&lt;font color="#0000ff"&gt;Declare&lt;/font&gt; &lt;font color="#0000ff"&gt;Function&lt;/font&gt; ShowCursor &lt;font color="#0000ff"&gt;Lib&lt;/font&gt; &lt;font color="#a31515"&gt;&amp;quot;user32&amp;quot;&lt;/font&gt; (&lt;font color="#0000ff"&gt;ByVal&lt;/font&gt; bShow &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long&lt;/font&gt;) &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long

Type&lt;/font&gt; POINTAPI
  x &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long
&lt;/font&gt;  y &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Long
End&lt;/font&gt; &lt;font color="#0000ff"&gt;Type&lt;/font&gt; 

&lt;font color="#0000ff"&gt;Sub&lt;/font&gt; GameLoop()

  &lt;font color="#008000"&gt;' Input related variables:
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; mouseLocation &lt;font color="#0000ff"&gt;As&lt;/font&gt; POINTAPI
  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; mouseLocationPrevious &lt;font color="#0000ff"&gt;As&lt;/font&gt; POINTAPI

  &lt;font color="#008000"&gt;' Get &amp;amp; Set original mouse position:
&lt;/font&gt;  GetCursorPos mouseLocation
  mouseLocationPrevious = mouseLocation

  &lt;font color="#008000"&gt;' Initialize the system:
&lt;/font&gt;  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Xdiff&amp;quot;&lt;/font&gt;).Value = 0
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Ydiff&amp;quot;&lt;/font&gt;).Value = 0
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SteeringWheel&amp;quot;&lt;/font&gt;).Value = 90
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value = 0
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarX&amp;quot;&lt;/font&gt;).Value = 250
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarY&amp;quot;&lt;/font&gt;).Value = 250

  &lt;font color="#008000"&gt;' Hide mouse cursor:
&lt;/font&gt;  ShowCursor 0

  &lt;font color="#008000"&gt;' We're going to loop until mouse click:
&lt;/font&gt;  &lt;font color="#0000ff"&gt;While&lt;/font&gt; GetAsyncKeyState(vbKeyRButton) = 0

    &lt;font color="#008000"&gt;' Magic:
&lt;/font&gt;    Sheets(&lt;font color="#a31515"&gt;&amp;quot;GameBoard&amp;quot;&lt;/font&gt;).Range(&lt;font color="#a31515"&gt;&amp;quot;Z_SortArea&amp;quot;&lt;/font&gt;).Sort _
      Sheets(&lt;font color="#a31515"&gt;&amp;quot;GameBoard&amp;quot;&lt;/font&gt;).Range(&lt;font color="#a31515"&gt;&amp;quot;NormalZ&amp;quot;&lt;/font&gt;), xlAscending

    GetCursorPos mouseLocation
    DrawCar &lt;font color="#a31515"&gt;&amp;quot;car1&amp;quot;&lt;/font&gt;, mouseLocation, mouseLocationPrevious

    ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarX&amp;quot;&lt;/font&gt;).Value = _
      ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarX&amp;quot;&lt;/font&gt;).Value - ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SpeedY&amp;quot;&lt;/font&gt;).Value
    ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarY&amp;quot;&lt;/font&gt;).Value = _
      ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarY&amp;quot;&lt;/font&gt;).Value - ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SpeedX&amp;quot;&lt;/font&gt;).Value
    mouseLocationPrevious = mouseLocation

    &lt;font color="#008000"&gt;' Let’s take a nap:
&lt;/font&gt;    Sleep(10)

    &lt;font color="#0000ff"&gt;If&lt;/font&gt; GetAsyncKeyState(vbKeyLButton) &amp;lt;&amp;gt; 0 &lt;font color="#0000ff"&gt;Then
&lt;/font&gt;      &lt;font color="#008000"&gt;' User is pressing the gas pedal =&amp;gt; More speed:
&lt;/font&gt;      ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value = _
        ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value + ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Accelerate&amp;quot;&lt;/font&gt;).Value
      &lt;font color="#0000ff"&gt;If&lt;/font&gt; ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value &amp;gt; ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;MaxPedal&amp;quot;&lt;/font&gt;).Value &lt;font color="#0000ff"&gt;Then
&lt;/font&gt;        ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;MaxPedal&amp;quot;&lt;/font&gt;).Value
      &lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;If
&lt;/font&gt;    &lt;font color="#0000ff"&gt;Else
&lt;/font&gt;      &lt;font color="#008000"&gt;' User isn't pressing the gas pedal =&amp;gt; Take of some speed (if moving):
&lt;/font&gt;      ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value = _
        ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value - ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Brake&amp;quot;&lt;/font&gt;).Value
      &lt;font color="#0000ff"&gt;If&lt;/font&gt; ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value &amp;lt; 0 &lt;font color="#0000ff"&gt;Then
&lt;/font&gt;        ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Pedal&amp;quot;&lt;/font&gt;).Value = 0
      &lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;If
&lt;/font&gt;    &lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;If
&lt;/font&gt;  &lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;While

&lt;/font&gt;  &lt;font color="#008000"&gt;' Bring back the mouse cursor:
&lt;/font&gt;  ShowCursor 1
&lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;Sub

Sub&lt;/font&gt; DrawCar(carID &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;String&lt;/font&gt;, _
  mouseLocation &lt;font color="#0000ff"&gt;As&lt;/font&gt; POINTAPI, _
  mouseLocationPrevious &lt;font color="#0000ff"&gt;As&lt;/font&gt; POINTAPI)

  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; diffX &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; diffY &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; Points(1 &lt;font color="#0000ff"&gt;To&lt;/font&gt; 5, 1 &lt;font color="#0000ff"&gt;To&lt;/font&gt; 2) &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Single

&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; carX &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; carY &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; carWidth &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double
&lt;/font&gt;  &lt;font color="#0000ff"&gt;Dim&lt;/font&gt; carHeight &lt;font color="#0000ff"&gt;As&lt;/font&gt; &lt;font color="#0000ff"&gt;Double

&lt;/font&gt;  carX = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarX&amp;quot;&lt;/font&gt;).Value
  carY = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarY&amp;quot;&lt;/font&gt;).Value
  carHeight = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarSize1&amp;quot;&lt;/font&gt;).Value
  carWidth = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;CarSize2&amp;quot;&lt;/font&gt;).Value

  &lt;font color="#008000"&gt;' Get input and store current values to sheet:
&lt;/font&gt;  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Xdiff&amp;quot;&lt;/font&gt;).Value = mouseLocationPrevious.x - mouseLocation.x
  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Ydiff&amp;quot;&lt;/font&gt;).Value = mouseLocationPrevious.y - mouseLocation.y

  ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SteeringWheel&amp;quot;&lt;/font&gt;).Value = _
    ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SteeringWheel&amp;quot;&lt;/font&gt;).Value + ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;Xdiff2&amp;quot;&lt;/font&gt;).Value

  Points(1, 1) = carX - carWidth
  Points(1, 2) = carY + carHeight
  Points(2, 1) = carX + carWidth
  Points(2, 2) = carY + carHeight
  Points(3, 1) = carX + carWidth
  Points(3, 2) = carY - carHeight
  Points(4, 1) = carX - carWidth
  Points(4, 2) = carY - carHeight
  Points(5, 1) = Points(1, 1)
  Points(5, 2) = Points(1, 2)

  &lt;font color="#008000"&gt;' Draw this car:
&lt;/font&gt;  &lt;font color="#0000ff"&gt;On&lt;/font&gt; &lt;font color="#0000ff"&gt;Error&lt;/font&gt; &lt;font color="#0000ff"&gt;Resume&lt;/font&gt; &lt;font color="#0000ff"&gt;Next
&lt;/font&gt;  ActiveSheet.Shapes(carID).Delete
  Err.Clear &lt;font color="#008000"&gt;' Deleting Shape that doesn't exist =&amp;gt; Error
&lt;/font&gt;  ActiveSheet.Shapes.AddPolyline(Points).Name = carID
  ActiveSheet.Shapes(carID).Rotation = ActiveSheet.Range(&lt;font color="#a31515"&gt;&amp;quot;SteeringWheel&amp;quot;&lt;/font&gt;).Value
&lt;font color="#0000ff"&gt;End&lt;/font&gt; &lt;font color="#0000ff"&gt;Sub&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;I did my tests on Excel 2007 and I used &lt;em&gt;.xlsm&lt;/em&gt; format (Macro enabled). You can grab my file from &lt;a href="http://cid-64286c13c6bd5d77.skydrive.live.com/self.aspx/Julkinen/Blog%20files/Car%20Game.xlsm" target="_blank"&gt;here&lt;/a&gt;. 

  &lt;br /&gt;

  &lt;br /&gt;So if you want to open that example it gives you this security warning: 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Security warning" border="0" alt="Security warning" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingExcelGame_1378A/image_6.png" width="390" height="80" /&gt; 

  &lt;br /&gt;

  &lt;br /&gt;You need to click &lt;em&gt;Options...&lt;/em&gt; button just below ribbon. And then check &lt;em&gt;Enable this content&lt;/em&gt; and click &lt;em&gt;OK&lt;/em&gt;: 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Security Alert - Macro" border="0" alt="Security Alert - Macro" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingExcelGame_1378A/image_5.png" width="485" height="429" /&gt; 

  &lt;br /&gt;

  &lt;br /&gt;And if you want to run the game you can just press &lt;em&gt;Alt-F8&lt;/em&gt; and then hit enter (or click &lt;em&gt;Run&lt;/em&gt;): 

  &lt;br /&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Macro" border="0" alt="Macro" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingExcelGame_1378A/image_9.png" width="375" height="358" /&gt;&lt;/p&gt;

&lt;p&gt;Well I have to say that I enjoyed playing with Excel. Maybe some day I’m going to use this for some serious thing or not &lt;strong&gt;:-)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Anyways... Happy hacking! 
  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9383568" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Fun/default.aspx">Fun</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Excel/default.aspx">Excel</category></item><item><title>Web Services and namespaces (or WCF?)</title><link>http://blogs.msdn.com/jannemattila/archive/2008/10/15/web-services-and-namespaces-or-wcf.aspx</link><pubDate>Wed, 15 Oct 2008 10:27:28 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9000423</guid><dc:creator>jannemattila</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/9000423.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=9000423</wfw:commentRss><description>&lt;p&gt;You might have encountered following situation:    &lt;br /&gt;1. You have created class library “MyLibrary” and it contains following class:     &lt;br /&gt;&lt;/p&gt;  &lt;p&gt;   &lt;table cellspacing="10"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" align="right"&gt;           &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyLibrary
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;Employee
&lt;/font&gt;  {
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;string&lt;/font&gt; FirstName;
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;string&lt;/font&gt; LastName;
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;2. You have created Web Service “MyWeb” using following VS template: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Web%20Service%20project_2.png"&gt;&lt;img title="Web Service project" border="0" alt="Web Service project" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Web%20Service%20project_thumb.png" width="128" height="20" /&gt;&lt;/a&gt; 

  &lt;br /&gt;&amp;#160; - It references “MyLibrary” 

  &lt;br /&gt;&amp;#160; - It contains following method: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt; : System.Web.Services.&lt;font color="#2b91af"&gt;WebService
&lt;/font&gt;{
  [&lt;font color="#2b91af"&gt;WebMethod&lt;/font&gt;]
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee)
  {
    &lt;font color="#008000"&gt;// TODO: implement
&lt;/font&gt;  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;3. Finally you create Windows Forms application “My Win App”: 
  &lt;br /&gt;&amp;#160; - You “Add Web Reference” to “MyWeb” and you name it “WebServices” 

  &lt;br /&gt;&amp;#160; - You write following code to use that web service: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
employee.FirstName = &lt;font color="#a31515"&gt;&amp;quot;John&amp;quot;&lt;/font&gt;;
employee.LastName = &lt;font color="#a31515"&gt;&amp;quot;Doe&amp;quot;&lt;/font&gt;;
WebServices.&lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt; myWeb = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;MyWeb&lt;/font&gt;();
myWeb.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;4. You run your application and all is fine. 
  &lt;br /&gt;5. Later you notice that you need to add reference to your “MyLibrary” into your “My Win App” 

  &lt;br /&gt;(There could be many reasons for this. For example you want to use same business logic that web service uses etc.) 

  &lt;br /&gt;6. You add following code to you “My Win App”: &lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee2 = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt; employeeManager = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt;();
employeeManager.AddNewEmployee(employee2);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;7. Code works fine but then you notice that you actually have the same class from two different namespaces. What if you try to mix and match them (and sometimes you just have to do that)? Let’s see what happens:&lt;/p&gt;

&lt;p&gt;
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; WebServices.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt; employeeManager = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyLibrary.&lt;font color="#2b91af"&gt;EmployeeManager&lt;/font&gt;();
employeeManager.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;8. When you try to compile your application and you’ll receive following error: 
  &lt;br /&gt;&lt;em&gt;&lt;img title="VS compile error" border="0" alt="VS compile error" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/VS%20compile%20error_3.png" width="703" height="62" /&gt;&amp;#160; Error&amp;#160;&amp;#160;&amp;#160; 11&amp;#160;&amp;#160;&amp;#160; The best overloaded method match for 'MyLibrary.EmployeeManager.AddNewEmployee(MyLibrary.Employee)' has some invalid arguments&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\MainForm.cs&amp;#160;&amp;#160;&amp;#160; 41&amp;#160;&amp;#160;&amp;#160; 1&amp;#160;&amp;#160;&amp;#160; MyWinApp 

    &lt;br /&gt;Error&amp;#160;&amp;#160;&amp;#160; 12&amp;#160;&amp;#160;&amp;#160; Argument '1': cannot convert from 'MyWinApp.WebServices.Employee' to 'MyLibrary.Employee'&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\MainForm.cs&amp;#160;&amp;#160;&amp;#160; 41&amp;#160;&amp;#160;&amp;#160; 32&amp;#160;&amp;#160;&amp;#160; MyWinApp 

    &lt;br /&gt;&lt;/em&gt;

  &lt;br /&gt;9. You open up the generated proxy code: 

  &lt;br /&gt;&lt;img title="Reference" border="0" alt="Reference" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/Reference_3.png" width="172" height="97" /&gt; 

  &lt;br /&gt;

  &lt;br /&gt;10. You locate the code where &lt;em&gt;Employee&lt;/em&gt; is defined and comment that part. And then you compile again with following results: 

  &lt;br /&gt;&lt;em&gt;Error&amp;#160;&amp;#160;&amp;#160; 11&amp;#160;&amp;#160;&amp;#160; The type or namespace name 'Employee' could not be found (are you missing a using directive or an assembly reference?)&amp;#160;&amp;#160;&amp;#160; C:\&amp;lt;path&amp;gt;\MyWinApp\Web References\WebServices\Reference.cs&amp;#160;&amp;#160;&amp;#160; 82&amp;#160;&amp;#160;&amp;#160; 36&amp;#160;&amp;#160;&amp;#160; MyWinApp 
    &lt;br /&gt;&lt;/em&gt;

  &lt;br /&gt;11. You fix that by resolving the missing type: 

  &lt;br /&gt;&lt;img title="VS Resolve" border="0" alt="VS Resolve" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/VS%20Resolve_3.png" width="518" height="66" /&gt; &lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;12. You compile and you’re happy right (obviously you need to modify also all &lt;em&gt;WebServices.Employee&lt;/em&gt; types to &lt;em&gt;MyLibrary.Employee&lt;/em&gt; types)?&lt;/p&gt;

&lt;p&gt;Well you might be happy since now your code works... &lt;strong&gt;BUT&lt;/strong&gt; you have manually edited generated file which will be re-generated every time you do “Update Web Reference” from Visual Studio and you’ll lose you modifications. And that’s not nice.&lt;/p&gt;

&lt;p&gt;If this would be question for me I would give you following answer (you might find different opinions on this one): Go to the WCF route instead &lt;strong&gt;:-) &lt;/strong&gt;If Windows Communication Foundation (WCF) is something new to you I think you should check these out and find more information on the web: &lt;a target="_blank" href="http://en.wikipedia.org/wiki/Windows_Communication_Foundation"&gt;Overview of WCF from Wikipedia&lt;/a&gt; and &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/netframework/aa663324.aspx"&gt;Windows Communication Foundation home on MSDN&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I’m going to run through this same example with WCF way and then we can (hopefully) see why it fits like good glove.&lt;/p&gt;

&lt;p&gt;1. Create new WCF project called “MyWcf” using following VS template: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%201_2.png"&gt;&lt;img title="WCF 1" border="0" alt="WCF 1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%201_thumb.png" width="138" height="18" /&gt;&lt;/a&gt; 

  &lt;br /&gt;2. Add reference to “MyLibrary” 

  &lt;br /&gt;3. Delete &lt;em&gt;IService1.cs&lt;/em&gt; and &lt;em&gt;Service1.svc &lt;/em&gt;from your newly created project. 

  &lt;br /&gt;4. Add new item “MyWcfService.svc” (using &lt;em&gt;WCF Service &lt;/em&gt;template) 

  &lt;br /&gt;5. Modify “IMyWcfService.cs” file: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.ServiceModel;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; MyLibrary;

&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyWcf
{
  [&lt;font color="#2b91af"&gt;ServiceContract&lt;/font&gt;]
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;interface&lt;/font&gt; &lt;font color="#2b91af"&gt;IMyWcfService
&lt;/font&gt;  {
    [&lt;font color="#2b91af"&gt;OperationContract&lt;/font&gt;]
    &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee);
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
6. Modify “MyWcfService.cs” file: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; MyLibrary;

&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; MyWcf
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;MyWcfService&lt;/font&gt; : &lt;font color="#2b91af"&gt;IMyWcfService
&lt;/font&gt;  {
    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; AddNewEmployee(&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee)
    {
      &lt;font color="#008000"&gt;// TODO: implement
&lt;/font&gt;    }
  }
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
7. Open up “web.config” and modify the &lt;em&gt;system.serviceModel&lt;/em&gt; section as follows: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;services&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;service&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;behaviorConfiguration&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfServiceBehavior&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfService&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;address&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;binding&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;basicHttpBinding&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;contract&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.IMyWcfService&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;identity&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
     &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;dns&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;value&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;localhost&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
    &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;identity&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;endpoint&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;address&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;mex&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;binding&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;mexHttpBinding&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;contract&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;IMetadataExchange&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
  &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;service&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;services&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;behaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceBehaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
   &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;behavior&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;MyWcf.MyWcfServiceBehavior&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceMetadata&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;httpGetEnabled&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;true&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
    &amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;serviceDebug&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#ff0000"&gt;includeExceptionDetailInFaults&lt;/font&gt;&lt;font color="#0000ff"&gt;=&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt;false&lt;/font&gt;&amp;quot;&lt;font color="#0000ff"&gt; /&amp;gt;
   &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;behavior&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
  &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;serviceBehaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
 &amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;behaviors&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;system.serviceModel&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
(you might notice that most important part is the &lt;strong&gt;binding&lt;em&gt; &lt;/em&gt;&lt;/strong&gt;what I have changed to be &lt;strong&gt;basicHttpBinding&lt;/strong&gt;) 

  &lt;br /&gt;

  &lt;br /&gt;8. Go back to your “My Win App” project. 

  &lt;br /&gt;&amp;#160; - Remove “WebServices” web references 

  &lt;br /&gt;&amp;#160; - Add new “Service Reference” to your newly created WCF Service and name it “WcfServices” 

  &lt;br /&gt;&amp;#160; - Verify that you have “Reuse types in referenced assemblies” checked in settings (you can see them if you click &lt;em&gt;Advanced...&lt;/em&gt; button from the “&lt;em&gt;Add Service Reference”&lt;/em&gt; dialog): 

  &lt;br /&gt;&lt;img title="WCF 2" border="0" alt="WCF 2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/WhyWCFsinceIlikeWebServices_78D2/WCF%202_3.png" width="287" height="256" /&gt;&amp;#160; &lt;br /&gt;

  &lt;br /&gt;9. Modify your code to use this new service: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;MyLibrary.&lt;font color="#2b91af"&gt;Employee&lt;/font&gt; employee = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;Employee&lt;/font&gt;();
employee.FirstName = &lt;font color="#a31515"&gt;&amp;quot;John&amp;quot;&lt;/font&gt;;
employee.LastName = &lt;font color="#a31515"&gt;&amp;quot;Doe&amp;quot;&lt;/font&gt;;
WcfServices.&lt;font color="#2b91af"&gt;MyWcfServiceClient&lt;/font&gt; myWcf = &lt;font color="#0000ff"&gt;new&lt;/font&gt; MyWinApp.WcfServices.&lt;font color="#2b91af"&gt;MyWcfServiceClient&lt;/font&gt;();
myWcf.AddNewEmployee(employee);&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
10. Enjoy one of the benefits of WCF &lt;strong&gt;:-)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is just one of the many benefits that WCF over the “good old ASP.NET Web Services”. So if you’re interested then you should start looking more information on the web. 
  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9000423" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx">.NET General</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/WCF/default.aspx">WCF</category></item><item><title>Use LINQ to access CRM objects</title><link>http://blogs.msdn.com/jannemattila/archive/2008/08/29/use-linq-to-access-crm-objects.aspx</link><pubDate>Fri, 29 Aug 2008 15:43:07 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8904857</guid><dc:creator>jannemattila</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8904857.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8904857</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;If you have written small console application to check some data from CRM database you have probably already read this article from MSDN: &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/cc151204.aspx"&gt;Use Filtered Views&lt;/a&gt;. That is okay but honestly I’m currently more into LINQ solution. I’ll show you what I mean...&lt;/p&gt;  &lt;p&gt;First I'll create new Console Application project and &lt;em&gt;Add New Item&lt;/em&gt; to it and select &lt;em&gt;LINQ to SQL Classes&lt;/em&gt; and name it &lt;em&gt;CRMDataClasses.dbml&lt;/em&gt;:     &lt;br /&gt;&lt;img title="LINQDataClasses" border="0" alt="LINQDataClasses" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CRMandLINQ_C741/LINQDataClasses_3.png" width="544" height="304" /&gt; &lt;/p&gt;  &lt;p&gt;Then I'll use &lt;em&gt;Server Explorer&lt;/em&gt; to connect to CRM database:     &lt;br /&gt;&lt;img title="ServerExplorer" border="0" alt="ServerExplorer" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CRMandLINQ_C741/ServerExplorer_3.png" width="254" height="164" /&gt;&lt;/p&gt;  &lt;p&gt;And then I'll drag &lt;em&gt;Account, Contact, FilteredAccount &lt;/em&gt;and &lt;em&gt;FilteredContact&lt;/em&gt; to the canvas of our newly created &lt;em&gt;CRMDataClasses.dbml: &lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CRMandLINQ_C741/CRMDataClasses_5.png"&gt;&lt;img title="CRMDataClasses" border="0" alt="CRMDataClasses" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CRMandLINQ_C741/CRMDataClasses_thumb_1.png" width="700" height="264" /&gt;&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;Now I'm ready to use LINQ to these views:    &lt;br /&gt;    &lt;table cellspacing="10"&gt;&lt;tbody&gt;       &lt;tr&gt;         &lt;td valign="top" align="right"&gt;           &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#2b91af"&gt;CRMDataClassesDataContext&lt;/font&gt; dataContext = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;CRMDataClassesDataContext&lt;/font&gt;();

&lt;font color="#0000ff"&gt;var&lt;/font&gt; queryContact = &lt;font color="#0000ff"&gt;from&lt;/font&gt; contact &lt;font color="#0000ff"&gt;in&lt;/font&gt; dataContext.Contacts
          &lt;font color="#0000ff"&gt;where&lt;/font&gt; contact.MobilePhone.Length &amp;gt; 0 &amp;amp;&amp;amp;
          contact.LastName.Length &amp;gt; 0
    &lt;font color="#0000ff"&gt;select&lt;/font&gt; contact;

&lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;Contact(s):&amp;quot;&lt;/font&gt;);
&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#2b91af"&gt;Contact&lt;/font&gt; c &lt;font color="#0000ff"&gt;in&lt;/font&gt; queryContact)
{
  &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(c.FirstName + &lt;font color="#a31515"&gt;&amp;quot; &amp;quot;&lt;/font&gt; + c.LastName + &lt;font color="#a31515"&gt;&amp;quot;, &amp;quot;&lt;/font&gt; + c.MobilePhone);
}

&lt;font color="#0000ff"&gt;var&lt;/font&gt; queryFilteredContact = &lt;font color="#0000ff"&gt;from&lt;/font&gt; contact &lt;font color="#0000ff"&gt;in&lt;/font&gt; dataContext.FilteredContacts
          &lt;font color="#0000ff"&gt;select&lt;/font&gt; contact;
&lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;&amp;quot;&lt;/font&gt;);

&lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;&amp;quot;Filtered contact(s):&amp;quot;&lt;/font&gt;);
&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#2b91af"&gt;FilteredContact&lt;/font&gt; c &lt;font color="#0000ff"&gt;in&lt;/font&gt; queryFilteredContact)
{
  &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(c.lastname);
}&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
&lt;/p&gt;

&lt;p&gt;On lines 3 to 6 I queried all contacts that have lastname and mobilephone filled in. On lines 14 to 15 I'm querying all contacts where current user has access to. &lt;strong&gt;NOTE: It doesn't return anything if you use SQL Authentication!&lt;/strong&gt; So both of these can be used to fill you applications needs. But do notice that for some reason the attributes at the &lt;em&gt;FilteredContacts&lt;/em&gt; are all lower case and in &lt;em&gt;Contacts&lt;/em&gt; their naming is a bit different. So if you plan to change from &lt;em&gt;Contacts &lt;/em&gt;to &lt;em&gt;FilteredContact&lt;/em&gt; your going to have to change the casing of the attributes little bit.&lt;/p&gt;

&lt;p&gt;This was just quick advice how you can leverage LINQ to your CRM solutions.
  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking!

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8904857" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+CRM/default.aspx">Microsoft CRM</category></item><item><title>Maximize the use of CPU with parallel extensions (+ some WPF stuff)</title><link>http://blogs.msdn.com/jannemattila/archive/2008/08/26/maximize-the-use-of-cpu-with-parallel-extensions-some-wpf-stuff.aspx</link><pubDate>Tue, 26 Aug 2008 22:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8898129</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8898129.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8898129</wfw:commentRss><description>&lt;p&gt;&lt;/p&gt;  &lt;p&gt;&lt;/p&gt;  &lt;p&gt;Since this is my &lt;u&gt;40th post to this blog&lt;/u&gt; I decided to go back to square one… or &lt;a href="http://blogs.msdn.com/jannemattila/archive/2007/01/07/solving-small-puzzles-with-just-a-few-lines-of-code.aspx" target="_blank"&gt;post one&lt;/a&gt; actually &lt;strong&gt;:-)&lt;/strong&gt; I’m going to create Windows Presentation Foundation (WPF) application that solves the &lt;a href="http://en.wikipedia.org/wiki/Knight%27s_tour" target="_blank"&gt;Knight’s Tour&lt;/a&gt; puzzle. I actually didn’t know about this puzzle before I bought book called &lt;a href="http://www.wrox.com/WileyCDA/WroxTitle/Puzzles-for-Programmers-and-Pros.productCd-0470121688.html" target="_blank"&gt;Puzzles for Programmers and Pros&lt;/a&gt;. That book had interesting puzzle that lead in to this post. So here we go!&lt;/p&gt;  &lt;p&gt;I said that I’m going to create WPF application for my UI. You might ask why not the “good old” Windows Forms application...? Well for these simple reasons:    &lt;br /&gt;    &lt;br /&gt;&lt;strong&gt;1.&lt;/strong&gt;&amp;#160; I don’t like to write code to &lt;em&gt;OnPaint&lt;/em&gt; / &lt;em&gt;MainForm_Paint&lt;/em&gt; methods.     &lt;br /&gt;&lt;strong&gt;2. &lt;/strong&gt;I wanted to define my user interface and then just say in code “hey knight go there” and it should just draw the UI with the knight in the correct position. But the defined UI must be also scalable.     &lt;br /&gt;&lt;strong&gt;3. &lt;/strong&gt;WPF doesn’t have same barriers than Windows Forms does =&amp;gt; It’s the face of future applications!     &lt;br /&gt;    &lt;br /&gt;I don’t probably have to explain my reason #1 for you if you have experienced the same that I have :-) You’ll end up writing the UI code a lot and that’s not what you’re trying to do. You’re trying to solve puzzle and you are suddenly focusing for the UI code. That’s wrong approach. Therefore reason #2 goes hand-in-hand with #1. &lt;/p&gt;  &lt;p&gt;So let’s look the the UI of the running application:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/WPF%20UI_2.png"&gt;&lt;img title="WPF UI" style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="306" alt="WPF UI" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/WPF%20UI_thumb.png" width="306" border="0" /&gt;&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;It’s the view of the classic chess board and I decided to take shortcut when creating the knight. I decided to use gray circle instead (or ellipse actually) &lt;strong&gt;:-)&lt;/strong&gt; And for the layout management I just took the easy approach by using &lt;em&gt;Grid&lt;/em&gt; and defining &lt;em&gt;Columns&lt;/em&gt; and &lt;em&gt;Rows&lt;/em&gt;. Here is the XAML for the UI:&lt;/p&gt;  &lt;table cellspacing="10"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" align="right"&gt;         &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;

      &lt;td valign="top"&gt;
        &lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Window&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Class&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Window1&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;x&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;http://schemas.microsoft.com/winfx/2006/xaml&amp;quot;
&lt;/font&gt;  &lt;font color="#ff0000"&gt; Title&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Knight's Tour&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; MinHeight&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;100&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; MinWidth&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;100&amp;quot;&lt;/font&gt; 
    &lt;font color="#ff0000"&gt; Width&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;300&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Height&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;300&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Loaded&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Window_Loaded&amp;quot;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Board&amp;quot;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    
    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.ColumnDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;ColumnDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.ColumnDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.RowDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;RowDefinition&lt;/font&gt;&lt;font color="#0000ff"&gt; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid.RowDefinitions&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;

&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Ellipse&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Knight&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Fill&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;Gray&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Panel.ZIndex&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;1&amp;quot;
&lt;/font&gt;        &lt;font color="#ff0000"&gt; Grid.Column&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;{&lt;/font&gt;&lt;font color="#a31515"&gt;Binding&lt;/font&gt;&lt;font color="#ff0000"&gt; Path&lt;/font&gt;&lt;font color="#0000ff"&gt;=KnightX}&amp;quot;&lt;/font&gt; 
        &lt;font color="#ff0000"&gt; Grid.Row&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;{&lt;/font&gt;&lt;font color="#a31515"&gt;Binding&lt;/font&gt;&lt;font color="#ff0000"&gt; Path&lt;/font&gt;&lt;font color="#0000ff"&gt;=KnightY}&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    
    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Rectangle&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;A8&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Fill&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;White&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Rectangle&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;B8&amp;quot;&lt;/font&gt;&lt;font color="#ff0000"&gt; Grid.Column&lt;/font&gt;&lt;font color="#0000ff"&gt;=&amp;quot;1&amp;quot; /&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#008000"&gt;&amp;lt;!-- Etc... --&amp;gt;
&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Grid&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;
&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Window&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;You probably noticed the interesting part of the XAML... and that’s the lines 30 and 31 where &lt;em&gt;Binding &lt;/em&gt;is defined. It means that these values coming from the public properties of the &lt;em&gt;DataContext&lt;/em&gt;. So let’s look at the code behind that XAML and let’s discuss the binding little bit more: 

  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.ComponentModel;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Diagnostics;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows.Media;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows.Shapes;

&lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;partial&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;Window1&lt;/font&gt; : &lt;font color="#2b91af"&gt;Window&lt;/font&gt;, &lt;font color="#2b91af"&gt;INotifyPropertyChanged
&lt;/font&gt;{
  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; knightX = 0;
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; KnightX
  {
    &lt;font color="#0000ff"&gt;get&lt;/font&gt; { &lt;font color="#0000ff"&gt;return&lt;/font&gt; knightX; }
    &lt;font color="#0000ff"&gt;set
&lt;/font&gt;    {
      knightX = &lt;font color="#0000ff"&gt;value&lt;/font&gt;;
      NotifyPropertyChanged(&lt;font color="#a31515"&gt;&amp;quot;KnightX&amp;quot;&lt;/font&gt;);
    }
  }

  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; knightY = 0;
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;int&lt;/font&gt; KnightY
  {
    &lt;font color="#0000ff"&gt;get&lt;/font&gt; { &lt;font color="#0000ff"&gt;return&lt;/font&gt; knightY; }
    &lt;font color="#0000ff"&gt;set
&lt;/font&gt;    {
      knightY = &lt;font color="#0000ff"&gt;value&lt;/font&gt;;
      NotifyPropertyChanged(&lt;font color="#a31515"&gt;&amp;quot;KnightY&amp;quot;&lt;/font&gt;);
    }
  }

  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; Window_Loaded(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, &lt;font color="#2b91af"&gt;RoutedEventArgs&lt;/font&gt; e)
  {
    Knight.DataContext = &lt;font color="#0000ff"&gt;this&lt;/font&gt;;
    &lt;font color="#008000"&gt;// Now moving the Knight is easy!
&lt;/font&gt;    KnightX = 3;
    KnightY = 3;
  }

  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;event&lt;/font&gt; &lt;font color="#2b91af"&gt;PropertyChangedEventHandler&lt;/font&gt; PropertyChanged;
  &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; NotifyPropertyChanged(&lt;font color="#2b91af"&gt;String&lt;/font&gt; info)
  {
    &lt;font color="#0000ff"&gt;if&lt;/font&gt; (PropertyChanged != &lt;font color="#0000ff"&gt;null&lt;/font&gt;)
    {
      PropertyChanged(&lt;font color="#0000ff"&gt;this&lt;/font&gt;, &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;PropertyChangedEventArgs&lt;/font&gt;(info));
    }
  }
  &lt;font color="#008000"&gt;//...&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;
You might already noticed that my window also implements &lt;em&gt;INotyfyPropertyChanged&lt;/em&gt; interface. And my two properties actually call &lt;em&gt;NotifyPropertyChanged&lt;/em&gt; method when they are changed. So what’s this all about? Well if you don’t do this your values will be updated for the first time and after that they don’t actually get “bubbled” up to the ellipse anymore... unless you implement the notify mechanisms yourself. This is quite important and you should probably read more information about it on MSDN. &lt;/p&gt;

&lt;p&gt;For the actual solving part I just used classic “old recursion” to solve the puzzle. And this is the part where we finally are going to the &lt;em&gt;title&lt;/em&gt; of my post... &lt;/p&gt;

&lt;p&gt;Classic one worker thread approach gives “fairly easy to implement but sub-optimal” solution. And you might ask why? And to answer this question I’m going so show you picture of task manager: 
  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU1_2.png"&gt;&lt;img title="CPU1" height="116" alt="CPU1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU1_thumb.png" width="654" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;This picture was taken when my solver was running in “full speed ahead” –mode. And guess what... &lt;u&gt;I’m not impressed&lt;/u&gt;! I’m actually just using single CPU (see the third box where green line has reached the roof)!!! So my worker thread approach is far from optimal resource usage.&lt;/p&gt;

&lt;p&gt;Okay... What can I do then? I could do multiple threads and handle them manually but that’s again writing a lot of code that doesn’t have anything to do with the actual solving!? So if I would chosen Windows Forms + manual handling of multiple threads I would have a lot of code and just small fraction of that would actually do work that I was originally planning to do. &lt;/p&gt;

&lt;p&gt;This is where &lt;a href="http://blogs.msdn.com/pfxteam/" target="_blank"&gt;&lt;em&gt;Parallel Extensions&lt;/em&gt;&lt;/a&gt;&lt;em&gt; &lt;/em&gt;(&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3&amp;amp;displaylang=en" target="_blank"&gt;download&lt;/a&gt;)&lt;em&gt; &lt;/em&gt;comes into the game! It’s additional library (&lt;em&gt;System.Threading.dll&lt;/em&gt;) sitting on top of .NET Framework 3.5 and it’s currently in CTP phase. But I still highly recommend you to check it out if you want easily get more horse power to your algorithms. &lt;/p&gt;

&lt;p&gt;I analyzed my code and noticed part where I could do things differently: 
  &lt;table cellspacing="10"&gt;&lt;tbody&gt;
      &lt;tr&gt;
        &lt;td valign="top" align="right"&gt;
          &lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/font&gt;&lt;/pre&gt;
        &lt;/td&gt;

        &lt;td valign="top"&gt;
          &lt;pre&gt;&lt;font color="#008000"&gt;// My code was this:
&lt;/font&gt;&lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#0000ff"&gt;int&lt;/font&gt; location &lt;font color="#0000ff"&gt;in&lt;/font&gt; startLocations)
{
  &lt;font color="#008000"&gt;// Calculations here!
&lt;/font&gt;}

&lt;font color="#008000"&gt;// And I changed it to this:
&lt;/font&gt;&lt;font color="#2b91af"&gt;Parallel&lt;/font&gt;.ForEach&amp;lt;&lt;font color="#0000ff"&gt;int&lt;/font&gt;&amp;gt;(startLocations, (location) =&amp;gt;
{
  &lt;font color="#008000"&gt;// Calculations here!
&lt;/font&gt;});&lt;/pre&gt;
        &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/tbody&gt;&lt;/table&gt;

  &lt;br /&gt;So I changed code in line 2 to be the code at line 8. What was the result at the task manager then: 

  &lt;br /&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU2_2.png"&gt;&lt;img title="CPU2" height="111" alt="CPU2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/KnightsTourparallelism_CDDE/CPU2_thumb.png" width="650" border="0" /&gt;&lt;/a&gt;&amp;#160;&lt;/p&gt;

&lt;p&gt;Well I believe that I managed to get better use of the available horse power &lt;strong&gt;:-)&lt;/strong&gt; It shows of course in the results: 

  &lt;br /&gt;“foreach”: ~50 solved solutions in ~5 minutes 

  &lt;br /&gt;”Parallel.ForEach”: ~450 solved solutions in &amp;lt; 5 minutes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To summarize... &lt;/strong&gt;&lt;u&gt;I just changed 1 line of code and I was able to get unbelievable results from it!&lt;/u&gt; So if you’re doing something similar then I recommend checking out the parallel extensions first before doing “own custom solution” for that. &lt;/p&gt;

&lt;p&gt;I originally thought that I would go little bit deeper into the details of my solver but this post ended up too long even without it so maybe I’ll pass this time... But I’ll include &lt;a href="http://blogs.msdn.com/jannemattila/attachment/8898129.ashx" target="_blank"&gt;video clip&lt;/a&gt; that shows the UI of the application when it’s solving.

  &lt;br /&gt;

  &lt;br /&gt;Anyways... Happy hacking! 

  &lt;br /&gt;

  &lt;br /&gt;J&lt;/p&gt;

&lt;p&gt;P.S. I’m also interested in F# and I’m probably going to do something fun with that too.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8898129" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/jannemattila/attachment/8898129.ashx" length="18623" type="video/x-ms-wmv" /><category domain="http://blogs.msdn.com/jannemattila/archive/tags/.NET+General/default.aspx">.NET General</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Application+Development/default.aspx">Application Development</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category></item><item><title>SQL Server Profiler and SPQuery</title><link>http://blogs.msdn.com/jannemattila/archive/2008/06/21/sql-server-profiler-and-spquery.aspx</link><pubDate>Sat, 21 Jun 2008 22:58:08 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8633702</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8633702.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8633702</wfw:commentRss><description>&lt;p&gt;&lt;strong&gt;Question:&lt;/strong&gt; I’m using SPQuery to retrieve items from my SharePoint list and now I want to know little bit more about SPQuery properties. Actually I’m interested about performance related things since I’m not sure about my query currently. I currently have following code to retrieve my data:&lt;br&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; (&lt;font color="#2b91af"&gt;SPSite&lt;/font&gt; site = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;SPSite&lt;/font&gt;(&lt;font color="#a31515"&gt;"http://mysharepoint"&lt;/font&gt;))
{
  &lt;font color="#0000ff"&gt;using&lt;/font&gt; (&lt;font color="#2b91af"&gt;SPWeb&lt;/font&gt; web = site.RootWeb)
  {
    &lt;font color="#2b91af"&gt;SPList&lt;/font&gt; docs = web.Lists[&lt;font color="#a31515"&gt;"MyDocs"&lt;/font&gt;];
    &lt;font color="#2b91af"&gt;SPQuery&lt;/font&gt; query = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;SPQuery&lt;/font&gt;();
    query.Query = &lt;font color="#a31515"&gt;@"&amp;lt;Query&amp;gt;
        &amp;lt;Where&amp;gt;
         &amp;lt;And&amp;gt;
           &amp;lt;Eq&amp;gt;
            &amp;lt;FieldRef Name='MyField' /&amp;gt;
            &amp;lt;Value Type='Text'&amp;gt;12345&amp;lt;/Value&amp;gt;
           &amp;lt;/Eq&amp;gt;
           &amp;lt;Eq&amp;gt;
            &amp;lt;FieldRef Name='ContentType' /&amp;gt;
            &amp;lt;Value Type='Text'&amp;gt;MyContentType&amp;lt;/Value&amp;gt;
           &amp;lt;/Eq&amp;gt;
         &amp;lt;/And&amp;gt;
        &amp;lt;/Where&amp;gt;
      &amp;lt;/Query&amp;gt;"&lt;/font&gt;;

    &lt;font color="#2b91af"&gt;SPListItemCollection&lt;/font&gt; items = docs.GetItems(query);
    &lt;font color="#0000ff"&gt;foreach&lt;/font&gt; (&lt;font color="#2b91af"&gt;SPListItem&lt;/font&gt; item &lt;font color="#0000ff"&gt;in&lt;/font&gt; items)
    {
      &lt;font color="#2b91af"&gt;Console&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;"Item: "&lt;/font&gt; + item.Title);
    }
  }
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Answer: &lt;/strong&gt;To answer this question I need to step into dark side for a minute... I mean that I’m going to ask you to play with SQL Server Profiler to analyze your query more closely. This is of course to give some ideas how to tune your query using query properties and nothing else. I don’t want you to start use any custom SQL queries or other weird hacks that you might come up. Just tune the query and that’s it. &lt;br&gt;&lt;strong&gt;NOTE: &lt;/strong&gt;&lt;u&gt;Do this stuff only in you own dev box&lt;/u&gt;. Don’t even think anything else. &lt;/p&gt;
&lt;p&gt;Before we start using the SQL Server Profiler you might want to stop SharePoint related services so that it’s easier to read the SQL trace. I’ve stopped &lt;em&gt;IIS Admin &lt;/em&gt;(+related), &lt;em&gt;Office SharePoint Server Search&lt;/em&gt; and &lt;em&gt;Windows SharePoint Services*&lt;/em&gt;. Now you can start SQL Server Profiler and you’re ready to start tracing. Just create new trace (File-&amp;gt;New Trace...) and select &lt;em&gt;Tuning&lt;/em&gt; for your template:&lt;br&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SQLProfilerandSQQuery_11109/SQLServerProfiler1_2.png" target="_blank"&gt;&lt;img border="0" alt="SQLServerProfiler1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SQLProfilerandSQQuery_11109/SQLServerProfiler1_thumb.png" width="640" height="404"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;And if I now run your query I’ll get something like 200+ rows in my trace window. But you’re probably just interested for the rows that tell what is going on. If you pause the trace after the query then it’s most likely the last line in the grid:&lt;br&gt;&lt;img border="0" alt="SQLServerProfiler2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SQLProfilerandSQQuery_11109/SQLServerProfiler2_068b6c0e-c3e1-4700-b280-ace6b48cf709.png" width="566" height="177"&gt;&amp;nbsp;&lt;br&gt;It gives you fairly long SQL (mine was ~4500 characters long) and it’s not trivial to understand what it is really doing. &lt;/p&gt;
&lt;p&gt;But first you probably notice the interesting&lt;em&gt;&amp;nbsp; SELECT TOP &lt;u&gt;2147483648&lt;/u&gt; ...&lt;/em&gt; And that’s the first thing you might want to change (of course you know best what you’re trying to achieve). But for that you have property called &lt;em&gt;RowLimit&lt;/em&gt;. So if you add “&lt;em&gt;query.RowLimit = 10;”&lt;/em&gt; to your code and re-run the trace you get following output:&lt;br&gt;&lt;img border="0" alt="SQLServerProfiler3" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SQLProfilerandSQQuery_11109/SQLServerProfiler3_c772bad8-4893-4c39-be9e-3f0f40c253d9.png" width="468" height="93"&gt;&amp;nbsp;&lt;br&gt;So now you have &lt;em&gt;SELECT TOP 11 ...&lt;/em&gt; which is better (note that the actual limit is your &lt;em&gt;RowLimit + 1&lt;/em&gt;). If you only need to display &lt;em&gt;n&lt;/em&gt; rows from database then don’t retrieve all and display just &lt;em&gt;n =&amp;gt; &lt;/em&gt;Use &lt;em&gt;RowLimit &lt;/em&gt;to narrow down the resultset.&lt;/p&gt;
&lt;p&gt;Next thing we need to take into consideration is &lt;em&gt;ViewFields&lt;/em&gt;. If I know that I’m only going to show &lt;em&gt;Title&lt;/em&gt;, then why should I need to retrieve more fields. Let’s narrow down the fields using &lt;em&gt;ViewFields&lt;/em&gt;:&lt;br&gt;
&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;query.ViewFields = &lt;font color="#a31515"&gt;@"&amp;lt;FieldRef Name='ID'/&amp;gt;
                     &amp;lt;FieldRef Name='Title'/&amp;gt;"&lt;/font&gt;;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;
&lt;p&gt;And again let’s test the trace and compare the query for the first trace. You may notice that the SQL is &lt;em&gt;only&lt;/em&gt; ~3000 chars long anymore and amount of columns at the resultset is a lot less (screenshot from management studio when both queries have been executed):&lt;br&gt;&lt;img border="0" alt="SQLServerProfiler4" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SQLProfilerandSQQuery_11109/SQLServerProfiler4_74dcdd16-91ed-4f65-9b7d-7fb9e23e54d9.png" width="384" height="207"&gt;&amp;nbsp;&lt;br&gt;Notice the size of scrollbar…. larger is better &lt;strong&gt;:-)&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now I have just added few lines of code and the result is ~1500 shorter SQL and we’re only getting the amount of rows from DB that we need to. I’m not going to play with another properties but if you’re interested you might want to start with &lt;em&gt;Include*&lt;/em&gt; properties. &lt;/p&gt;
&lt;p&gt;I showed small example that how can use SQL Server toolset to verify (at least for some extend) that your queries are going for the right direction. &lt;br&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8633702" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+Office+SharePoint+Server+2007/default.aspx">Microsoft Office SharePoint Server 2007</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Creating Localization Tool with Silverlight</title><link>http://blogs.msdn.com/jannemattila/archive/2008/06/08/creating-localization-tool-with-silverlight.aspx</link><pubDate>Sun, 08 Jun 2008 15:11:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8580696</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8580696.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8580696</wfw:commentRss><description>&lt;p&gt;Since &lt;a href="http://silverlight.net/GetStarted/" mce_href="http://silverlight.net/GetStarted/" target="_blank"&gt;Silverlight 2&lt;/a&gt; is in Beta 2 phase it’s definately time to create small application with it. Together with my &lt;a href="http://jarmoh.spaces.live.com/" mce_href="http://jarmoh.spaces.live.com/" target="_blank"&gt;friend&lt;/a&gt; we’e created small example application called &lt;i&gt;Localization Tool&lt;/i&gt;. Idea in our example application is pretty simple... allow end users to modify texts used in application. And for the end user it should be easy, intuitive and of course fast. We used a lot of information available from internet in order to help us in the implementation (see links at the end of this post). &lt;/p&gt; &lt;p&gt;Before going into implementation details you can check out the final solution in action (click the image to open video in new window):&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;a href="http://silverlight.services.live.com/invoke/48523/xEncoderApp0/iframe.html" title="Silverlight Localization Tool in action" mce_href="http://silverlight.services.live.com/invoke/48523/xEncoderApp0/iframe.html" target="_blank"&gt;&lt;img src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool_2141408c-2006-4e97-8f68-9ce334353abf.png" alt="Localization Tool" mce_src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool_2141408c-2006-4e97-8f68-9ce334353abf.png" border="0" height="361" width="640"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;In our solution we took WCF + LINQ + SQL approach and created following simple datatable:&lt;br&gt;&lt;img src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table_f27bf6b5-aa8f-442e-b047-4d8321fd6f2c.png" alt="Localization Tool table" mce_src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table_f27bf6b5-aa8f-442e-b047-4d8321fd6f2c.png" border="0" height="131" width="308"&gt; &lt;br&gt;That table is then used to store data like this:&lt;br&gt;&lt;img src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table%20data_c9c96871-7919-46eb-8d31-6c0eb2e358a7.png" alt="Localization Tool table data" mce_src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table%20data_c9c96871-7919-46eb-8d31-6c0eb2e358a7.png" border="0" height="149" width="485"&gt; &lt;br&gt;After that we’ve created LINQ to SQL data class for our table:&lt;br&gt;&lt;img src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table%20data%20classes_33435a74-44f0-404b-a1fb-887b6b84e955.png" alt="Localization Tool table data classes" mce_src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20table%20data%20classes_33435a74-44f0-404b-a1fb-887b6b84e955.png" border="0" height="163" width="202"&gt; &lt;br&gt;Then we created service that provides data access to our localization table. All code behind that service is here:&lt;/p&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;8&lt;br&gt;9&lt;br&gt;10&lt;br&gt;11&lt;br&gt;12&lt;br&gt;13&lt;br&gt;14&lt;br&gt;15&lt;br&gt;16&lt;br&gt;17&lt;br&gt;18&lt;br&gt;19&lt;br&gt;20&lt;br&gt;21&lt;br&gt;22&lt;br&gt;23&lt;br&gt;24&lt;br&gt;25&lt;br&gt;26&lt;br&gt;27&lt;br&gt;28&lt;br&gt;29&lt;br&gt;30&lt;br&gt;31&lt;br&gt;32&lt;br&gt;33&lt;br&gt;34&lt;br&gt;35&lt;br&gt;36&lt;br&gt;37&lt;br&gt;38&lt;br&gt;39&lt;br&gt;40&lt;br&gt;41&lt;br&gt;42&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System;&lt;br&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Collections.Generic;&lt;br&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Linq;&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; LocalizationTool_Web&lt;br&gt;{&lt;br&gt;  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;LocalizationService&lt;/font&gt; : &lt;font color="#2b91af"&gt;ILocalizationService&lt;br&gt;&lt;/font&gt;  {&lt;br&gt;    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#2b91af"&gt;List&lt;/font&gt;&amp;lt;&lt;font color="#2b91af"&gt;Localization&lt;/font&gt;&amp;gt; GetLocalizationByLanguage(&lt;font color="#0000ff"&gt;string&lt;/font&gt; language)&lt;br&gt;    {&lt;br&gt;      &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt; dataContext = &lt;br&gt;        &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt;();&lt;br&gt;      &lt;font color="#0000ff"&gt;var&lt;/font&gt; query = &lt;font color="#0000ff"&gt;from&lt;/font&gt; localization &lt;font color="#0000ff"&gt;in&lt;/font&gt; dataContext.Localizations&lt;br&gt;            &lt;font color="#0000ff"&gt;where&lt;/font&gt; localization.Language.Equals(language)&lt;br&gt;            &lt;font color="#0000ff"&gt;select&lt;/font&gt; localization;&lt;br&gt;      &lt;font color="#0000ff"&gt;return&lt;/font&gt; query.ToList();&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; UpdateLocalization(&lt;font color="#2b91af"&gt;Localization&lt;/font&gt; localization)&lt;br&gt;    {&lt;br&gt;      &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt; dataContext = &lt;br&gt;        &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt;();&lt;br&gt;      &lt;font color="#0000ff"&gt;var&lt;/font&gt; updateRow = dataContext.Localizations.First(&lt;br&gt;        loc =&amp;gt; &lt;br&gt;          loc.LanguageKey.Equals(localization.LanguageKey) &amp;amp;&amp;amp; &lt;br&gt;          loc.Language.Equals(localization.Language));&lt;br&gt;      updateRow.Text = localization.Text;&lt;br&gt;      updateRow.Comment = localization.Comment;&lt;br&gt;      dataContext.SubmitChanges();&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#2b91af"&gt;String&lt;/font&gt; [] GetLocales()&lt;br&gt;    {&lt;br&gt;      &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt; dataContext = &lt;br&gt;        &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;LocalizationDataClassesDataContext&lt;/font&gt;();&lt;br&gt;      &lt;font color="#0000ff"&gt;var&lt;/font&gt; query = (&lt;font color="#0000ff"&gt;from&lt;/font&gt; localization &lt;font color="#0000ff"&gt;in&lt;/font&gt; dataContext.Localizations&lt;br&gt;             &lt;font color="#0000ff"&gt;select&lt;/font&gt; localization.Language).Distinct();&lt;br&gt;&lt;br&gt;      &lt;font color="#0000ff"&gt;return&lt;/font&gt; query.ToArray();&lt;br&gt;    }&lt;br&gt;  }&lt;br&gt;}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Now we’re ready to create UI for our application! We’ve used stackpanels for the layout management and then we used few &lt;i&gt;Style&lt;/i&gt; elements to make things look little bit better. &lt;br&gt;Here is small example about the styles in our &lt;i&gt;App.xaml&lt;/i&gt;:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;8&lt;br&gt;9&lt;br&gt;10&lt;br&gt;11&lt;br&gt;12&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Application&lt;/font&gt;&lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;="http://schemas.microsoft.com/client/2007"&lt;br&gt;&lt;/font&gt;      &lt;font color="#ff0000"&gt; xmlns&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;x&lt;/font&gt;&lt;font color="#0000ff"&gt;="http://schemas.microsoft.com/winfx/2006/xaml"&lt;/font&gt; &lt;br&gt;      &lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Class&lt;/font&gt;&lt;font color="#0000ff"&gt;="LocalizationTool.App"&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Application.Resources&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;br&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Style&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Key&lt;/font&gt;&lt;font color="#0000ff"&gt;="Border"&lt;/font&gt;&lt;font color="#ff0000"&gt; TargetType&lt;/font&gt;&lt;font color="#0000ff"&gt;="Border"&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Setter&lt;/font&gt;&lt;font color="#ff0000"&gt; Property&lt;/font&gt;&lt;font color="#0000ff"&gt;="CornerRadius"&lt;/font&gt;&lt;font color="#ff0000"&gt; Value&lt;/font&gt;&lt;font color="#0000ff"&gt;="10"/&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Setter&lt;/font&gt;&lt;font color="#ff0000"&gt; Property&lt;/font&gt;&lt;font color="#0000ff"&gt;="Background"&lt;/font&gt;&lt;font color="#ff0000"&gt; Value&lt;/font&gt;&lt;font color="#0000ff"&gt;="#FFdedede"/&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Setter&lt;/font&gt;&lt;font color="#ff0000"&gt; Property&lt;/font&gt;&lt;font color="#0000ff"&gt;="VerticalAlignment"&lt;/font&gt;&lt;font color="#ff0000"&gt; Value&lt;/font&gt;&lt;font color="#0000ff"&gt;="Top"/&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;Setter&lt;/font&gt;&lt;font color="#ff0000"&gt; Property&lt;/font&gt;&lt;font color="#0000ff"&gt;="Margin"&lt;/font&gt;&lt;font color="#ff0000"&gt; Value&lt;/font&gt;&lt;font color="#0000ff"&gt;="8,8,8,8"/&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;Style&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#008000"&gt;&amp;lt;!-- ... and few more styles ... –&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;br&gt;At the &lt;i&gt;UserControl_Loaded&lt;/i&gt; we query for all the possible locales so that we can list them add the &lt;i&gt;ListBox.&lt;/i&gt; Here is the code for that (unfortunately the code looks terrible since it’s splitted into several rows only for display reasons):&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;8&lt;br&gt;9&lt;br&gt;10&lt;br&gt;11&lt;br&gt;12&lt;br&gt;13&lt;br&gt;14&lt;br&gt;15&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; UserControl_Loaded(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, &lt;font color="#2b91af"&gt;RoutedEventArgs&lt;/font&gt; e)&lt;br&gt;{&lt;br&gt;  LocalizationClientService.&lt;font color="#2b91af"&gt;LocalizationServiceClient&lt;/font&gt; client = &lt;br&gt;    &lt;font color="#0000ff"&gt;new&lt;/font&gt; LocalizationTool.LocalizationClientService.&lt;font color="#2b91af"&gt;LocalizationServiceClient&lt;/font&gt;();&lt;br&gt;  client.GetLocalesCompleted += &lt;br&gt;    &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;EventHandler&lt;/font&gt;&amp;lt;LocalizationClientService.&lt;font color="#2b91af"&gt;GetLocalesCompletedEventArgs&lt;/font&gt;&amp;gt;&lt;br&gt;      (client_GetLocalesCompleted);&lt;br&gt;  client.GetLocalesAsync();&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;font color="#0000ff"&gt;void&lt;/font&gt; client_GetLocalesCompleted(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, &lt;br&gt;  LocalizationClientService.&lt;font color="#2b91af"&gt;GetLocalesCompletedEventArgs&lt;/font&gt; e)&lt;br&gt;{&lt;br&gt;  languages.ItemsSource = e.Result;&lt;br&gt;}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Using that same method we query all the localizations specific for that selected locale. There’s nothing fancy about that.. but let’s look at the search functionality little bit closer.&lt;br&gt;Our search button was defined like this in our &lt;i&gt;Page.xaml&lt;/i&gt;:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;TextBox&lt;/font&gt;&lt;font color="#ff0000"&gt; Style&lt;/font&gt;&lt;font color="#0000ff"&gt;="{&lt;/font&gt;&lt;font color="#a31515"&gt;StaticResource&lt;/font&gt;&lt;font color="#ff0000"&gt; TextBox&lt;/font&gt;&lt;font color="#0000ff"&gt;}"&lt;/font&gt; &lt;br&gt;    &lt;font color="#ff0000"&gt; TextChanged&lt;/font&gt;&lt;font color="#0000ff"&gt;="searchCriteria_TextChanged"&lt;/font&gt; &lt;br&gt;    &lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;="searchCriteria" /&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;And in our code behind we had following code to execute the query:&lt;br&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;8&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#2b91af"&gt;String&lt;/font&gt; search = searchCriteria.Text.ToLower();&lt;br&gt;&lt;font color="#0000ff"&gt;var&lt;/font&gt; query = &lt;font color="#0000ff"&gt;from&lt;/font&gt; row &lt;font color="#0000ff"&gt;in&lt;/font&gt; rows&lt;br&gt;      &lt;font color="#0000ff"&gt;where&lt;br&gt;&lt;/font&gt;        row.LanguageKey.ToLower().Contains(search) ||&lt;br&gt;        row.Text.ToLower().Contains(search) ||&lt;br&gt;        row.Comment.ToLower().Contains(search)&lt;br&gt;      &lt;font color="#0000ff"&gt;select&lt;/font&gt; row;&lt;br&gt;SetDatagridItemSource(query.ToArray());&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;And you can probably pretty easily see that actually that search box functionality doesn’t retrieve data from server at all. It uses the previously retrieved data (variable &lt;i&gt;rows&lt;/i&gt;) and filters out it by using linq. Therefore the performance is really good.&lt;/p&gt;
&lt;p&gt;We also added functionality to view two different languages at the same time. This helps to make the translations since the user can directly see what’s the equivalent row in another language. This was implemented withthe concept &lt;i&gt;base language. &lt;/i&gt;User can select what language he/she wants to use at the compare. Here’s example of that:&lt;br&gt;&lt;img src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20lock%20base%20language_f0b9535b-7415-4dbb-8df8-25c5f3fde44e.png" alt="Localization Tool lock base language" mce_src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CreatingLocalizationToolwithSilverlight_195A/Localization%20Tool%20lock%20base%20language_f0b9535b-7415-4dbb-8df8-25c5f3fde44e.png" border="0" height="81" width="401"&gt;&amp;nbsp; &lt;br&gt;This was implemented by using the &lt;i&gt;RowDetails&lt;/i&gt; of datagrid. Here is the xaml of our datagrid:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td align="right" valign="top"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;br&gt;2&lt;br&gt;3&lt;br&gt;4&lt;br&gt;5&lt;br&gt;6&lt;br&gt;7&lt;br&gt;8&lt;br&gt;9&lt;br&gt;10&lt;br&gt;11&lt;br&gt;12&lt;br&gt;13&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;System_Windows_Controls&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#a31515"&gt;DataGrid&lt;/font&gt; &lt;br&gt;  &lt;font color="#ff0000"&gt; AutoGenerateColumns&lt;/font&gt;&lt;font color="#0000ff"&gt;="True"&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;="resultsDatagrid"&lt;br&gt;&lt;/font&gt;  &lt;font color="#ff0000"&gt; Width&lt;/font&gt;&lt;font color="#0000ff"&gt;="770"&lt;/font&gt;&lt;font color="#ff0000"&gt; Height&lt;/font&gt;&lt;font color="#0000ff"&gt;="290"&lt;/font&gt;&lt;font color="#ff0000"&gt; Margin&lt;/font&gt;&lt;font color="#0000ff"&gt;="10,10,10,10"&lt;br&gt;&lt;/font&gt;  &lt;font color="#ff0000"&gt; CommittingEdit&lt;/font&gt;&lt;font color="#0000ff"&gt;="resultsDatagrid_CommittingEdit"&lt;br&gt;&lt;/font&gt;  &lt;font color="#ff0000"&gt; SelectionChanged&lt;/font&gt;&lt;font color="#0000ff"&gt;="resultsDatagrid_SelectionChanged"&lt;br&gt;&lt;/font&gt;  &lt;font color="#ff0000"&gt; LoadingRowDetails&lt;/font&gt;&lt;font color="#0000ff"&gt;="resultsDatagrid_LoadingRowDetails"&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;System_Windows_Controls&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#a31515"&gt;DataGrid.RowDetailsTemplate&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;DataTemplate&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;      &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;&lt;/font&gt;&lt;font color="#a31515"&gt;StackPanel&lt;/font&gt;&lt;font color="#ff0000"&gt; x&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#ff0000"&gt;Name&lt;/font&gt;&lt;font color="#0000ff"&gt;="tooltipPanel"&lt;/font&gt; &lt;br&gt;            &lt;font color="#ff0000"&gt; Orientation&lt;/font&gt;&lt;font color="#0000ff"&gt;="Horizontal"&lt;/font&gt;&lt;font color="#ff0000"&gt; Background&lt;/font&gt;&lt;font color="#0000ff"&gt;="LightYellow" /&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;    &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;DataTemplate&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&lt;/font&gt;&lt;font color="#a31515"&gt;  &lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;System_Windows_Controls&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#a31515"&gt;DataGrid.RowDetailsTemplate&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;br&gt;&amp;lt;/&lt;/font&gt;&lt;font color="#a31515"&gt;System_Windows_Controls&lt;/font&gt;&lt;font color="#0000ff"&gt;:&lt;/font&gt;&lt;font color="#a31515"&gt;DataGrid&lt;/font&gt;&lt;font color="#0000ff"&gt;&amp;gt;&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;And in &lt;i&gt;resultsDatagrid_LoadingRowDetails &lt;/i&gt;we just searched for the corresponding data row from our local base language array. If row was found then we dynamically created &lt;i&gt;TextBoxes&lt;/i&gt; to display the values of base language.&lt;/p&gt;
&lt;p&gt;This was just small example application that demonstrates the possibilities of Silverlight. Hopefully you got the idea that you can do pretty cool business applications with it.&lt;br&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;
&lt;h3&gt;Links:&lt;/h3&gt;
&lt;p&gt;&lt;a href="http://silverlight.net/GetStarted/" mce_href="http://silverlight.net/GetStarted/" target="_blank"&gt;Silverlight.Net - Getting started&lt;/a&gt;&lt;br&gt;&lt;a href="http://blogs.msdn.com/swiss_dpe_team/archive/2008/03/17/silverlight-2-beta1-wcf-linq-to-sql-a-powerfull-combination.aspx" mce_href="http://blogs.msdn.com/swiss_dpe_team/archive/2008/03/17/silverlight-2-beta1-wcf-linq-to-sql-a-powerfull-combination.aspx" target="_blank"&gt;Silverlight 2 Beta1 + WCF + LINQ to SQL = a powerfull combination&lt;/a&gt;&lt;br&gt;&lt;a href="http://silverlight.net/learn/tutorials/sqldatagrid.aspx" mce_href="http://silverlight.net/learn/tutorials/sqldatagrid.aspx" target="_blank"&gt;Displaying SQL Database Data in a DataGrid using LINQ and WCF&lt;/a&gt;&lt;br&gt;&lt;a href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx" mce_href="http://weblogs.asp.net/scottgu/pages/silverlight-tutorial-part-3-using-networking-to-retrieve-data-and-populate-a-datagrid.aspx" target="_blank"&gt;Scott Guthrie: Silverlight Tutorial Part 3: Using Networking to Retrieve Data and Populate a DataGrid&lt;/a&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8580696" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/jannemattila/attachment/8580696.ashx" length="240744" type="application/zip" /><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Silverlight/default.aspx">Silverlight</category></item><item><title>Adding CRM 4.0 to your own win app with web form authentication</title><link>http://blogs.msdn.com/jannemattila/archive/2008/05/26/adding-crm-4-0-to-your-own-win-app-with-web-form-authentication.aspx</link><pubDate>Mon, 26 May 2008 09:15:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8551960</guid><dc:creator>jannemattila</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8551960.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8551960</wfw:commentRss><description>&lt;p&gt;Last year I wrote small article &lt;a href="http://blogs.msdn.com/jannemattila/archive/2007/09/25/adding-ms-crm-to-your-own-windows-forms-applications.aspx" target="_blank"&gt;Adding MS CRM to your own windows forms applications&lt;/a&gt; that demonstrates the usage of CRM in your own custom windows forms applications. In CRM 4.0 the story is pretty much the same but there’re few things that you need to take into account so that everything works as you expect. One thing is multi-tenancy and another one is different authentication mechanisms. CRM 4.0 introduces new authentication mechanism called Service Provider License Agreement / SPLA / IFD / Internet Facing Deployment (it has sooo many names :-). Basicly it just means that you login to CRM using web form so it isn’t anything fancier. And that authentication is of course cookie based so if you use Fiddler you’ll see something like this (Note: image is clipped):&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="CRMCookie" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Add.0toyourownwinappwithwebformauthentic_6429/CRMCookie_0e5cfaaf-10d3-46e3-be26-73dba2a6c877.jpg" width="344" height="105"&gt; &lt;/p&gt; &lt;p&gt;So there is &lt;em&gt;MSCRMSession&lt;/em&gt; ticket that authenticates the user to the system. Okay fine… but what if I want to spawn CRM session whenever I like and still provide “&lt;em&gt;auto login” &lt;/em&gt;for my end users (and of course you’re not using windows authentication a.k.a. On Premise authentication)? Let’s first use Fiddler to determine what’s happening under covers in IFD (and compare it to win auth):&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Add.0toyourownwinappwithwebformauthentic_6429/IFD_2.jpg" target="_blank"&gt;&lt;img border="0" alt="IFD" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Add.0toyourownwinappwithwebformauthentic_6429/IFD_thumb.jpg" width="700" height="252"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;Here’s screenshot of Fiddler when user logs on (see highlighted username and password). So the login is just normal HTTP POST. &lt;/p&gt; &lt;p&gt;If we compare that to win auth then we’ll see that there’s that normal &lt;em&gt;authorization&lt;/em&gt; headers that makes the magic happen:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Add.0toyourownwinappwithwebformauthentic_6429/WinAuth_2.jpg" target="_blank"&gt;&lt;img border="0" alt="WinAuth" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Add.0toyourownwinappwithwebformauthentic_6429/WinAuth_thumb.jpg" width="700" height="230"&gt;&lt;/a&gt;&lt;/p&gt; &lt;p&gt;You have probably noticed the other difference... which is status code 401. In win auth the system returns 401 which starts the authentication routine. In IFD you’ll save time because you don’t do those 401 round-trips.&lt;/p&gt; &lt;p&gt;But okay.. now we need to achieve “&lt;em&gt;auto login"&lt;/em&gt; and for that we’ll need little bit of code. Example code has been taken from &lt;a href="http://support.microsoft.com/kb/174923" target="_blank"&gt;How To Use the PostData Parameter in WebBrowser Control&lt;/a&gt; and it has been modified for this case. And here we go:&lt;/p&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;string&lt;/font&gt; signInUrl = &lt;font color="#a31515"&gt;"http://mcs.mycrmserver.local/signin.aspx?targeturl="&lt;/font&gt;;
&lt;font color="#0000ff"&gt;string&lt;/font&gt; destinationUrl = &lt;font color="#2b91af"&gt;HttpUtility&lt;/font&gt;.UrlEncode(&lt;font color="#a31515"&gt;"http://mcs.mycrmserver.local/loader.aspx"&lt;/font&gt;);
&lt;font color="#0000ff"&gt;using&lt;/font&gt; (&lt;font color="#2b91af"&gt;WebBrowser&lt;/font&gt; browser = &lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;WebBrowser&lt;/font&gt;())
{
  &lt;font color="#0000ff"&gt;string&lt;/font&gt; postData = &lt;font color="#a31515"&gt;"txtUserName="&lt;/font&gt; + username + &lt;font color="#a31515"&gt;"&amp;amp;txtPassword="&lt;/font&gt; + password;
  browser.Navigate(
    signInUrl + destinationUrl &lt;font color="#008000"&gt;/* Url */&lt;/font&gt;,
    &lt;font color="#a31515"&gt;"_blank"&lt;/font&gt; &lt;font color="#008000"&gt;/* Target */&lt;/font&gt;,
    &lt;font color="#2b91af"&gt;Encoding&lt;/font&gt;.UTF8.GetBytes(postData) &lt;font color="#008000"&gt;/* Postdata */&lt;/font&gt;,
    &lt;font color="#a31515"&gt;"Content-Type: application/x-www-form-urlencoded"&lt;/font&gt; + &lt;font color="#2b91af"&gt;Environment&lt;/font&gt;.NewLine &lt;font color="#008000"&gt;/* Headers */&lt;/font&gt;);
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;That code clips uses &lt;em&gt;System.Windows.Forms.WebBrowser &lt;/em&gt;component to POST the login information to CRM so that end user won’t notice the logon event at all. In this example the user will be redirected into the main CRM page (as defined at line 2) but you can of course use any page under CRM if you need to.&lt;/p&gt;
&lt;p&gt;Obviously this approach poses some security threats since you need to store username and especially password in memory. But you can make this thing better by storing the confidential values at &lt;em&gt;System.Security.SecureString&lt;/em&gt; (more info can be found in &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.securestring.aspx" target="_blank"&gt;MSDN&lt;/a&gt;). &lt;em&gt;SecureString&lt;/em&gt; is designed for storing confidential text. And of course you should use HTTPS so that you don’t pass on the information plain text. &lt;/p&gt;
&lt;p&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8551960" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+CRM/default.aspx">Microsoft CRM</category></item><item><title>CRM 4.0 (or SharePoint or custom application) and DebugView</title><link>http://blogs.msdn.com/jannemattila/archive/2008/05/07/crm-4-0-or-sharepoint-or-custom-application-and-debugview.aspx</link><pubDate>Wed, 07 May 2008 10:04:03 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8465208</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8465208.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8465208</wfw:commentRss><description>&lt;p&gt;Every now and then I’m find myself trying to solve same issues over and over again :-) That’s why I found myself (again) using &lt;em&gt;DebugView&lt;/em&gt; as my debugging assistant at remote box. If you don’t know what &lt;em&gt;DebugView&lt;/em&gt; is then you should definitely try it out. I’m going to give you few ideas how you could use it at your applications. You can download the &lt;em&gt;DebugView&lt;/em&gt; from &lt;a title="DebugView download" href="http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx" target="_blank"&gt;Technet&lt;/a&gt;.&lt;/p&gt; &lt;p&gt;Why do I use &lt;em&gt;DebugView&lt;/em&gt;? Well I want to get debug messages from running system BUT... I don’t want to write to EventLog or to File since it’s totally unnecessary to write all the messages all the time. I just want messages when I’m ready to observe the system. And DebugView is handy tool for that. Here is list of steps how I normally implement that kind of approach:&lt;br&gt;1) Create log class that implements logging/tracing/debugging (this is pretty much just wrapper to few simple method calls) (I recommend making this as singleton)&lt;br&gt;2) Use you log class in your application/solution&lt;br&gt;3) Use &lt;em&gt;DebugView &lt;/em&gt;to follow the trace messages written by your log class&lt;/p&gt; &lt;p&gt;In 1) I mention that I recommend using singleton approach at the log class. This is important especially at the CRM 4.0 where you need to add default trace listener so that you’ll get the trace messages. Here is the code example for that:&lt;/p&gt; &lt;p&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Diagnostics;
&lt;font color="#008000"&gt;// ...
&lt;/font&gt;&lt;font color="#2b91af"&gt;Trace&lt;/font&gt;.Listeners.Add(&lt;font color="#0000ff"&gt;new&lt;/font&gt; &lt;font color="#2b91af"&gt;DefaultTraceListener&lt;/font&gt;());&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;
&lt;p&gt;After you have added the default listener you can just use this oneliner:&lt;/p&gt;
&lt;p&gt;
&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#2b91af"&gt;Trace&lt;/font&gt;.WriteLine(&lt;font color="#a31515"&gt;"My Trace: "&lt;/font&gt; + message);&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt;
&lt;p&gt;Now your trace can be seen at the &lt;em&gt;DebugView&lt;/em&gt;:&lt;br&gt;&lt;img border="0" alt="DebugView" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/CRM4.0andDebugView_60C3/DebugView_c8bc2cb2-c36a-463a-9b14-168b4e41f642.png" width="470" height="243"&gt;&lt;br&gt;(here is CRM 4.0 where I have menu item that points to custom ASPX page and I’m using “My Trace” to see what’s happening in there)&lt;/p&gt;
&lt;p&gt;&lt;em&gt;DebugView&lt;/em&gt; has many built-in functionalities like “Save As” etc. that can help you on your debug/trace efforts. So I recommend that you learn to play around with it.&lt;/p&gt;
&lt;p&gt;I also had the magic word “SharePoint” in my title. That is for the simple reason that this same story applies to SharePoint too. And don’t forget... Since &lt;em&gt;System.Diagnostics.Trace&lt;/em&gt; is implemented at the .NET Framework this story also applies to all other applications which are built on top of that.&lt;/p&gt;
&lt;p&gt;You might also want to read my previous post about tracing: &lt;a title="CRM 4.0, SharePoint and ASP.NET Trace" href="http://blogs.msdn.com/jannemattila/archive/2008/02/23/crm-4-0-sharepoint-and-asp-net-trace.aspx" target="_blank"&gt;CRM 4.0, SharePoint and ASP.NET Trace&lt;/a&gt;.&lt;br&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8465208" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Application+Development/default.aspx">Application Development</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+Office+SharePoint+Server+2007/default.aspx">Microsoft Office SharePoint Server 2007</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+CRM/default.aspx">Microsoft CRM</category></item><item><title>Simple Outlook Add-in with VSTO 3.0</title><link>http://blogs.msdn.com/jannemattila/archive/2008/04/06/simple-outlook-add-in-with-vsto-3-0.aspx</link><pubDate>Sun, 06 Apr 2008 22:56:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8363721</guid><dc:creator>jannemattila</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/8363721.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=8363721</wfw:commentRss><description>&lt;p&gt;Creating add-ins with Visual Studio 2008 and VSTO 3.0 (Visual Studio Tools for Office) is easy and fast… Just use File –&amp;gt; New –&amp;gt; Project –&amp;gt; Select correct add-in i.e. Outlook 2007 Add-in and you project template is created. And you can run you add-in in debug by just hitting F5! So the basic setup is just few clicks and then you’re ready to start implementing your actual functionality i.e. extending ribbon etc.&lt;/p&gt; &lt;p&gt;In this post I’m going to create simple add-in that demonstrates how you can send emails on behalf of someone else and the selection for doing that is done automatically. Let’s go through the scenario since it’s easier to understand the functionality that way.&lt;/p&gt; &lt;p&gt;I have users John, Jane and Administrator. John wants to contact Jane but doesn’t know how to contact her so he sends out email to the administrator:&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;img border="0" alt="EmailToAdmin" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/EmailToAdmin_ca6d9d05-4ffa-42ea-90ac-ff69a17edb17.png" width="403" height="411"&gt; &lt;/p&gt; &lt;p&gt;Administrator receives mail from John:&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="EmailFromJohn" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/EmailFromJohn_9af3492b-0477-4c1d-974f-a17354bf5217.png" width="707" height="267"&gt;&lt;/p&gt; &lt;p&gt;Since Administrator knows Jane’s home phone he can contact her in urgent matters and get her message back to John. In this case Administrator just presses Reply to John’s email and writes the message as Jane dictates it over the phone:&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="EmailResponseToJohn" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/EmailResponseToJohn_12f3cbab-69ef-44cf-aecd-fc027cf5a65b.png" width="414" height="527"&gt; &lt;/p&gt; &lt;p&gt;John receives email that appears to be written by Jane:&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="EmailResponseAtJohnsOutlook" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/EmailResponseAtJohnsOutlook_caddf6bd-64d8-41b1-bdd8-69c5a51720bc.png" width="326" height="214"&gt; &lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Okay that was our scenario and now let’s see how it was created. &lt;/p&gt; &lt;p&gt;Step 1: Well this is pretty obviously :-)&lt;/p&gt; &lt;p&gt;&lt;img border="0" alt="Addin1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Addin1_1ee85521-ed94-495c-bd7e-371019570cb7.png" width="543" height="308"&gt; &lt;/p&gt; &lt;p&gt;Step 2: Write following code (and as always.. &lt;u&gt;use this at your own risk&lt;/u&gt; :-). This should be extended with better error handling, sanity checks etc.):&lt;/p&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#0000ff"&gt;using&lt;/font&gt; Microsoft.Office.Interop.Outlook;
&lt;font color="#0000ff"&gt;using&lt;/font&gt; System.Windows.Forms;

&lt;font color="#0000ff"&gt;namespace&lt;/font&gt; My_Outlook_2007_Add_in
{
  &lt;font color="#0000ff"&gt;public&lt;/font&gt; &lt;font color="#0000ff"&gt;partial&lt;/font&gt; &lt;font color="#0000ff"&gt;class&lt;/font&gt; &lt;font color="#2b91af"&gt;ThisAddIn
&lt;/font&gt;  {
    &lt;font color="#2b91af"&gt;MailItem&lt;/font&gt; _mailItem = &lt;font color="#0000ff"&gt;null&lt;/font&gt;;
    &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; ThisAddIn_Startup(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, System.&lt;font color="#2b91af"&gt;EventArgs&lt;/font&gt; e)
    {
      &lt;font color="#0000ff"&gt;this&lt;/font&gt;.Application.ItemLoad += &lt;font color="#0000ff"&gt;new
&lt;/font&gt;        &lt;font color="#2b91af"&gt;ApplicationEvents_11_ItemLoadEventHandler&lt;/font&gt;(Application_ItemLoad);
    }

    &lt;font color="#0000ff"&gt;void&lt;/font&gt; Application_ItemLoad(&lt;font color="#0000ff"&gt;object&lt;/font&gt; Item)
    {
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (Item &lt;font color="#0000ff"&gt;is&lt;/font&gt; &lt;font color="#2b91af"&gt;MailItem&lt;/font&gt;)
      {
        &lt;font color="#0000ff"&gt;try
&lt;/font&gt;        {
          &lt;font color="#2b91af"&gt;MailItem&lt;/font&gt; mailItem = Item &lt;font color="#0000ff"&gt;as&lt;/font&gt; &lt;font color="#2b91af"&gt;MailItem&lt;/font&gt;;
          mailItem.Open += &lt;font color="#0000ff"&gt;new&lt;/font&gt; 
            &lt;font color="#2b91af"&gt;ItemEvents_10_OpenEventHandler&lt;/font&gt;(mailItem_Open);
          _mailItem = mailItem;
        }
        &lt;font color="#0000ff"&gt;catch&lt;/font&gt; (System.&lt;font color="#2b91af"&gt;Exception&lt;/font&gt; ex)
        {
          &lt;font color="#2b91af"&gt;MessageBox&lt;/font&gt;.Show(ex.ToString(), 
            &lt;font color="#a31515"&gt;"Exception"&lt;/font&gt;, 
            &lt;font color="#2b91af"&gt;MessageBoxButtons&lt;/font&gt;.OK, &lt;font color="#2b91af"&gt;MessageBoxIcon&lt;/font&gt;.Error);
        }
      }
    }

    &lt;font color="#0000ff"&gt;void&lt;/font&gt; mailItem_Open(&lt;font color="#0000ff"&gt;ref&lt;/font&gt; &lt;font color="#0000ff"&gt;bool&lt;/font&gt; Cancel)
    {
      &lt;font color="#0000ff"&gt;if&lt;/font&gt; (_mailItem.Subject.Contains(&lt;font color="#a31515"&gt;"Jane"&lt;/font&gt;) == &lt;font color="#0000ff"&gt;true&lt;/font&gt;)
      {
        &lt;font color="#008000"&gt;// This requires "Send As" privileges at the AD:
&lt;/font&gt;        _mailItem.SentOnBehalfOfName = &lt;font color="#a31515"&gt;"&lt;/font&gt;&lt;font color="#a31515"&gt;jane@contoso.com&lt;/font&gt;&lt;font color="#a31515"&gt;"&lt;/font&gt;;
      }
    }

    &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; ThisAddIn_Shutdown(&lt;font color="#0000ff"&gt;object&lt;/font&gt; sender, System.&lt;font color="#2b91af"&gt;EventArgs&lt;/font&gt; e)
    {
    }

&lt;font color="#0000ff"&gt;    #region&lt;/font&gt; VSTO generated code
    &lt;font color="#808080"&gt;///&lt;/font&gt;&lt;font color="#008000"&gt; &lt;/font&gt;&lt;font color="#808080"&gt;&amp;lt;summary&amp;gt;
&lt;/font&gt;    &lt;font color="#808080"&gt;///&lt;/font&gt;&lt;font color="#008000"&gt; Required method for Designer support - do not modify
&lt;/font&gt;    &lt;font color="#808080"&gt;///&lt;/font&gt;&lt;font color="#008000"&gt; the contents of this method with the code editor.
&lt;/font&gt;    &lt;font color="#808080"&gt;///&lt;/font&gt;&lt;font color="#008000"&gt; &lt;/font&gt;&lt;font color="#808080"&gt;&amp;lt;/summary&amp;gt;
&lt;/font&gt;    &lt;font color="#0000ff"&gt;private&lt;/font&gt; &lt;font color="#0000ff"&gt;void&lt;/font&gt; InternalStartup()
    {
      &lt;font color="#0000ff"&gt;this&lt;/font&gt;.Startup += &lt;font color="#0000ff"&gt;new&lt;/font&gt; System.&lt;font color="#2b91af"&gt;EventHandler&lt;/font&gt;(ThisAddIn_Startup);
      &lt;font color="#0000ff"&gt;this&lt;/font&gt;.Shutdown += &lt;font color="#0000ff"&gt;new&lt;/font&gt; System.&lt;font color="#2b91af"&gt;EventHandler&lt;/font&gt;(ThisAddIn_Shutdown);
    }
    
&lt;font color="#0000ff"&gt;    #endregion
&lt;/font&gt;  }
}&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;Code works so that we first trap events from &lt;em&gt;ItemLoad&lt;/em&gt; (on line 11). In &lt;em&gt;Application_ItemLoad&lt;/em&gt; we check that is this current item &lt;em&gt;MailItem&lt;/em&gt; and if it is we then hook &lt;em&gt;Open&lt;/em&gt; event of that Item. And when mail item is opened we’ll check that does that contain &lt;em&gt;Jane &lt;/em&gt;at the subject. And if it does then we set the &lt;em&gt;SentOnBehalfOfName&lt;/em&gt; property for the mail item. &lt;u&gt;You need to modify contents between lines 37 and 41 to make this work in real life&lt;/u&gt;. I just made this as simple as possible. So probably adding few more checks isn’t bad idea :-)&lt;/p&gt;
&lt;p&gt;Step 3: Build, Test and when everything is working then Publish:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="Publish0" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish0_f42db157-d347-4a40-80db-ec2d0fc1cfa4.png" width="301" height="191"&gt; &lt;/p&gt;
&lt;p&gt;Publishing wizard asks the location for the published files:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish1_2.png" target="_blank"&gt;&lt;img border="0" alt="Publish1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish1_thumb.png" width="372" height="332"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish2_2.png" target="_blank"&gt;&lt;img border="0" alt="Publish2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish2_thumb.png" width="372" height="332"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish3_2.png" target="_blank"&gt;&lt;img border="0" alt="Publish3" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Publish3_thumb.png" width="372" height="327"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;After that published location contains following files:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Folder1_2.png" target="_blank"&gt;&lt;img border="0" alt="Folder1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Folder1_thumb.png" width="533" height="170"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;em&gt;Setup.exe &lt;/em&gt;installs the add-in. &lt;em&gt;Application Files&lt;/em&gt; folder contains subfolder for each version of the add-in (since it will be published to same location on update). You can actually control the check for new versions from VS (by default add-in checks for update once per week):&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="VS" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/VS_3.png" width="505" height="348"&gt; &lt;/p&gt;
&lt;p&gt;&lt;br&gt;Now you can install the add-in for all the necassary users. Setup also knows about prerequisites (.NET framework 3.5 and VSTO 3.0 Runtime) and knows how to get them during install:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/PreReq1_2.png" target="_blank"&gt;&lt;img border="0" alt="PreReq1" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/PreReq1_thumb.png" width="302" height="357"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/PreReq2_2.png"&gt;&lt;img border="0" alt="PreReq2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/PreReq2_thumb.png" width="302" height="360"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;And when prereqs are ok we’re ready to install our app (this screenshot is from Windows Server 2003):&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Folder2_3.png" target="_blank"&gt;&lt;img border="0" alt="Folder2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Folder2_thumb.png" width="404" height="203"&gt;&lt;/a&gt; &lt;/p&gt;
&lt;p&gt;After the installation add-in can be seen in Add Remove Programs (and removed if needed):&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="AddRemove" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/AddRemove_047d2bf4-89ab-4f8c-8964-f0df9a338fc3.png" width="348" height="135"&gt; &lt;/p&gt;
&lt;p&gt;Now your Outlook will work as described at the beginning of the post. &lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Hmm… you might start wondering that isn’t there few security issues in this approach? How can administrator act as Jane and how that isn’t displayed at his Outlook? Well by default security forbids this kind of scenario but if this kind of approach is needed then you need to give permission to the users at the AD (&lt;strong&gt;Important note:&lt;em&gt; &lt;/em&gt;&lt;/strong&gt;I gave &lt;em&gt;Everyone&lt;/em&gt; permissions to “&lt;em&gt;Send As”&lt;/em&gt; but that is not the way to go in real life):&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="AD" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/AD_ec3ccefa-ec5b-402f-aa6a-d47558e7ed84.png" width="482" height="556"&gt; &lt;/p&gt;
&lt;p&gt;And what does Outlook do then? In previous screenshots Outlook didn’t notify administrator about the &lt;em&gt;SentOnBehalfOfName&lt;/em&gt; property change. However if you add more email addresses to your email then Outlook refreshes the view to this:&lt;/p&gt;
&lt;p&gt;&lt;img border="0" alt="Outlook" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/SimpleOutlookAddinwithVSTO3.0_10CBD/Outlook_68e46237-e636-4c26-8be1-4dc7e00b1251.png" width="313" height="427"&gt; &lt;/p&gt;
&lt;p&gt;So Outlook added &lt;em&gt;From…&lt;/em&gt; field and it now clearly indicates that this message will be send as &lt;em&gt;Jane&lt;/em&gt;. Outlook just makes sure that user is aware of the senders change. Obviously our add-in could inform the end user with some kind of tooltip or something like that but I’ll leave those details to you.&lt;/p&gt;
&lt;p&gt;Well here was small example of Outlook add-in. I’ll probably come back to add-ins in near future so stay tuned… &lt;br&gt;&lt;br&gt;Anyways… Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8363721" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/2007+Office+system/default.aspx">2007 Office system</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item><item><title>Comparing two databases (schema and/or data)</title><link>http://blogs.msdn.com/jannemattila/archive/2008/02/13/comparing-two-databases-schema-and-or-data.aspx</link><pubDate>Wed, 13 Feb 2008 22:28:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:7679505</guid><dc:creator>jannemattila</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/jannemattila/comments/7679505.aspx</comments><wfw:commentRss>http://blogs.msdn.com/jannemattila/commentrss.aspx?PostID=7679505</wfw:commentRss><description>&lt;p&gt;Quite often people like to know what's happening under the covers when they do something through user interface. For example they use CRM, SharePoint or some other product through user interface and they would like to know what has happened at the database. Normally my answer is that you don't need to know that because "&lt;em&gt;Don't touch the database rule still applies&lt;/em&gt;" but this time I have different answer :-) &lt;/p&gt; &lt;p&gt;I'll shortly explain how can you check that stuff yourself using Visual Studio 2008 and &lt;em&gt;Schema compare&lt;/em&gt; and/or &lt;em&gt;Data compare&lt;/em&gt; functionalities. But it's important to understand that this kind of approach shouldn't be ever executed against production databases. So you really need to have separate dev environment (and databases) for this kind of testing.&lt;/p&gt; &lt;p&gt;Now we're ready to go. I'll use Microsoft CRM 4.0 in my example. I have created two tenants and named them &lt;em&gt;Demo&lt;/em&gt; and &lt;em&gt;DemoEmpty&lt;/em&gt;. And that of course means that I have databases &lt;em&gt;Demo_MSCRM&lt;/em&gt; and &lt;em&gt;DemoEmpty_MSCRM&lt;/em&gt; at my SQL Server. If I now start up my Visual Studio 2008 and connect those two databases to the server explorer and start playing around with this (&lt;em&gt;Data -&amp;gt; Schema Compare -&amp;gt; New Schema Comparison...&lt;/em&gt;):&lt;/p&gt; &lt;p&gt;&lt;img height="209" alt="VSMenuSchemaCompare" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare_b26b5932-3e44-4471-99ce-8254b0dd4146.png" width="617" border="0"&gt; &lt;/p&gt; &lt;p&gt;Then I get this dialog and select those two databases (&lt;strong&gt;NOTE&lt;em&gt;: &lt;/em&gt;Right hand side is the target database!&lt;/strong&gt;):&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare2_2.png" target="_blank"&gt;&lt;img height="291" alt="VSMenuSchemaCompare2" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare2_thumb.png" width="640" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;And when I click the &lt;em&gt;OK&lt;/em&gt;-button the Visual Studio starts crawling the two databases and then creates list of differences:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare3_2.png" target="_blank"&gt;&lt;img height="148" alt="VSMenuSchemaCompare3" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare3_thumb.png" width="640" border="0"&gt;&lt;/a&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;Currently we're not yet interested at the differences since we just want to make the databases equal. So I just pressed &lt;em&gt;Write updates&lt;/em&gt; button from the toolbar to make the &lt;em&gt;DemoEmpty_MSCRM&lt;/em&gt; same as the &lt;em&gt;Demo_MSCRM &lt;/em&gt;(remember &lt;em&gt;DemoEmpty&lt;/em&gt; was the target database). Obviously this makes my &lt;em&gt;DemoEmpty&lt;/em&gt; database useless but I use it only to track changes at the &lt;em&gt;Demo&lt;/em&gt; database. &lt;/p&gt; &lt;p&gt;I also used &lt;em&gt;Data Compare -&amp;gt; New Data Comparison...&lt;/em&gt; so that both databases would then have same content (of course you can achieve this same with backup/restore but it's not as fun as this approach!):&lt;/p&gt; &lt;p&gt;&lt;img height="149" alt="VSDataComparepng" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSDataComparepng_dd13296d-c796-4bda-8c93-255a1a3205fe.png" width="400" border="0"&gt; &lt;/p&gt; &lt;p&gt;Now if I refresh the &lt;em&gt;Schema compare&lt;/em&gt; we'll get this view:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSSchemaCompare_2.png" target="_blank"&gt;&lt;img height="366" alt="VSSchemaCompare" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSSchemaCompare_thumb.png" width="640" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;Or closer view of the &lt;em&gt;schema compare&lt;/em&gt;:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare4_3.png" target="_blank"&gt;&lt;img height="399" alt="VSMenuSchemaCompare4" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSMenuSchemaCompare4_thumb.png" width="640" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;We can easily see that our database schemas are equal since the &lt;em&gt;Status &lt;/em&gt;is &lt;em&gt;Equal &lt;/em&gt;for the tables and &lt;em&gt;Update Action &lt;/em&gt;is set to &lt;em&gt;Skip&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;And now we're finally ready to go to the user interface for the tenant &lt;em&gt;Demo&lt;/em&gt; and make some changes. I'll type &lt;em&gt;&lt;u&gt;http://crmserver/Demo&lt;/u&gt;&lt;/em&gt; into my browser and go to the &lt;em&gt;Settings &lt;/em&gt;and &lt;em&gt;Customization -&amp;gt; Customize Entities&lt;/em&gt;. And I'll just add new custom entity called &lt;em&gt;MyDemo&lt;/em&gt; (how original name!) and it makes my entity name &lt;em&gt;new_demo&lt;/em&gt; (since I didn't even change the default prefix... and this is just lazyness I know!):&lt;/p&gt; &lt;p&gt;&lt;img height="246" alt="CRMMyDemoEntity" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/CRMMyDemoEntity_8fa9bd37-68a2-45f7-86d3-0aac435aa6c0.png" width="670" border="0"&gt; &lt;/p&gt; &lt;p&gt;After I have saved my new entity I'm ready to re-run the &lt;em&gt;Schema compare&lt;/em&gt;:&lt;/p&gt; &lt;p&gt;&lt;a href="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSNewEntitySchemaChange_2.png" target="_blank"&gt;&lt;img height="590" alt="VSNewEntitySchemaChange" src="http://blogs.msdn.com/blogfiles/jannemattila/WindowsLiveWriter/Comparingdatabaseschemaandordata_B87C/VSNewEntitySchemaChange_thumb.png" width="679" border="0"&gt;&lt;/a&gt; &lt;/p&gt; &lt;p&gt;And of course the changes we're something that you could expect... two new tables called &lt;em&gt;New_demoBase&lt;/em&gt; and &lt;em&gt;New_demoExtensionBase&lt;/em&gt;. If you look at the definitions:&lt;/p&gt; &lt;p&gt;&lt;em&gt;New_demoBase&lt;/em&gt;:&lt;/p&gt; &lt;table cellspacing="10"&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#008000"&gt;-- Columns
&lt;/font&gt;&lt;font color="#0000ff"&gt;CREATE TABLE &lt;/font&gt;[dbo].[New_demoBase]
(
[New_demoId] [uniqueidentifier] &lt;font color="#0000ff"&gt;NOT NULL&lt;/font&gt;,
[CreatedOn] [datetime] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[CreatedBy] [uniqueidentifier] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[ModifiedOn] [datetime] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[ModifiedBy] [uniqueidentifier] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[OwningUser] [uniqueidentifier] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[OwningBusinessUnit] [uniqueidentifier] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[statecode] [int] &lt;font color="#0000ff"&gt;NOT NULL&lt;/font&gt;,
[statuscode] [int] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[DeletionStateCode] [int] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[VersionNumber] [timestamp] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[ImportSequenceNumber] [int] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[OverriddenCreatedOn] [datetime] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[TimeZoneRuleVersionNumber] [int] &lt;font color="#0000ff"&gt;NULL&lt;/font&gt;,
[UTCConversionTimeZoneCode] [int] &lt;font color="#0000ff"&gt;NULL
&lt;/font&gt;) &lt;font color="#0000ff"&gt;ON &lt;/font&gt;[PRIMARY]
&lt;font color="#008000"&gt;-- ...&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;&lt;em&gt;New_demoExtensionBase&lt;/em&gt;:&lt;/p&gt;
&lt;table cellspacing="10"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top" align="right"&gt;&lt;pre&gt;&lt;font color="gray"&gt;1
2
3
4
5
6
7&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td valign="top"&gt;&lt;pre&gt;&lt;font color="#008000"&gt;-- Columns
&lt;/font&gt;&lt;font color="#0000ff"&gt;CREATE TABLE &lt;/font&gt;[dbo].[New_demoExtensionBase]
(
[New_demoId] [uniqueidentifier] &lt;font color="#0000ff"&gt;NOT NULL&lt;/font&gt;,
[New_name] [nvarchar] (100) &lt;font color="#0000ff"&gt;COLLATE &lt;/font&gt;Latin1_General_CI_AI &lt;font color="#0000ff"&gt;NULL
&lt;/font&gt;) &lt;font color="#0000ff"&gt;ON &lt;/font&gt;[PRIMARY]&lt;br&gt;&lt;font color="#008000"&gt;-- ...&lt;/font&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;p&gt;You'll understand that actually only those base CRM columns are at the &lt;em&gt;New_demoBase&lt;/em&gt; table and all the other (1 in my case since I was lazy and didn't add more fields :-) fields are at the &lt;em&gt;New_demoExtensionBase&lt;/em&gt; table. Of course there are other important changes too like the two new views: &lt;em&gt;FilteredNew_demo&lt;/em&gt; and &lt;em&gt;New_demo&lt;/em&gt;. &lt;em&gt;FilteredNew_demo &lt;/em&gt;is view that uses the users rights to retrieve data... so it automatically filters out all the rows that user doesn't have access to (that's why it's called &lt;em&gt;Filtered&lt;/em&gt; :-). And the other view just combines the two tables (&lt;em&gt;Base&lt;/em&gt; + &lt;em&gt;ExtensionBase&lt;/em&gt;) together.&lt;/p&gt;
&lt;p&gt;But this was just small example how you could compare databases and see what is happening at your application. Remember to use this compare tool carefully... because you could easily make you target database useless... so it would be wise to create backup before playing around with it.&lt;br&gt;&lt;br&gt;Anyways... Happy hacking!&lt;br&gt;&lt;br&gt;J&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=7679505" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/jannemattila/archive/tags/tips+and+tricks/default.aspx">tips and tricks</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Microsoft+CRM/default.aspx">Microsoft CRM</category><category domain="http://blogs.msdn.com/jannemattila/archive/tags/Visual+Studio/default.aspx">Visual Studio</category></item></channel></rss>