• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5151
  • Last Modified:

CRM SDK 4.0 Microsoft.Crm.Sdk.Utility, the type or namespace utility does not exist in microsoft.crm.sdk

Hi,

I am using CRM SDK 4.0 to retrieve a contact from the CRM using Visual web developer 2005. I have added a reference to the web service "crmservice.asmx" and called it CRMSDK, as mentioned in the documentation. I have also added the required dlls and the crmserviceutility.cs to my project. I get this error "The type or namespace name 'Utility' does not exist in the namespace 'Microsoft.Crm.Sdk' (are you missing an assembly reference)". Any idea(s)? thanks in advance!
Please the code below:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrmSdk;
using Microsoft.Crm.Sdk.Utility; //THIS LINE THROWS THE ERROR

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Set up the CRM Service.
        CrmAuthenticationToken token = new CrmAuthenticationToken();
        // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
        token.AuthenticationType = 0;
        token.OrganizationName = "orgName";

        CrmService service = new CrmService();
        service.Url = "http://myServer/mscrmservices/2006/crmservice.asmx";
        
        service.CrmAuthenticationTokenValue = token;
        service.Credentials = System.Net.CredentialCache.DefaultCredentials;

        // Create the column set object that indicates the properties to be retrieved.
        ColumnSet cols = new ColumnSet();

        // Set the properties of the column set.
        cols.Attributes = new string[] { "fullname" };

        // contactGuid is the GUID of the record being retrieved.
        Guid contactGuid = new Guid("4D507FFE-ED25-447B-80DE-00AE3EB18B84");

        // Retrieve the contact.
        // The EntityName indicates the EntityType of the object being retrieved.
        contact contact = (contact)service.Retrieve(EntityName.contact.ToString(), contactGuid, cols);
    }
}
=================================================
Below is the built in code for the crmserviceutility.cs,

// =====================================================================
//  File:		CrmServiceUtility.cs
//  Summary:	TODO
// ============================================================
//
//  This file is part of the Microsoft CRM 4.0 SDK Code Samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// ==========================================================

using System;
using System.Collections.Generic;
using System.Text;
using CrmSdk;
using MetadataServiceSdk;

namespace Microsoft.Crm.Sdk.Utility
{
	public class CrmServiceUtility
	{
		public static CrmService GetCrmService()
		{
			return GetCrmService(null, null);
		}

		public static CrmService GetCrmService(string organizationName)
		{
			return GetCrmService(null, organizationName);
		}

		/// <summary>
		/// Set up the CRM Service.
		/// </summary>
		/// <param name="organizationName">My Organization</param>
		/// <returns>CrmService configured with AD Authentication</returns>
		public static CrmService GetCrmService(string crmServerUrl, string organizationName)
		{
			// Get the CRM Users appointments
			// Setup the Authentication Token
			CrmSdk.CrmAuthenticationToken token = new CrmSdk.CrmAuthenticationToken();
			token.OrganizationName = organizationName;
		
			CrmService service = new CrmService();

			if (crmServerUrl != null &&
				crmServerUrl.Length > 0)
			{
				UriBuilder builder = new UriBuilder(crmServerUrl);				
				builder.Path = "//MSCRMServices//2007//CrmService.asmx";
				service.Url = builder.Uri.ToString();
			}

			service.Credentials = System.Net.CredentialCache.DefaultCredentials;
			service.CrmAuthenticationTokenValue = token;

			return service;
		}

		/// <summary>
		/// Set up the CRM Metadata Service.
		/// </summary>
		/// <param name="organizationName">My Organization</param>
		/// <returns>MetadataService configured with AD Authentication</returns>
		public static MetadataService GetMetadataService(string crmServerUrl, string organizationName)
		{
			// Get the CRM Users appointments
			// Setup the Authentication Token
			MetadataServiceSdk.CrmAuthenticationToken token = new MetadataServiceSdk.CrmAuthenticationToken();
			token.OrganizationName = organizationName;

			MetadataService service = new MetadataService();

			if (crmServerUrl != null &&
				crmServerUrl.Length > 0)
			{
				UriBuilder builder = new UriBuilder(crmServerUrl);				
				builder.Path = "//MSCRMServices//2007//MetadataService.asmx";
				service.Url = builder.Uri.ToString();
			}
			
			service.Credentials = System.Net.CredentialCache.DefaultCredentials;
			service.CrmAuthenticationTokenValue = token;

			return service;
		}
		
		/// <summary>
		/// Create a Crm label
		/// </summary>
		/// <param name="label">string label value for LocLabel</param>
		/// <param name="langCode">Language Code for CrmLabel</param>
		/// <returns></returns>
		public static MetadataServiceSdk.CrmLabel CreateSingleLabel(string label, int langCode)
		{
			MetadataServiceSdk.CrmNumber crmNumber = new MetadataServiceSdk.CrmNumber();
			crmNumber.Value = langCode;

			MetadataServiceSdk.LocLabel locLabel = new MetadataServiceSdk.LocLabel();
			locLabel.LanguageCode = crmNumber;
			locLabel.Label = label;

			MetadataServiceSdk.CrmLabel crmLabel = new MetadataServiceSdk.CrmLabel();
			crmLabel.LocLabels = new MetadataServiceSdk.LocLabel[] { locLabel };

			return crmLabel;
		}
	}
}

Open in new window

0
arif_caesar
Asked:
arif_caesar
  • 16
  • 12
1 Solution
 
Feridun KadirPrincipal ConsultantCommented:
I don't think you should have line 11 at all.

Depending on what your code does you may need to a web reference to the metadataservice.

0
 
arif_caesarAuthor Commented:
Thaks for the prompt reply, feridun.
If I take off line 11, I get error at line 18, "The type or namespace name 'CrmAuthenticationToken'" could not be found (are u missing a directive or an assembly reference?)
I also get an error at line 26, "CrmSdk.CrmService does not contain a definition for 'CrmAuthenticaionTokenValue'".
I already have added a web reference to the metadataservice and when I use it in the code by calling it as "using MetadataServiceSdk;", I still get the same errors shown above.
0
 
Feridun KadirPrincipal ConsultantCommented:
I've had a brief look at the code. You seem to have two examples copied from the SDK that don't seem to fit together.  What is it that you are trying to do?

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
arif_caesarAuthor Commented:
I am trynig to retrieve a contact from CRM using the web service. I can get rid of crmserviceutility.cs but I came across one of the blogs where they advised of adding it in the project. Later on, i'll be working on a code to add a contact to CRM but for now it's just the retrieval...
0
 
Feridun KadirPrincipal ConsultantCommented:
Here is the code from the SDK to retrieve a contact. Note:
1) I'm using a console application
2) I've added a web reference to the Crm web service called CrmSdk
3) You need to update the service.Url line with the correct name for your CRM web service
4) This example returns details for one contact only and the GUID in the code almost certainly doesn't exist in your system so you will need to replace it with one that does - otherwise you get an error.

using System;
using System.Collections.Generic;
using System.Text;
using ConsoleApplication2.CrmSdk;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
            token.AuthenticationType = 0;
            token.OrganizationName = "AdventureWorksCycle";

            CrmService service = new CrmService();
            service.Url = "http://crm/mscrmservices/2007/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Create the column set object that indicates the properties to be retrieved.
            ColumnSet cols = new ColumnSet();

            // Set the properties of the column set.
            cols.Attributes = new string[] { "fullname" };

            // contactGuid is the GUID of the record being retrieved.
            Guid contactGuid = new Guid("4D507FFE-ED25-447B-80DE-00AE3EB18B84");

            // Retrieve the contact.
            // The EntityName indicates the EntityType of the object being retrieved.
            contact contact = (contact)service.Retrieve(EntityName.contact.ToString(), contactGuid, cols);
        }
    }
}

Open in new window

0
 
arif_caesarAuthor Commented:
Thank u ever so much for that, to create a console application, I need to get a visual c# developer or visual studio. let's c if i can manage to get one, m basically a classic ASP developer.
Have you by any chance got the code which allows you to create a contact in the CRM? cheers!
0
 
Feridun KadirPrincipal ConsultantCommented:
Sure. Again copying an example from the CRM SDK which you can download from here: http://www.microsoft.com/downloads/details.aspx?displaylang=en&FamilyID=82e632a7-faf9-41e0-8ec1-a2662aae9dfb




using System;
using System.Collections.Generic;
using System.Text;
using ConsoleApplication3.CrmSdk;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
            token.AuthenticationType = 0;
            token.OrganizationName = "AdventureWorksCycle";

            CrmService service = new CrmService();
            service.Url = "http://crm/mscrmservices/2007/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Create the contact object.
            contact contact = new contact();

            // Create the properties for the contact object.
            contact.firstname = "Jesper";
            contact.lastname = "Aaberg";
            contact.address1_line1 = "23 Market St.";
            contact.address1_city = "Sammamish";
            contact.address1_stateorprovince = "MT";
            contact.address1_postalcode = "99999";
            contact.donotbulkemail = new CrmBoolean();
            contact.donotbulkemail.Value = true;

            // Create the contact in Microsoft Dynamics CRM.
            Guid contactGuid = service.Create(contact);
        }
    }
}

Open in new window

0
 
arif_caesarAuthor Commented:
Thanks again! I have managed to get visual c# 2008 express edition and tried your code / steps for retrieving the contact from CRM using a console app. This time I have only added a web reference to the CrmService without adding the .dlls. Alas, I get these errors:
Error      1      The type or namespace name 'CrmAuthenticationToken' could not be found (are you missing a using directive or an assembly reference?)
Error      2      The type or namespace name 'CrmAuthenticationToken' could not be found (are you missing a using directive or an assembly reference?)
Error      3      The type or namespace name 'CrmService' could not be found (are you missing a using directive or an assembly reference?)
Error      4      The type or namespace name 'CrmService' could not be found (are you missing a using directive or an assembly reference?)
0
 
Feridun KadirPrincipal ConsultantCommented:
You don't need the dlls. Would you mind posting the exact code you have please?
0
 
arif_caesarAuthor Commented:
Here it is:

using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using ConsoleApplication1.CrmSdk; //the web reference was added using
              //http://myServer/mscrmservices/2006/crmservice.asmx?WSDL&uniquename=myOrgName

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the CRM Service.
            CrmAuthenticationToken token = new CrmAuthenticationToken(); //TWO ERRORS ARE THROWN
            // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
            token.AuthenticationType = 0;
            token.OrganizationName = "myOrgName";

            CrmService service = new CrmService();//TWO ERRORS ARE THROWN HERE
            service.Url = "http://myServer/mscrmservices/2006/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;

            // Create the column set object that indicates the properties to be retrieved.
            ColumnSet cols = new ColumnSet();

            // Set the properties of the column set.
            cols.Attributes = new string[] { "fullname" };

            // contactGuid is the GUID of the record being retrieved.
            Guid contactGuid = new Guid("4D507FFE-ED25-447B-80DE-00AE3EB18B84");

            // Retrieve the contact.
            // The EntityName indicates the EntityType of the object being retrieved.
            contact contact = (contact)service.Retrieve(EntityName.contact.ToString(), contactGuid, cols);
        }
    }
}
0
 
Feridun KadirPrincipal ConsultantCommented:
Replace ConsoleApplication1 with the name of your project

Also, why have you used 2006 instead of 2007 in the web services. 2006 refers to the CRM 3.0 web services.
0
 
arif_caesarAuthor Commented:
Oh I forgot to mention, I am using CRM 3.0. ConsoleApplication1 is the name of the project.
0
 
Feridun KadirPrincipal ConsultantCommented:
Hmm. In that case the authentication lines are different.

Replace these lines:

            CrmAuthenticationToken token = new CrmAuthenticationToken(); //TWO ERRORS ARE THROWN
            // You can use enums.cs from the SDK\Helpers folder to get the enumeration for Active Directory authentication.
            token.AuthenticationType = 0;
            token.OrganizationName = "myOrgName";

            CrmService service = new CrmService();//TWO ERRORS ARE THROWN HERE
            service.Url = "http://myServer/mscrmservices/2006/crmservice.asmx";
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;


with
            CrmService service = new CrmService();
            service.Url = "http://myServer/mscrmservices/2006/crmservice.asmx";
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;


Also, did you replace the word myServer with the name of your crm server?
0
 
arif_caesarAuthor Commented:
I have created another test console app project, named it myCS1 so now I am calling the web service as "using myCS1.CrmSdk;" which throws me an error saying "The type or namespace name 'myCS1' could not be found (are you missing a using directive or an assembly reference?)"
Yes, I am replacing the myServer with the actual crm server name.
0
 
arif_caesarAuthor Commented:
my mistake, the errors are
Error      1      The type or namespace name 'CrmSdk' does not exist in the namespace 'myCS1' (are you missing an assembly reference?)
Error      2      The type or namespace name 'CrmService' could not be found (are you missing a using directive or an assembly reference?)
Error      4      The type or namespace name 'ColumnSet' could not be found (are you missing a using directive or an assembly reference?)
Error      6      The type or namespace name 'contact' could not be found (are you missing a using directive or an assembly reference?)
Error      8      The name 'EntityName' does not exist in the current context





0
 
arif_caesarAuthor Commented:
Feridun, I have just download crm sdk 3.0. Do I have to install it first before I can use it? I only extracted files from it. I havn't installed anything from crm sdk 4 either.
0
 
Feridun KadirPrincipal ConsultantCommented:
No there is nothing to install. The main file is the .chm file that has the detailed help.  The other files are sample codes.

I'm stumped by your namespace problems. Like you did with myCS1 I've just created a new Visual C# console application and called the solution FKApp.

I added the crm web reference and then the following using directive:

using FKApp.CrmSdk;

Have you double-checked the case of CrmSdk in the web reference and the using statement because it is case-sensitive.
0
 
arif_caesarAuthor Commented:
I have managed to build the application without any exceptions by creating a web app, adding the reference for crmservice. Although I do get a runtime soapexception which is beyond my understanding of .Net. Here it is:

Line 262:            object[] results = this.Invoke("Retrieve", new object[] {

this error occurs at this line:
contact contact = (contact)service.Retrieve(EntityName.contact.ToString(), contactGuid, cols);
0
 
Feridun KadirPrincipal ConsultantCommented:
I suspect its what I mentioned earlier:

4) This example returns details for one contact only and the GUID in the code almost certainly doesn't exist in your system so you will need to replace it with one that does - otherwise you get an error.

0
 
arif_caesarAuthor Commented:
soapexception
screenShot1.bmp
0
 
arif_caesarAuthor Commented:
I did take off the dummy ID and replaced it with a functional one.
0
 
arif_caesarAuthor Commented:
FYI - I have taken the new ID from the ContactBase table of SQL Server, Is it the right place for it?
0
 
Feridun KadirPrincipal ConsultantCommented:
Yes that is ok. BUt I'm still willing to bet the ID is the problem. In the error message click View Details under Actions.
Expand to InnerText under Detail to see the error.


contact-error.PNG
0
 
arif_caesarAuthor Commented:
Perhaps u r ryt.....this is wat is says ""\n0x80040225\nThe specified user is either disabled or is not a member of any business unit.\nPlatform\n"", i'll be back in a jiff!
0
 
Feridun KadirPrincipal ConsultantCommented:
The errors is referring to the windows account that you are logged in as. Do you have an enabled CRM user account?
0
 
arif_caesarAuthor Commented:
I don't think if I have one yet as so far I have got the right only privileges to the SQL Server and can't access CRM.
0
 
Feridun KadirPrincipal ConsultantCommented:
That'll be the reason then. You can only work with the CRM web services if you have an enabled CRM account.
0
 
arif_caesarAuthor Commented:
thanks feridun, that was really helpul. I will be back with a few other questions once I get a CRM account and start working on the real time project.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 16
  • 12
Tackle projects and never again get stuck behind a technical roadblock.
Join Now