How do I compile C# class using VS.Net2008 or 5 for MS CRM?

I'm a newbie to C# development and also MS CRM.
I need to create two classes, one for a bulk import into a custom entity and the other to create an autonumber field.

I have all of the C# code and I beleive i have configured my project correctly.  however when I try and compile the class I get the error "The name 'CrmSdk' does not exist in the current context".  I am using the standard code examples from Microsoft so thus using exactly the same terminology - CrmSdk is the name of the Web Service Reference for the CrmService.

I then thought that i needed the SDK help file from the SDK kit, so I added that and got the following error: "The type or namespace name 'CrmBoolean' could not be found (are you missing a using directive or an assembly reference?)"

I haven't built a C# project before so I'm totally new to this.  So.... I then went and got some really simple code to verify the CrmService access this is the code I have used:

using System;
using MyProject.CrmSdk;

namespace MyProject
   public class Class1
      public Class1()
         CrmService service = new CrmService();

         account myAccount = new account();
         myAccount.accountnumber = "1234";

And... I still get exactly the same errors.
My project includes the reference Microsoft.CRM.Platform.Callout.Base.dll, but this is only required for the first project mentioned above.
Do I need to do something to VS .Net 2008 or my project prior to creating the project or am I missing something fundamental?

I have to be honest this is driving me potty and ever so slightly insane.

Any help would be greatly appreciated and would stop me from throwing myself underneath the next train!


using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using Microsoft.Crm.Callout;
using ItemFormCallout.CrmSdk;
namespace ItemFormCallout.Callout
            /// This sample shows how to log various events with callouts
            public class ItemCallout: CrmCalloutBase
                public ItemCallout()
                // This sample shows how to log an object creation using a precallout 
                public override PreCalloutReturnValue PreCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, ref string entityXml, ref string errorMessage)
                //Call the helper function NextItemNumber() to return the next highest item number value 
                string nextItemNumber = NextItemNumber();
            //Create an xml document in order to work with the xml stream
            XmlDocument entityDoc = new XmlDocument();
            //Create the appropriate xml code node
            XmlNodeList propertyList = entityDoc.GetElementsByTagName("Property");
            XmlElement ItemNumberValue = null;
            XmlElement properties = (XmlElement) entityDoc.GetElementsByTagName("Properties")[0];
            XmlElement ItemNumberElement = entityDoc.CreateElement("Property");
            XmlAttribute typeAttrib = entityDoc.CreateAttribute("type");
            // Set the values for our new_itemnumber field
            ItemNumberElement.SetAttribute("type", "", "StringProperty");
            ItemNumberElement.SetAttribute("Name", "new_itemnumber");
            ItemNumberValue = entityDoc.CreateElement("Value");
            ItemNumberValue.InnerText = NextItemNumber();
            //Add back to the entityXml
            StringWriter output = new StringWriter();
            entityXml = output.ToString();
            //Remove the extra XML that will confuse CRM
            entityXml = entityXml.Replace("xmlns=\"\"","");
            entityXml = entityXml.Replace("<?xml version=\"1.0\" encoding=\"utf-16\"?>", "");
            return PreCalloutReturnValue.Continue;
            #region Helpers
            public bool IsTextIncluded( string InputString, string ValueToCheck )
            return (InputString.IndexOf(ValueToCheck) > -1);
            public string NextItemNumber()
            // Standard CRM Service Setup
            CrmService = new CrmService();
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.Url = "http://sbs2003svr:5555/mscrmservices/2006/crmservice.asmx";
            // We need a user that has global read access to the Item record so that we
            // have the absolute maximum item number.  If all roles have global read privelages to
            // the read value of the lead, then this would not be neccessary
            // for production, access this guid in a cinfig file.
            //Guid callerid = new Guid("0686EE2A-FC46-DA11-B935-0003FF07CD51");
            //Impersonate our global read user
            //service.CallerIdValue = new CallerId();
            //service.CallerIdValue.CallerGuid = callerid;
            // Create a set of collumns to return
            ColumnSet cols = new ColumnSet();
            cols.Attributes = new string [] {"new_custitem", "new_itemnumber"};
            // set the order of the bring back the highest new_itemnumber value at the top
            OrderExpression order = new OrderExpression();
            order.AttributeName = "new_itemnunber";
            order.OrderType = OrderType.Descending;
            // To improve performance, we will only pass back the top record
            // This will only return 1 page with 1 record per page
            PagingInfo pages = new PagingInfo();
            pages.PageNumber = 1;
            pages.Count = 1;
            // create an query expression and set the query parameters
            QueryExpression query = new QueryExpression();
            query.EntityName = EntityName.item.ToString();
            query.Columnset = cols;
            query.Orders = new OrderExpression[] {order};
            query.PageInfo = pages;
            //Retrieve the value from CRM
            BusinessEntityCollection retrieved = service.RetrieveMultiple(query);
            string nextNumber = "1";
            //check to see if we have any records
            if (retrieved.BusinessEntities.Length > 0)
            //cast to results item object and only retrieve first record
            item results = (item)retrieved.BusinessEntities[0];
            //Return the next value item number.  If there are records, but none have a number
            //(the result will be null), so just pass bavk 1
            nextNumber = (results.new_itemnumber !=null) ? (results.new_itemnumber.Value +1).ToString() : "1";
            return nextNumber;

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Have you added a REFERENCE (Project/Add Reference in Soultion Explorer) to the CRM library?

MindenManAuthor Commented:
Hi Arthur;
Thanks for replying so quickly.
Two references have been added thus far.
One is a standard reference to the CRM dll found on the server CD.  I don't think i need that one for the simple code but I do need it for the more complex one added to my question.
The other reference is a Web Reference which points to the CrmService.asmx on the CRM web server.

Do you think i need any others?  If so do you know what I need to add?

Thank you again.
You don't reference CRM dlls directly - you need to add a Web Reference to the CRM Web Service.  If you've already done that, then you change the reference to "CrmSdk" in your code to the name of your web reference - or you can rename your web reference to "CrmSdk".  Note that not all examples use the same name for the web reference, so you need to adjust accordingly.  
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

MindenManAuthor Commented:
thanks also for the quick response.  
Below is what I did, I'm assuming this was correct?  Please let me know otherwise:

The web reference points to the web service, the web service is found at http://sbs2003svr:5555/mscrmservices/2006/crmservice.asmx, when you go to specify the web reference you put in the Url and it then allows you to create a web reference to it, which I did calling it CrmSdk.  the web reference then appeared in my solutions explorer window under "Web Reference".  I understand that the name is case sensitive so I have tried all different case scenarios.

Thanks again and look forward to an eagerly awaited response.
Hi MindenMan,

A couple of things, as was previously said by dstanley, in CRM 3.0 you should not need to directly reference the DLL's, if you still have references to these dll's in your project they need to be removed as they will interfere with the CrmSdk web reference. The next part is the namespace referencing, this can often be a difficulty when referencing in your project, make sure your root namespace is consistant throughout your project, including in the project properties pages.

Some other important things... based on what I am seeing here you are using CRM 3.0 and your sample code is trying to make use of a CRM 3.0 Callout assembly, if a callout is what you want then Visual Studio 2005 and 2008 are not options as CRM 3.0 is unfortunately written on the .Net 1.1 Framework and as such callouts need to be created on the .Net 1.1 Framework in VS 2003. There is however a freely available Wrapper for VS 2005 which you can get from but VS 2008 will definately be out of the question.

If you are not needing to write a callout and need to create an aspx integration you can use the other Visual Studio's but will need to make sure that your aspx web application runs under a seperate application pool to the CRM Web Site.

MindenManAuthor Commented:
Thanks BMO.
I'm gutted!  Total news to me about VS2003, 2005 and 2008 - what are MS playing at?  I can't believe I have to go and buy VS2005 just to do some "simple" callouts on CRM3 after forking out for VS2008.  Like I said totally gutted.  In would have epxected to be able to write code for a current product using their latest software - oh simple me!

Really like to say thanks BMO, otherwise I could've been at this forever and got nowhere.

So, I assume that my callout script aside, the simple code on my question won't work either?  I didn't reference any DLLs on that one.  I just created a new project. one web reference as mentioned above and then clicked build and.... no dice.

I've checked through all of the property pages and anything I could view and I haven't found any other references to namespaces of another name.

Thanks in advance again.
Sorry that you are so gutted, but I can feel your pain, I am a bleeding edge software user myself, so always like to use the latest and greatest and have the same challenges when things use outdated technology, so I know how you feel.

I would assume that your callout script should still build, but I haven't actually tried to build a CRM 3.0 callout is VS 2008, so I would think that there is something simple wrong in terms of syntax and namespaces etc. but it is unfortunately very difficult to figure it out by just looking at what you have pasted here :(

If you have Software Assurance for the licenses of CRM you will be able to acquire CRM 4.0 which will work very well with VS 2008. I would highly recommend a migration to 4.0 especially if you are only just starting to work with .Net stuff as it is vastly improved in 4.0 and also better to migrate before you get too carried away with 3.0 assemblies. The upgrade is very simple, especially if you haven't got any custom .Net assemblies, it iss pretty much a next, next, wizard install over your CRM 3.0 installation and it upgrades everything for you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MindenManAuthor Commented:
Many thanks BMO.
An upgrade it is then.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.