Initializing and setting DefaultCrendentials for OrganizationService in CRM 2011


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

  1. Microsoft.Xrm.Sdk.
  2. System.ServiceModel.
  3. 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);
}
}

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

One thought on “Initializing and setting DefaultCrendentials for OrganizationService in CRM 2011”

  1. 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

    Like

Please share your thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

CRM for You and Me

Customer Relationship Mgt App for the Professional

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog

Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Knowhere365

Specific topics by Django Lohn on the whole Microsoft365 Stack

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue MVP

A Microsoft Business Applications MVP

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

Two Bite Tips

Valuable Tips To Grow Your Business

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.

CRM Keeper

Dynamics 365 Customer Engagement, CRM, Microsoft CRM, Dynamics CRM

EVOLVED365

Step into the world of a Dynamics 365 Consultant

Dianamics PCF Lady

Diana & Dynamics 365 & Power Platform

Sara Lagerquist

No Code Customization Concepts

innovativeaj

developer, designer, writer, fun loving, patriotic, humble and a sweet person inside out :) love and respect people who have a great sense of humor.

Temmy Wahyu Raharjo

Dreaming to be a clean coder and TDD minded programmer.

Transform 365

We blog about problems we face and code we write to help others

%d bloggers like this: