locked
VS 2005 vs 2008 RRS feed

  • Question

  • Hi all,

    I have created a website in vs-2005 that is used to create, update and delete the records in CRM using SDK. I hosted the same in IIS as a new website (not in CRM Website and Default website). Its working fine for me. The same code I have used in vs-2008 for those operations and hosted in IIS as a new website. Now I am getting the following error.

    Stack Trace

    [WebException: The request failed with HTTP status 401: Unauthorized.]
       System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall) +533199
       System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters) +204
       Microsoft.Crm.SdkTypeProxy.CrmService.Execute(Request Request) +47
       _Default.fillGrid() in c:\Inetpub\wwwroot\empdetails2\Default.aspx.cs:200
       _Default.Page_Load(Object sender, EventArgs e) in c:\Inetpub\wwwroot\empdetails2\Default.aspx.cs:28
       System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34
       System.Web.UI.Control.OnLoad(EventArgs e) +99
       System.Web.UI.Control.LoadRecursive() +47
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061


    Code is

    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using Microsoft.Crm.Sdk.Query;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    public partial class _Default : System.Web.UI.Page
    {
        DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            dt.Columns.Add("Employee Name", typeof(string));
            dt.Columns.Add("Employee Id", typeof(string));
            dt.Columns.Add("Guid", typeof(Guid));
            if (!IsPostBack)
            {
                fillGrid();
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            CrmService service = getService();

            DynamicEntity emp = new DynamicEntity();
            emp.Name = "new_employeedetails";

            StringProperty empname = new StringProperty();
            empname.Name = "new_employeename";
            empname.Value = TextBox1.Text;

            StringProperty empid = new StringProperty();
            empid.Name = "new_employeeid";
            empid.Value = TextBox2.Text;

            emp.Properties.Add(empname);
            emp.Properties.Add(empid);

            Guid id = service.Create(emp);

            fillGrid();
            clear();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            CrmService service = getService();
           
            DynamicEntity emp = new DynamicEntity();
            emp.Name = "new_employeedetails";

            StringProperty empname = new StringProperty();
            empname.Name = "new_employeename";
            empname.Value = TextBox1.Text;

            StringProperty empid = new StringProperty();
            empid.Name = "new_employeeid";
            empid.Value = TextBox2.Text;

            KeyProperty empguid = new KeyProperty();
            empguid.Name = "new_employeedetailsid";
            empguid.Value = new Key();
            empguid.Value.Value = new Guid(Session["id"].ToString());

            emp.Properties.Add(empname);
            emp.Properties.Add(empid);
            emp.Properties.Add(empguid);

           service.Update(emp);

            fillGrid();
            clear();
        }
        protected void Button3_Click(object sender, EventArgs e)
        {
            CrmService service = getService();
            Guid empguid = new Guid(Session["id"].ToString());
            service.Delete("new_employeedetails", empguid);
            fillGrid();
            clear();
        }

        public void fillGrid()
        {
            CrmService service = getService();

            RetrieveMultipleRequest req = new RetrieveMultipleRequest();
            req.ReturnDynamicEntities = true;
            QueryExpression qe = new QueryExpression();
            qe.ColumnSet = new AllColumns();
            qe.Distinct = false;
            qe.EntityName = "new_employeedetails";
            FilterExpression exp = new FilterExpression();
            exp.FilterOperator = LogicalOperator.And;
            ConditionExpression cond = new ConditionExpression();
            cond.AttributeName = "new_employeename";
            cond.Operator = ConditionOperator.NotNull;
            exp.Conditions.Add(cond);
            qe.Criteria = exp;
            req.Query = qe;
            RetrieveMultipleResponse resp = service.Execute(req) as RetrieveMultipleResponse;
            BusinessEntityCollection beColl = resp.BusinessEntityCollection;
            foreach (DynamicEntity de in beColl.BusinessEntities)
            {
                string name = de.Properties["new_employeename"].ToString();
                string id = de.Properties["new_employeeid"].ToString();
                Guid empguid = ((Key)de.Properties["new_employeedetailsid"]).Value;

                DataRow dr = dt.NewRow();
                dr["Employee Name"] = name;
                dr["Employee Id"] = id;
                dr["Guid"] = empguid;
                dt.Rows.Add(dr);
            }
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
       
        public CrmService getService()
        {
            CrmAuthenticationToken token = new CrmAuthenticationToken();
            token.OrganizationName = "B2BLIVE8";
            token.AuthenticationType = 0;
            CrmService service = new CrmService();
            service.CrmAuthenticationTokenValue = token;
            service.Credentials = System.Net.CredentialCache.DefaultCredentials;
            service.Url = http://<Server>/mscrmservices/2007/crmservice.asmx;
            return service;
        }
      
        public void clear()
        {
            TextBox1.Text = " ";
            TextBox2.Text = " ";
           
        }
        protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[3].Visible = false;
        }
        protected void GridView1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            Session["id"] = GridView1.SelectedRow.Cells[3].Text;
            TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
            TextBox2.Text = GridView1.SelectedRow.Cells[2].Text;
        }
    }


    What could be the reason?

    Wednesday, July 22, 2009 11:04 AM

Answers

  • Hello Golden Fish,

    Check the Authentication in IIS for both sites and make sure that you disabled anonymous access in both so they will read from windows credentials.
    Also, add  <identity impersonate="true" /> under your system.web node in the web.config file.

    Tuesday, August 11, 2009 7:08 AM

  • To run on windows authentication:

    1. Open the authentication methods window from the Directoty Security and then click on edit in Authentication and access control section.

    2. Uncheck the Enable anonymous access and check only Integrated windows authentication.

    Or you can run the web service under a domain account that has access on CRM...

    1. Open the authentication methods window from the Directoty Security and then click on edit in Authentication and access control section.

    2. Check the Enable anonymous access and set a user name and password of a domain account that has access in CRM.

    Tuesday, August 18, 2009 8:47 AM

All replies

  • HI,

    I think you could first start with your web.config, and see if it is same for both the application. Specially the authentication section.

    Regards,
    Nishant Rana
    http://nishantrana.wordpress.com
    Wednesday, July 22, 2009 11:08 AM
  • yes both are same.

    <?xml version="1.0"?>
    <!--
        Note: As an alternative to hand editing this file you can use the
        web admin tool to configure settings for your application. Use
        the Website->Asp.Net Configuration option in Visual Studio.
        A full list of settings and comments can be found in
        machine.config.comments usually located in
        \Windows\Microsoft.Net\Framework\v2.x\Config
    -->
    <configuration>
     <appSettings/>
     <connectionStrings/>
     <system.web>
      <!--
                Set compilation debug="true" to insert debugging
                symbols into the compiled page. Because this
                affects performance, set this value to true only
                during development.
            -->
      <compilation debug="true"/>
      <!--
                The <authentication> section enables configuration
                of the security authentication mode used by
                ASP.NET to identify an incoming user.
            -->
      <authentication mode="Windows"/>
      <!--
                The <customErrors> section enables configuration
                of what to do if/when an unhandled error occurs
                during the execution of a request. Specifically,
                it enables developers to configure html error pages
                to be displayed in place of a error stack trace.

            <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
                <error statusCode="403" redirect="NoAccess.htm" />
                <error statusCode="404" redirect="FileNotFound.htm" />
            </customErrors>
            -->
     </system.web>
    </configuration>

    Wednesday, July 22, 2009 11:22 AM
  • Hi,

    Are both these web application on the same server? and And are you accessing the page from the same machine?

    Regards,
    Nishant Rana
    http://nishantrana.wordpress.com
    Wednesday, July 22, 2009 11:27 AM
  • Hi,

    I have developed boht the applications in my machine and copy the folder into server wwwroot folder and create a website from there.
    Wednesday, July 22, 2009 11:29 AM
  • Hello Golden Fish,

    Check the Authentication in IIS for both sites and make sure that you disabled anonymous access in both so they will read from windows credentials.
    Monday, August 10, 2009 9:40 AM
  • Hello Golden Fish,

    Check the Authentication in IIS for both sites and make sure that you disabled anonymous access in both so they will read from windows credentials.
    Also, add  <identity impersonate="true" /> under your system.web node in the web.config file.

    Tuesday, August 11, 2009 7:08 AM

  • To run on windows authentication:

    1. Open the authentication methods window from the Directoty Security and then click on edit in Authentication and access control section.

    2. Uncheck the Enable anonymous access and check only Integrated windows authentication.

    Or you can run the web service under a domain account that has access on CRM...

    1. Open the authentication methods window from the Directoty Security and then click on edit in Authentication and access control section.

    2. Check the Enable anonymous access and set a user name and password of a domain account that has access in CRM.

    Tuesday, August 18, 2009 8:47 AM