locked
ServiceContext namespace? RRS feed

  • Question

  • Using on Premises, CRM 2011 and writing in C#...

    I am writing some code based on the example in http://msdn.microsoft.com/en-us/library/gg334593.aspx.

    For the line of code that reads:

     using (ServiceContext svcContext = new ServiceContext(_serviceProxy))
    {
    
    }
    I am getting an error on ServiceContext.

    "The type or namespace name 'ServiceContext' could not be found (are you missing a using directive or an assembly reference?)"

    I have the following using directives, although I am not sure all are required; what am I missing?

    using System;
    using System.ServiceModel;
    using System.ServiceModel.Description;

    using System.Linq;
    using System.Collections;
    using System.Collections.Generic;
    using System.Collections.Specialized;
    using System.Xml.Linq;
    using System.Configuration;
    using System.Data;

    using System.Text;

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Client;
    using Microsoft.Xrm.Client;
    using Microsoft.Xrm.Client.Configuration;
    using Microsoft.Xrm.Sdk.Query;
    using Microsoft.Crm.Sdk.Messages;
    using Microsoft.Xrm.Sdk.Discovery;

    using Xrm;

    Thank you for all assistance.

    Wednesday, May 22, 2013 3:00 PM

Answers

  • you need to add the file Xrm.cs inside your project (probably you already did) and in your case you need to write:

     using (XrmServiceContext svcContext = new XrmServiceContext(_serviceProxy))
    {
    
    }

    because your contextname is XrmServiceContext


    My blog: www.crmanswers.net

    • Marked as answer by MeProgrammer Wednesday, May 22, 2013 3:52 PM
    Wednesday, May 22, 2013 3:46 PM

All replies

  • Hi,
    to use the service context you need to generate first the early bound classes, as described here:

    http://msdn.microsoft.com/en-us/library/gg327844.aspx

    the parameter servicecontextname is the one that you need to specify (in your example you need to write /servicecontextname:ServiceContext)



    My blog: www.crmanswers.net


    Wednesday, May 22, 2013 3:09 PM
  • Thanks for replying, Guido.

    The following is the CrmSvcUtil command I used:

    CrmSvcUtil.exe /codeCustomization:"Microsoft.Xrm.Client.CodeGeneration.CodeCustomization, Microsoft.Xrm.Client.CodeGeneration" /out:Xrm\Xrm.cs /url:http://ourservername/ourinstancename/XRMServices/2011/Organization.svc
    /domain:ourdomainname /username:myusername /password:mypassword /namespace:Xrm /serviceContextName:XrmServiceContext

    So I have specified the service context. How do I use this in the code?

    Wednesday, May 22, 2013 3:39 PM
  • you need to add the file Xrm.cs inside your project (probably you already did) and in your case you need to write:

     using (XrmServiceContext svcContext = new XrmServiceContext(_serviceProxy))
    {
    
    }

    because your contextname is XrmServiceContext


    My blog: www.crmanswers.net

    • Marked as answer by MeProgrammer Wednesday, May 22, 2013 3:52 PM
    Wednesday, May 22, 2013 3:46 PM
  • Yes, I did that, thanks: sucess! I got beyond that error.
    Could you help me some more? When I execute the following code, I get "User does not have the privilege to act on behalf another user." on the foreach.

    using (XrmServiceContext context = new XrmServiceContext(_serviceProxy))
                {
                    
                    foreach (var report in context.new_xxxcontrolSet)
                    {
                        if (sToday == report.ce_NextRunDate.ToString())
                        {
                            //Run Report
                            Console.Write("Running report for " + report.ce_name);
                        }
                    }
                }

    I believe I've seen this before but I don't recall what the answer is. Do you know?

    Thank you again.

    Wednesday, May 22, 2013 3:55 PM