Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

Get field value from Dynamics CRM 365 using WCF Service

$
0
0

I have a requirement to create web service to get a records in dynamics crm. And there is a web application that will call this web service to display the records in it. In this case, i created WCF Service project. I have done writing the script for this WCF Service project. Since i haven't had an access to the CRM Server, i couldn't test this WCF Service. And this is the first time i write WCF Service. So i need an advice and correction whether my script is correct or is not.

This is my code:

Service1.svc.cs

namespace WCF_CRM_Multipolar
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IService1
    {

        OrganizationServiceProxy _orgservice;
        string userName = "";
        string passWord = "";
        string Url      = "";
        private bool InitializeCRMService(string userName, string passWord, string Url)
        {
            bool isSuccess = false;
            Uri organizationUrl = new Uri(Url);
            ClientCredentials credit = new ClientCredentials();
            credit.UserName.UserName = userName;
            credit.UserName.Password = passWord;
            _orgservice = new OrganizationServiceProxy(organizationUrl, null, credit, null);
            _orgservice.ServiceConfiguration.CurrentServiceEndpoint.EndpointBehaviors.Add(new ProxyTypesBehavior());

            if (_orgservice != null)
                isSuccess = true;
            return isSuccess;

        }
	public string BudgetBalance(string userlogin)
        {
            if (InitializeCRMService(userName, passWord, Url))
            {
                QueryExpression query = new QueryExpression("systemuser");
                string[] cols = { "businessunitid", "domainname" };
                query.Criteria = new FilterExpression();
                query.Criteria.AddCondition("domainname", ConditionOperator.Equal, userlogin);
                query.ColumnSet = new ColumnSet(cols);
                var userbusinessunit = _orgservice.RetrieveMultiple(query);
                Guid buid = (Guid)userbusinessunit[0].Attributes["businessunitid"];

                QueryExpression query1 = new QueryExpression("new_budget");
                string[] cols1 = { "owningbusinessunit", "new_selfremainbudget"};
                query1.Criteria = new FilterExpression();
                query1.Criteria.AddCondition("owningbusinessunit", ConditionOperator.Equal, buid);
                query1.ColumnSet = new ColumnSet(cols1);
                var budgetremaining = _orgservice.RetrieveMultiple(query1);


                Decimal remain_budget = budgetremaining[0].GetAttributeValue<decimal>("new_selfremainbudget");

                return "Latest budget available: " + remain_budget.ToString();
            }
            else
            {
                return "Failed to retrieve budge from crm";
            }
        }
    }
}


IService1.cs

namespace WCF_CRM_Multipolar
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {

        [OperationContract]
        string BudgetBalance(string userlogin);


        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.

}

 

The idea is when the web application call this web service, the web application fill the parameter (user login) and get the remain_budget as the return value. So they can use remain_budget and display it in web application.

The question:

When i deploy this to IIS. Is the web application can call this web service and get the return value ? If is not, could you tell me where is the error that i made


Viewing all articles
Browse latest Browse all 55831

Trending Articles