In this article, I am trying to explain how to Create, Update, Delete, Retrieve using Organization service.
To use Organization service , Add Organization.svc Service Reference in your Visual studio Solution
In First Step I am declaring and Initializing IOrganizationService Object, To Inatalizing Object i am using some functions
IOrganizationService service= GetSoapService();
//Function for object Initalization
public static IOrganizationService GetSoapService()
{
Uri serviceUrl = CombineUrl(GetServerBaseUrl(), "/XRMServices/2011/Organization.svc/web");
BasicHttpBinding binding = new BasicHttpBinding(Uri.UriSchemeHttps == serviceUrl.Scheme
? BasicHttpSecurityMode.Transport : BasicHttpSecurityMode.TransportCredentialOnly);
binding.MaxReceivedMessageSize = int.MaxValue;
binding.MaxBufferSize = int.MaxValue;
binding.SendTimeout = TimeSpan.FromMinutes(2);
return new SilverCrmSoap.CrmSdk.OrganizationServiceClient(binding, new EndpointAddress(serviceUrl));
}
public static Uri GetServerBaseUrl()
{
//comment before using as webResource
string serverUrl = "http://crm2011:5555/ITSoft";
//Uncomment before using as webResource
//string serverUrl = (string)GetContext().Invoke("getServerUrl");
if (serverUrl.EndsWith("/"))
serverUrl = serverUrl.Substring(0, serverUrl.Length - 1);
return new Uri(serverUrl);
}
public static Uri CombineUrl(Uri baseValue, string value)
{
if (null == baseValue)
{
throw new ArgumentNullException("baseValue");
}
else if (string.IsNullOrEmpty(value))
{
return baseValue;
}
//Ensure that a double '/' is not being added
string newValue = baseValue.AbsoluteUri;
if (!newValue.EndsWith("/", StringComparison.Ordinal))
{
//Check if there is a character at the beginning of value
if (!value.StartsWith("/", StringComparison.Ordinal))
{
newValue += "/";
}
}
else if (value.StartsWith("/", StringComparison.Ordinal))
{
value = value.Substring(1);
}
//Create the combined URL
return new Uri(newValue + value);
}
Code to Create Account
void CreateAccount()
{
Entity entity = new Entity();
entity.LogicalName = "account";
entity.Id = new Guid(); //if we will not pass, API will create
entity["name"] = "Create Account using ORganization Service";
service.BeginCreate(entity, new AsyncCallback(CreateAccount), service);
}
void CreateAccount(IAsyncResult result)
{
Guid accoutid = ((IOrganizationService)result.AsyncState).EndCreate(result);
}
Code to Update Account
void UpdateAccount(Guid AccountID)
{
Entity entity = new Entity();
entity.LogicalName = "account";
entity.Id = AccountID;
entity["name"] = "Update Account using ORganization Service";
service.BeginUpdate(entity, new AsyncCallback(UpdateAccount), service);
}
void UpdateAccount(IAsyncResult result)
{
((IOrganizationService)result.AsyncState).EndUpdate(result);
}
Code to Delete Account
public void DeleteAccount(Guid AccountID)
{
service.BeginDelete("account", AccountID, new AsyncCallback(DeleteAccount), service);
}
private void DeleteAccount(IAsyncResult result)
{
((IOrganizationService)service).EndDelete(result);
}
Code to Retrieve Account
void RetrieveAccount(Guid AccountID)
{
ColumnSet _cols = new ColumnSet()
{
Columns = new ObservableCollection<string>(new string[] { "parentcustomerid", "accountid" })
//AllColumns = true
};
service.BeginRetrieve("account", AccountID, new AsyncCallback(RetrieveAccount), service);
}
private void RetrieveAccount(IAsyncResult result)
{
Entity _account = ((IOrganizationService)service).EndRetrieve(result);
}
Code to Retrieve Multiple Account
void RetrieveMultipleAccount(Guid AccountID)
{
QueryExpression query = new QueryExpression()
{
EntityName = "account",
ColumnSet = new ColumnSet()
{
AllColumns = true
},
Criteria = new FilterExpression()
{
Conditions = new ObservableCollection<ConditionExpression>(
new ConditionExpression[]
{
new ConditionExpression(){
AttributeName="parentcustomerid",
Operator = ConditionOperator.Equal,
Values = new ObservableCollection<object>{AccountID}
}
})
},
};
OrganizationRequest _OrgRequest = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
_OrgRequest["Query"] = query;
service.BeginExecute(_OrgRequest, new AsyncCallback(RetrieveMultipleAccount), service);
}
void RetrieveMultipleAccount(IAsyncResult result)
{
OrganizationResponse response = ((IOrganizationService)service).EndExecute(result);
EntityCollection results = (EntityCollection)response["EntityCollection"];
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.