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

Posted on 2008-02-12
Medium Priority
Last Modified: 2010-04-21
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", "http://www.w3.org/2001/XMLSchema-instance", "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

Question by:MindenMan
LVL 44

Expert Comment

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


Author Comment

ID: 20875265
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.
LVL 25

Expert Comment

ID: 20875619
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.  
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


Author Comment

ID: 20876279
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.

Expert Comment

ID: 20876607
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 http://blogs.msdn.com/arash/archive/2006/08/25/719626.aspx 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.


Author Comment

ID: 20876863
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.

Accepted Solution

bmosoftware earned 2000 total points
ID: 20878839
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.

Author Closing Comment

ID: 31430137
Many thanks BMO.
An upgrade it is then.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
For cloud, the “train has left the station” and in the Microsoft ERP & CRM world, that means the next generation of enterprise software from Microsoft is here: Dynamics 365 is Microsoft’s new integrated business solution that unifies CRM and ERP fun…
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

624 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