[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-05-19
28
Medium Priority
?
5,117 Views
Last Modified: 2013-11-07
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
Comment
Question by:arif_caesar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 16
  • 12
28 Comments
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797215
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
 

Author Comment

by:arif_caesar
ID: 32797274
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797329
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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 

Author Comment

by:arif_caesar
ID: 32797385
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
 
LVL 30

Accepted Solution

by:
Feridun Kadir earned 2000 total points
ID: 32797418
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
 

Author Comment

by:arif_caesar
ID: 32797452
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797471
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
 

Author Comment

by:arif_caesar
ID: 32797539
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797555
You don't need the dlls. Would you mind posting the exact code you have please?
0
 

Author Comment

by:arif_caesar
ID: 32797571
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797596
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
 

Author Comment

by:arif_caesar
ID: 32797616
Oh I forgot to mention, I am using CRM 3.0. ConsoleApplication1 is the name of the project.
0
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32797679
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
 

Author Comment

by:arif_caesar
ID: 32797717
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
 

Author Comment

by:arif_caesar
ID: 32797735
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
 

Author Comment

by:arif_caesar
ID: 32797781
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32798348
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
 

Author Comment

by:arif_caesar
ID: 32798404
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32798430
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
 

Author Comment

by:arif_caesar
ID: 32798445
soapexception
screenShot1.bmp
0
 

Author Comment

by:arif_caesar
ID: 32798454
I did take off the dummy ID and replaced it with a functional one.
0
 

Author Comment

by:arif_caesar
ID: 32798475
FYI - I have taken the new ID from the ContactBase table of SQL Server, Is it the right place for it?
0
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32798514
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
 

Author Comment

by:arif_caesar
ID: 32798620
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32798903
The errors is referring to the windows account that you are logged in as. Do you have an enabled CRM user account?
0
 

Author Comment

by:arif_caesar
ID: 32799129
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
 
LVL 30

Expert Comment

by:Feridun Kadir
ID: 32799146
That'll be the reason then. You can only work with the CRM web services if you have an enabled CRM account.
0
 

Author Comment

by:arif_caesar
ID: 32799493
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
Viewers will learn the different options available in the Backstage view in Excel 2013.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question