IMPORTANT: First things first. You have to set up your Silverlight app to make a web services connection to CRM. The best tutorial I have found for this is located here in the MSDN:
http://msdn.microsoft.com/en-us/library/gg594452.aspx
After you finish that setup you are ready to implement the rest of this post.
Now once that is done you can retrieve a record in CRM using the following syntax. In the following example I am retrieving a record of type "new_testdata", which is a custom entity I have created.
Here is the call in C#:
private void RetrieveEntity()
{
try
{
EntityReference refEntity = new EntityReference();
//the guid of the entity you want to retrieve
refEntity.Id = new Guid("1C49A4FD-0B96-E011-8D5C-1CC1DEE8EA49");
//the name of the entity type you want to retrieve
refEntity.LogicalName = "new_testdata";
//define request type
OrganizationRequest request = new OrganizationRequest() { RequestName = "Retrieve" };
request["Target"] = refEntity;
ColumnSet columns = new ColumnSet();
//put comma delimited list of attributes you want to retrieve here
columns.Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] { "new_name", "new_otherattribute" });
request["ColumnSet"] = columns;
//the silverlight utility class is created during the setup process laid out at http://msdn.microsoft.com/en-us/library/gg594452.aspx
IOrganizationService service = SilverlightUtility.GetSoapService();
//send the async request and specify it's callback
service.BeginExecute(request, new AsyncCallback(RetrieveEntityResult), service);
}
catch (Exception ex)
{
this.ReportError(ex);
}
}
Now here is the call-back code:
private void RetrieveEntityResult(IAsyncResult result)
{
try
{
OrganizationResponse Response = ((IOrganizationService)result.AsyncState).EndExecute(result);
Entity e = (Entity)Response["Entity"];
//strEntityName is a global variable I am using in my program so I can call it in the //dispatcher method below
strEntityName = "entity name = " + e.GetAttributeValue<string>("new_name");
//call a method that does something in the main UI thread (update the UI with the results of the call)
//from method named "method" in this case
this.Dispatcher.BeginInvoke(method);
}
catch (Exception ex)
{
this.ReportError(ex);
}
}
You will need to use the this.Dispatcher.BeginInvoke to call a method to act on the UI or perform actions in the main thread. I did not include the dispatcher invoked method called "method" in this case because it could do anything in the UI. In my case I will tell you though it is setting a TextBox.Text property to the value of the global variable "strEntityName".
You will notice that the biggest change in thinking and syntax from standard CRM SDK work will be that you have to specify your request and response properties as strings explicitly in code.
I hope this helps!
-
How can I return a value from the callback function?
ReplyDeleteI am trying to retrieve entity guid by processing the result that I get in the callback function.
I want to return this entity guid to the main function. How can I do it?
My code looks like this.
public Guid RetrieveEntityGuid()
{
Guid id = new Guid();
...
...
...
...
...
...
service.BeginExecute(request, new AsyncCallback(RetrieveEntityGuid_Callback), service);
return id;
}
public void RetrieveEntityGuid_Callback(IAsyncResult result)
{
Guid id = new Guid();
OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
EntityCollection results = (EntityCollection)response["EntityCollection"];
foreach (var r in results.Entities)
{
id = (Guid)r.Attributes[0].Value;
}
}
So, I want to return the id that I get in this callback function to my main function (RetrieveEntityGuid).
Thanks in advance.
please ,provide me the answer of this problem as I also facing the same issue in Async CTP call of this method.
ReplyDeleteEmail me on Tejas@elantechnologies.com
That is the point of the line:
Delete"this.Dispatcher.BeginInvoke(method);"
You need to replace "method" with the method you want it to call, and in that method you can interact with the UI thread.