Monthly Archives: July 2011

Getting : The handshake failed due to an unexpected packet format exception while accessing Microsoft SharePoint 2010 from ASP.NET 4.0 application

Yesterday I encountered strange thing when accessing SharePoint 2010.

What I had :

I created some time ago ASP.NET application for uploading file to SharePoint 2007. Our company upgraded the SharePoint to latest 2010, so I had to update my tool also. It used ASP.NET 3.5. Internally we are using Forefront and I have Forefront client installed on my dev box. Everything worked fine. (I’m not sure, if the Forefront client is installed also the server, but I suppose so).

What was the problem :

After migration to ASP.NET 4.0, my code wasn’t working. When calling Lists.UpdateListItems on web service generated proxy class, I got nice System.Net.WebException The underlying connection was closed: An unexpected error occurred on a send exception with underling The handshake failed due to an unexpected packet format exception. So I created small console app and the code was working again (I used same library in the ASP.NET app and also in console app). But the ASP.NET app wasn’t.

Exception looked like this :

system.net.webexception

Also as I saw, it had to do something with SSL certificates.

Desperate, I fired up Fiddler, my tool of choice for HTTP traffic debugging and ASP.NET page started to work without any code change. Then it hit me : proxy problem.

The solution :

The Forefront client is set so, it will direct any app to either use our proxy server, or go directly to the server inside of our organization. In my case the ASP.NET app tried to use proxy settings and called the Sharepoint server via proxy and I would say, something messed up the SSL certificates. Anyway, you just have to say to your app, that it shouldn’t use proxy and this can be done in code, or in web.config (respectively app.config). I used this approach. How to set proxy in web.cofign file can be found here : http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx

Enjoy. 🙂

How to turn on developer errors in Microsoft CRM 2011 (aka Titan or 5.0)

Developing for Microsoft CRM can be a big pain sometimes. Often, there is an error on the server, but there is only generic note for normal (understand user) customer. We as devs need stack trace and other fancy things and bells and whistles. I remember, that Microsoft CRM 4.0 had this possibility and it could be done via some 3rd party tool that another developer created and for me it was just checking a checkbox.

Moving to CRM 2011 I had feeling, that there must be a way to turn this thing on again (and turn off in production indeed).

So, if you want to turn developer erros on your dev CRM 2011 , you should go to the web.config and search for this text :

<!–
Show Developer Errors
========================================================================================
If a server-side error occurs, this setting determines whether or not Debug error
messages are rendered to the user.  Debug error message are not recommended for
production environments, but may be useful in attempting to debug a problem you are
having with MSCRM.
Recommended Setting: “Off”
Values: “On” or “Off”
–>
<add key=”DevErrors” value=”On” />

Setting On and Off will change your web.config file and thus restarting the whole app, but you have a sweet reward. Anytime there will be crash, you will see something like this :
detailed error information crm 2011

On the left you can see big icons with some additional information :

  • Detailed information on error that occured,
  • Original ASP.NET error information (famous ASP.NET yellow screen of the death),
  • Debug information for this server,
  • Generic error that the user would see,
  • Button that will copy some error information to the clipboard.

So, hope this helps. 🙂

Enjoy.

How to enable and disable entities in Microsoft CRM 2011

The problem :

In my current project I need to enable and disable some entities, and because there is no direct method on generated proxy class (via crmsvcutil.exe), I created small helper with help from CRM SDK. Particular chunk of code that i’ve used, can be found here : http://www.resultondemand.nl/support/sdk/aad1f053-c775-4825-9d13-4925ee10aeec.htm or here http://msdn.microsoft.com/en-us/library/gg334435.aspx.

The code :

Following is the snippet (method is static as you see, I placed it in static class CRMHelpers and call it as neccessary) :

         /// <summary>
        /// Sets the state of the entity.
        /// </summary>
        /// <param name="serviceProxy">The service proxy.</param>
        /// <param name="EntityId">The entity id.</param>
        /// <param name="EntityLogicalName">Name of the entity logical.</param>
        /// <param name="StateValue">The state value.</param>
        /// <param name="StatusValue">The status value.</param>
        static public void SetEntityState(IOrganizationService serviceProxy, Guid EntityId, string EntityLogicalName, int StateValue, int StatusValue) {
            SetStateRequest setStateRequest = new SetStateRequest() {
                EntityMoniker = new EntityReference {
                    Id = EntityId,
                    LogicalName = EntityLogicalName
                },
                State = new OptionSetValue(StateValue),
                Status = new OptionSetValue(StatusValue)
            };
            serviceProxy.Execute(setStateRequest);
        }

You can download the source code here :

CRMHelpers.zip

How to use the code :

Following is the snippet in one of my classes where I use the helper code.

	/// <summary>
	/// Opens the opportunity.
	/// </summary>
	/// <param name="entity">The entity.</param>
	protected void OpenOpportunity(Opportunity entity) {
		CRMHelpers.SetEntityState(crmService, entity.Id, Opportunity.EntityLogicalName, (int)OpportunityState.Open, -1);
	}

There are two values, that you supply to the SetEntityState : StateValue and StatusValue. These make a pair and if you supply unavailable pair, you will get an exception. You can obtain valid pair from existing entity, or you can use this sample : Retrieving State and Status Options via the MetadataService that worked with CRM 4.0. I haven’t tried it on CRM 2011 (aka 5.0). As you can see I’m using cast from Enum that is generated via crmsvcutil.exe utility from SDK for StateValue and hard coded value for StatusValue.

Additional stuff :

In addition there are some other related tasks, that I found on the internet and you may be interested in :

How to enable and disable whole organization in CRM 2011

General discussion about enabling/disabling entities in CRM2011

Sample code to activate / deactivate record in CRM 2011

 

In case of any questions, ask them in the comments.

Enjoy.