Showing posts with label CRM Programmatically. Show all posts
Showing posts with label CRM Programmatically. Show all posts

Saturday, January 8, 2011

Programmatically set Picklist Option value in CRM Entity Item

In my older post I have collect Picklist options in DropDownList.
So now I save this DropDown’s selected value in CRM’s Entity Item’s Attribute.
You can’t save option in Entity Item, without picklist’s Value & Text. Means you have to provide Option’s Value & Options Text.
Also you have to create a new instance of the picklist before setting the value.
See the below sample code snippet for set the picklist option value.


CrmSdk.new_customentity newcustomentity = new CrmSdk.new_customentity();//Custom Entity's obj

newcustomentity.new_entityattribute = new CrmSdk.Picklist();//Creating New Instance before assign Value & Text
newcustomentity.new_entityattribute.Value = Convert.ToInt32(objDropDownList.SelectedItem.Value); //Set DropDownList's Value
newcustomentity.new_entityattribute.name = objDropDownList.SelectedItem.Text;//Set DropDownList's Text


Happy Coding!!!

Programmatically Getting CRM Entiry Picklist Option value

Whenever use picklist in CRM Entity’s Attribute you have to collect all picklist options in any collections like ArrayList, HashTable OR controls like DropDownList, ListBox etc. for display Options in UI.
For collecting Picklist Options you have to use CRM’s MetaDataService.asmx as older Post.
See the below sample code snippet for collect picklist options.




RetrieveAttributeRequest request = new RetrieveAttributeRequest();//Create Obj for RetrieveAttributeRequest

request.EntityLogicalName = CrmSdk.EntityName.new_crmcustomentity.ToString();//Assign CRM's Custom Entity

request.LogicalName = "new_customattribute";//Set Obj's Picklist Attribute Name



//Now collect entity's response using Metadataservice execution

RetrieveAttributeResponse response = (RetrieveAttributeResponse)service.Execute(request);//service is MetaDataService.asmx's object

//collect Picklist Attribute Metadata value

PicklistAttributeMetadata picklist = (PicklistAttributeMetadata)response.AttributeMetadata;



//here I use DropDownList for collecting PickList Options

foreach (Option o in picklist.Options)

{

objDropDownList.Items.Add(new ListItem(o.Label.UserLocLabel.Label, o.Value.Value.ToString()));

}


In my Next Post you will see how to set Picklist Option in CRM Entity Item from Custom UI.

Happy Coding !!!

Thursday, January 6, 2011

Programmatically Create MS CRM Account

Using CRM SDK4.0 you can customize CRM programmatically.
Download CRMSDK 4.0 from here
http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=82e632a7-faf9-41e0-8ec1-a2662aae9dfb&displaylang=en
Then add web service reference in your project
Example:
http://CrmApplicationUrl/MSCRMServices/2007/CrmService.asmx
Also you have to pass below Items in CRM Code.
- Organization Name

- Username, Password & Domain (CRM Credential)
See the below function code for CRM Account Creation.




public static void createAccount(string NewAccountName)
{
CrmSdk.CrmService myCrm = new CrmSdk.CrmService();
myCrm.Url = "http://CrmApplicationUrl/MSCRMServices/2007/CrmService.asmx";

CrmSdk.CrmAuthenticationToken myCrmToken = new CrmSdk.CrmAuthenticationToken();
myCrmToken.AuthenticationType = 0;
myCrmToken.OrganizationName = GetCrmOrganization();//"OrganizationName";

myCrm.CrmAuthenticationTokenValue = myCrmToken;

System.Net.NetworkCredential nc = new System.Net.NetworkCredential();
nc.UserName = "Username";//Pass Username of CRM
nc.Password = "Password";//Pass Password of above CRMAccount
nc.Domain = "Domain";//Pass Domain name

myCrm.Credentials = nc;
CrmSdk.account newAccount = new CrmSdk.account();

newAccount.name = NewAccountName;
Guid newAccountId = myCrm.Create(newAccount);
}



Happy coding !!!