Wednesday, 29 July 2015

Sample code for creating contact record through web service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk;
using System.ServiceModel.Description;

namespace crmtovswebservice
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public void run(string a, string b)
        {

            try
            {
           
                ClientCredentials cre = new ClientCredentials();
                cre.UserName.UserName = "username";
                cre.UserName.Password = "password";


                Uri serviceUri = new Uri("your server name");

           OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                IOrganizationService service = (IOrganizationService)proxy;


                Entity ent = new Entity("contact");// creation of entity
                ent["firstname"] = a;
                ent["lastname"] = b;
                service.Create(ent);

             
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException();
            }


        }
    }
}

Thursday, 23 July 2015

Sample code for retreiving data Through Odata in crm

function player()
 {

   var id = Xrm.Page.data.entity.attributes.get("ccs_playername2").getValue();
   alert(id[0].name);
   var serverUrl = "http://"+window.location.host+"/" + Xrm.Page.context.getOrgUniqueName();
   if(id!=null)
   {
     var ODataPath = serverUrl + "/XRMServices/2011/OrganizationData.svc";
     var retrieveReq = new XMLHttpRequest();
     //replace with your odata query
     var Odata = ODataPath + "/ccs_informationSet?$select=ccs_jersey&$filter=ccs_informationId eq guid'" + id[0].id + "'";
     retrieveReq.open("GET", Odata, true);
     retrieveReq.setRequestHeader("Accept", "application/json");
     retrieveReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
     retrieveReq.onreadystatechange = function () { retrieveReqCallBack(this); };
     retrieveReq.send();
   }
 }
 function retrieveReqCallBack(retrieveReq) {
       if (retrieveReq.readyState == 4) { 

  var retrieved =this.parent.JSON.parse(retrieveReq.responseText).d;     
  //for (i = 0; i < retrieved.results.length; i++) {
 // statecode = retrieved.results[i].ccs_jersey;
  //alert(statecode);
  //}
          
            var statecode = retrieved.results[0].ccs_jersey;
             alert(statecode);
             Xrm.Page.data.entity.attributes.get("ccs_jerseyno").setValue(statecode);
           }        
 }

Sample Code For creating Plugins For sending Email in crm

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
namespace Plugins
{
    public class Folloeplugins:IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                try
                {
                   
                  
                        Entity email = new Entity("email");
                        Entity fromparty=new Entity("activityparty");
                        Entity toparty=new Entity("activityparty");
                        toparty["partyid"] = new EntityReference("systemuser", context.UserId);
                        fromparty["partyid"] = new EntityReference("systemuser", context.UserId);
                        email["from"] = new Entity[] { fromparty };
                        email["to"] = new Entity[] { toparty };
                        email["subject"] = "email subject - " + DateTime.Now.ToString();
                        email["description"] = "email description";


                        email["regardingobjectid"] = new EntityReference("account", entity.Id);
                       
                      
                        Guid emailid=  service.Create(email);
                        SendEmailRequest sendEmailreq = new SendEmailRequest
                        {
                            EmailId = emailid,
                            TrackingToken = "",
                            IssueSend = true
                        };
                        SendEmailResponse sendEmailresp = (SendEmailResponse)service.Execute(sendEmailreq);
                     
                   
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
                }
                   
                }
            }

        }
    }
 

Tuesday, 14 July 2015

Sample code for creating a record in visual studio and updating it in crm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using System.ServiceModel.Description;
using System.Web.Services.Protocols;

namespace sample_code2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                ClientCredentials cre = new ClientCredentials();
                cre.UserName.UserName = "username";// enter your user name
                cre.UserName.Password = "password";// enter your password
                Uri serviceUri = new Uri("login address");//give the url of your crm organization.You will                                                                                                                     find it in developer resource.

            OrganizationServiceProxy proxy = new OrganizationServiceProxy(serviceUri, null, cre, null);
                proxy.EnableProxyTypes();
                IOrganizationService service = (IOrganizationService)proxy;


                Entity ent = new Entity("contact");// creation of entity
                ent["firstname"] = textBox1.Text;
                ent["lastname"] = textBox2.Text;
                service.Create(ent);


            }
            catch (SoapException ex)
            {
                throw new InvalidOperationException();
            }
         

        }

    }
}

Thursday, 9 July 2015

Creation of simple plugin in crm

using System;
using System.ServiceModel;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
  public class FollowupPlugin: IPlugin
 {
  /// <summary>
        /// A plug-in that creates a follow-up task activity when a new account is created.
  /// </summary>
        /// <remarks>Register this plug-in on the Create message, account entity,
        /// and asynchronous mode.
        /// </remarks>
        public void Execute(IServiceProvider serviceProvider)
  {
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService =
                (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
                // Obtain the target entity from the input parameters.
                Entity entity = (Entity)context.InputParameters["Target"];

                // Verify that the target entity represents an account.
                // If not, this plug-in was not registered correctly.
                if (entity.LogicalName != "account")
                    return;

                try
                {
                    // Create a task activity to follow up with the account customer in 7 days. 
                    Entity followup = new Entity("task");

                    followup["subject"] = "Send e-mail to the new customer.";
                    followup["description"] =
                        "Follow up with the customer. Check if there are any new issues that need resolution.";
                    followup["scheduledstart"] = DateTime.Now.AddDays(7);
                    followup["scheduledend"] = DateTime.Now.AddDays(7);
                    followup["category"] = context.PrimaryEntityName;

                    // Refer to the account in the task activity.
                    if (context.OutputParameters.Contains("id"))
                    {
                        Guid regardingobjectid = new Guid(context.OutputParameters["id"].T
String());
                        string regardingobjectidType = "account";

                        followup["regardingobjectid"] =
                        new EntityReference(regardingobjectidType, regardingobjectid);
                    }

                    // Obtain the organization service reference.
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                    // Create the task in Microsoft Dynamics CRM.
                    tracingService.Trace("FollowupPlugin: Creating the task activity.");
                    service.Create(followup);
                }
                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in the FollupupPlugin plug-in.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowupPlugin: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

Thursday, 2 July 2015

Java script program for Validating Email and Phone Number in crm

function phonenumber()
{
var a=Xrm.Page.getAttribute( "ccs_phoneno").getValue();
var reg = /^.*(?=.{10,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=]).*$/;
   var K = reg.exec(a);
       if (!K)
                 alert("phone number is  valid");
            else

             alert("phone number is not valid");

         var email = Xrm.Page.getAttribute( "ccs_email_id").getValue();
var reg = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
        var OK = reg.exec(email);
       if (!OK)

    alert("Email Address isn't  valid");

        else
         alert("Email Address  is  valid");

   }  

Monday, 11 May 2015

Program to find the number of vowels in a string using Java


public class Vowels {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        String str[]={"a,e,i,o,u"};
         int j=0;
        System.out.println("enter the string");
        Scanner sc=new Scanner(System.in);
        String str1=sc.nextLine();
        for(int i=0;i<str1.length();i++)
        {
           char ch=str1.charAt(i);
           String temp=Character.toString(ch);
            
            if(temp.equalsIgnoreCase("a" )|| temp.equalsIgnoreCase("e")||temp.equalsIgnoreCase("i")||temp.equalsIgnoreCase("o")||temp.equalsIgnoreCase("u"))
            {
                    
                j++;
            }
                
        }
                    
        System.out.println("No of vowels is"+j);
           
           
        
    }
}
        
      
        
    

Saturday, 9 May 2015

String Comparison using compareToIgnorecase

Simple Java Program For Comparing Two Strings Using                                   CompareToIgnoreCase




import java.util.Scanner;

public class Stringcomparrison {

 
    public static void main(String[] args) {
        
  
       Scanner input = new Scanner( System.in );
       System.out.print("Enter the string ");
       String str=input.nextLine();
       StringBuffer s=new StringBuffer(str);
       if(str.compareToIgnoreCase("Muthu")==0)
       
           System.out.println("equal");
           else
           System.out.println("not equal");
       
       
       
    }
}