I received this error after switching from the CRM 2013 SDK dlls to the new CRM 2015 SDK .Dll’s.

The type or namespace name ‘Xrm’ does not exist in the namespace ‘Microsoft’ (are you missing an assembly reference?)

Which led me to this StackOverflow question; “Can’t access Microsoft.Xrm namespace” which seems very similar but does not solve my problem.

What eventually led me to a solution was a warning listed further down in the list of errors. It seems that Microsoft with the new SDK has switched to .Net 4.5.2 so the whole problem for me was that I needed to install a later version of .Net (minimum 4.5.2) and then switch target frameworks to this.

This is the warning that led me to this solution:

The primary reference “Microsoft.Xrm.Client, Version=5.0.9689.2166, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL” could not be resolved because it was built against the “.NETFramework,Version=v4.5.2″ framework. This is a higher version than the currently targeted framework “.NETFramework,Version=v4.5.1″.

So the solution turned out to be as simple as getting the developper pack of .Net 4.5.2. and switching target frameworks on my project to this in visual studio.

SmallImage


I’ve recently been playing with Unity quite a bit and got stuck on how to add event listeners to buttons that I made in the visual designer programatically and not through the ui interface. This Code adds an onClick event to any button in the scene, and if any of them are clicked the method ButtonClick is called with the button as a paramater.

In my opinion, this is much nicer than using the messy graphical interface to set it up, and also keeps all the logic in the main game class.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameCode: MonoBehaviour
{
   void Start ()
   {
      var allUIElements = UnityEngine.UI.Button.allSelectables.ToArray ();
      foreach (var uiItem in allUIElements)
      {
         if(uiItem.GetType() == (typeof(Button)))
         {
            Button uiButton = (Button) uiItem;
            uiButton.onClick.RemoveAllListeners();
            uiButton.onClick.AddListener(() => ButtonClick(uiButton));
         }
      }
   }

   public void ButtonClick(Button clickedButton)
   {
      Debug.Log ("clicked: " + clickedButton.name);
     }
   }
}

 


I’ve been working on a game in Unity for a few weeks and I have been using the WWW object to make POST requests. Problem is that the WWW blocks the rendering thread so long running queries can make the game lag and stutter (alot).

To try to fix this I wanted to make a separate thread for all my network calls but since Unity is not thread safe I could not use the WWW request any more.

So I remade the request using the WebClient class, which was working great up until I tried it on windows phone. It appears this is not supported on WP. The reason for this is that twindows phone uses a slimmed down version of .net where as iOS and Android compile the code using .net mono.

Windows Phone 8 uses a different flavor of .NET called .NET for Windows Phone which is missing some of the types available on other platforms. You’ll have to either replace these types with different ones or implement them yourself.

- http://docs.unity3d.com/Manual/wp8-faq.html

If you try to use these methods on the WP8 you get (annoying) messages like these:

Error: method System.Byte[] System.Net.WebClient::UploadData(System.String,System.String,System.Byte[])` doesn’t exist in target framework.

Error: method System.IO.Stream System.Net.HttpWebRequest::GetRequestStream() doesn’t exist in target framework.

Error: method System.Net.WebResponse System.Net.HttpWebRequest::GetResponse() doesn’t exist in target framework.

So after a bit of searching (and alot of trial and error) I arrived at the below code, which works on all platforms.

This is using WebRequests, according to the API for windows phone only certain methods are available so I could only use the async methods, but I’ve written the code so that it appears to be synchronous to the calling method which is what I wanted for my application (as I am calling it from a separate “networking thread” and not in the main thread).

/// <summary>
/// Make post request to url with given paramaters
/// </summary>
/// <param name="url">URL to post data to http://server.com/method </param>
/// <param name="data">{ Data: data }</param>
/// <returns>string server response</returns>
public string PostData(string url, string data)
{
    // json request, hard coded right now but use "data" paramater to set this value.
    string jsonRequest = "{\"Data\": \"data\"}"; // the json request
 
    var request = System.Net.WebRequest.Create(url) as System.Net.HttpWebRequest;
 
    // this could be different for your server
    request.ContentType = "application/json";
 
    // i want to do post and not get
    request.Method = "POST";
 
    // used to check if async call is complete
    bool isRequestCallComplete = false;
 
    // store the response in this
    string responseString = string.Empty;
 
    request.BeginGetRequestStream(ar =>
    {
        var requestStream = request.EndGetRequestStream(ar);
        using (var sw = new System.IO.StreamWriter(requestStream))
        {
            // write the request data to the server
            sw.Write(jsonRequest);
 
            // force write of all content
            sw.Flush();
        }
 
        request.BeginGetResponse(a =>
        {
            var response = request.EndGetResponse(a);
            var responseStream = response.GetResponseStream();
            using (var sr = new System.IO.StreamReader(responseStream))
            {
                // read in the servers response right here.
                responseString = sr.ReadToEnd();
            }
            // set this to true so the while loop at the end stops looping.
            isRequestCallComplete = true;
        }, null);
 
    }, null);
 
    // wait for request to complete before continuing
    // probably want to add some sort of time out to this
    // so that the request is stopped after X seconds.
    while (isRequestCallComplete == false) { Thread.Sleep(50); }
 
    return responseString;
}

 

 

Many times when you are working with CRM it is usefull to be able to get the GUID of a record. It certainly makes my life alot easier when I can tell a coworker exactly which record I'm talking about, or when someone is testing they can give me the guid of the record that does not work.

There is a really simple way of getting the guid using a bookmark in your browser. Heres how to do it:

  1. Create a bookmark in your favorite browser.
  2. Name it "Show CRM GUID"
  3. Edit the properties, and paste the below javascript into the URL field.
javascript:!function(){var a=/id=([^&]+)/.exec(document.URL)[1],b=a?decodeURIComponent(a):"Could not find guid";window.prompt("Record ID:",b)}();

Now when you want the GUID of a crm record do this:

  1. Navigate to the record in CRM.
  2. Open it in a new window using the button in the top right.
  3. Click on the bookmark button you created above.

You should now get a popup with the GUID which looks something like this:

SmallImage

Also, if your interested, here is the unminified version of the javascript code.

javascript:(function() {
   var captured = /id=([^&]+)/.exec(document.URL)[1]; 
   var result = captured ? decodeURIComponent(captured) : 'Could not find guid';
   window.prompt('Record ID:',result);
})();

If your are still using CRM 2011 use this code instead:

javascript:(function() {var url=document.URL;var guid = url.search(/id(%)3d(%)257b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-Z0-9]{12}/);window.prompt('Record ID:',url.substring(guid+10, guid+46))})();

Unminified: 

javascript:(function() {
  var url=document.URL;
  var guid = url.search(/id(%)3d(%)257b[A-F0-9]{8}(?:-[A-F0-9]{4}){3}-[A-Z0-9]{12}/);
  window.prompt('Record ID:',url.substring(guid+10, guid+46))
})();

Functionality such as Split() is for the most of us something we take for granted in whatever programming language we are using.
There is however no Split method in MSSQL. So when I was faced with the problem of splitting strings into multiple rows it got a bit tricky.
I ended up with the function below which will take two arguments, the string to work with, and the separator to split on.

CREATE FUNCTION [dbo].[CustomSplit] (@sep char(1), @s varchar(512))
RETURNS table
AS
RETURN (
   WITH Pieces(n, start, stop) AS (
      SELECT 1, 1, CHARINDEX(@sep, @s)
      UNION ALL
      SELECT n + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)
      FROM Pieces
      WHERE stop > 0
   )
   SELECT n,
   SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 512 END) AS s
   FROM Pieces
)

 
Here is an example of what this code will do:

SELECT * FROM dbo.CustomSplit('x','aaaaaaxbbbbbbbbbxcccccccc')

SmallImage