I created a simple windows application just to see how to make use of IOrganizationService within CRM 2011.
Here we need to add references to the following dlls
- Microsoft.Xrm.Sdk.
- System.ServiceModel.
- System.Runtime.Serialization.
This is the sample code
Uri organizationUri = new Uri("http://crmservername/orgname/XRMServices/2011/Organization.svc");
Uri homeRealmUri = null;
ClientCredentials credentials = new ClientCredentials();
// set default credentials for OrganizationService
credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
// or
credentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
OrganizationServiceProxy orgProxy = new OrganizationServiceProxy(organizationUri, homeRealmUri, credentials, null);
IOrganizationService _service = (IOrganizationService)orgProxy;
try
{
Entity myAccount = new Entity("account");
myAccount["name"] = "Test Account";
_service.Create(myAccount);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Discover more from Nishant Rana's Weblog
Subscribe to get the latest posts sent to your email.

Hi
default credentials in the sense it will take ur windows credentials. instead of that give the username,password and domain name for crm 2011. I had worked on sharepoint 2010 and crm integratiion. Here is my Sample code i had used.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MSCRMBdcModel.CrmSdk;
namespace MSCRMBdcModel.BdcModel1
{
///
/// All the methods for retrieving, updating and deleting data are implemented in this class file.
/// The samples below show the finder and specific finder method for Entity1.
///
public class AccountService
{
private static CrmService GetCRMService()
{
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType = 0;
token.OrganizationName = “Vertexcs”;
CrmService service = new CrmService();
service.Url = “http://192.168.1.200/mscrmservices/2007/crmservice.asmx”;
service.CrmAuthenticationTokenValue = token;
//service.Credentials = System.Net.CredentialCache.DefaultCredentials;
service.Credentials = new System.Net.NetworkCredential(“uname”, “pwd”, “domain”);
return service;
}
///
/// This is a sample specific finder method for Entity1. If you want to delete or rename the method think about changing the xml
/// in the BDC model file as well.
///
///
/// Entity1
public static Account ReadItem(string id)
{
CrmService crmService = GetCRMService();
ColumnSet cols = new ColumnSet()
{
Attributes = new string[] { “accountid”, “name”, “parentaccountid”, “address1_city”,
“numberofemployees”,”creditonhold” }
};
account crmAccount = (account)crmService.Retrieve(EntityName.account.ToString(), new Guid(id), cols);
Account acc = new Account();
acc.AccountID = crmAccount.accountid.Value.ToString();
acc.Name = crmAccount.name;
acc.ParentAccount = (crmAccount.parentaccountid != null ? crmAccount.parentaccountid.name : string.Empty);
acc.City = crmAccount.address1_city;
acc.NoOfEmployees = (crmAccount.numberofemployees != null ? crmAccount.numberofemployees.Value : 0);
acc.CreditHold = (crmAccount.creditonhold != null ? crmAccount.creditonhold.Value : false);
return acc;
}
///
/// This is a sample specific method for Entity1. If you want to delete or rename the method think about changing the xml
/// in the BDC model file as well.
///
/// IEnumerable of Entity1
public static IEnumerable ReadList()
{
CrmService crmService = GetCRMService();
List accounts = new List();
QueryExpression query = new QueryExpression();
query.EntityName = EntityName.account.ToString();
query.ColumnSet = new ColumnSet() { Attributes = new string[] { “accountid”, “name”, “parentaccountid”, “address1_city”,
“numberofemployees”,”creditonhold” } };
RetrieveMultipleRequest req = new RetrieveMultipleRequest();
req.Query = query;
RetrieveMultipleResponse resp = (RetrieveMultipleResponse)crmService.Execute(req);
foreach (BusinessEntity be in resp.BusinessEntityCollection.BusinessEntities)
{
account crmAccount = (account)be;
Account acc = new Account();
acc.AccountID = crmAccount.accountid.Value.ToString();
acc.Name = crmAccount.name;
acc.ParentAccount = (crmAccount.parentaccountid != null ? crmAccount.parentaccountid.name : string.Empty);
acc.City = crmAccount.address1_city;
acc.NoOfEmployees = (crmAccount.numberofemployees != null ? crmAccount.numberofemployees.Value : 0);
acc.CreditHold = (crmAccount.creditonhold != null ? crmAccount.creditonhold.Value : false);
accounts.Add(acc);
}
return accounts;
}
public static Account Create(Account newAccount)
{
CrmService crmService = GetCRMService();
account acc = new account();
acc.name = newAccount.Name;
acc.address1_city = newAccount.City;
acc.creditonhold = new CrmBoolean() { name = “creditonhold”, Value = newAccount.CreditHold };
acc.numberofemployees = new CrmNumber() { Value = newAccount.NoOfEmployees };
Guid id = crmService.Create(acc);
newAccount.AccountID = id.ToString();
return newAccount;
}
public static void Update(Account account)
{
CrmService crmService = GetCRMService();
account acc = new account();
acc.accountid = new Key() { Value = new Guid(account.AccountID) };
acc.name = account.Name;
acc.address1_city = account.City;
acc.creditonhold = new CrmBoolean() { name = “creditonhold”, Value = account.CreditHold };
acc.numberofemployees = new CrmNumber() { Value = account.NoOfEmployees };
crmService.Update(acc);
}
public static void Delete(string accountID)
{
CrmService crmService = GetCRMService();
crmService.Delete(EntityName.account.ToString(), new Guid(accountID));
}
}
}
Regards
Prasad
LikeLike