Showing posts with label SilverLight. Show all posts
Showing posts with label SilverLight. Show all posts

Tuesday, March 13, 2012

Disassociate an Entity Record From Another Entity Record in Microsoft Dynamics CRM 2011 Using C# in Silverlight

This tutorial will show you how to disassociate an entity record from another entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

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

Here is the call in C#:

OrganizationRequest request = new OrganizationRequest() { RequestName = "Disassociate" };

//Target is the entity that you are associating your entities to.
EntityReference entrefTarget = new EntityReference();
entrefTarget.Id = new Guid("06AA44B1-336B-E111-B3CA-1CC1DEF1B5FF");
entrefTarget.LogicalName = "account";
request["Target"] = entrefTarget;

//RelatedEntities are the entities you are associating to your target (can be more than 1)
EntityReferenceCollection entrefcolCollection = new EntityReferenceCollection();
EntityReference entrefRelatedEntity1 = new EntityReference();
entrefRelatedEntity1.Id = new Guid("32B365C9-5F0C-E111-BF0B-1CC1DEE89AA8");
entrefRelatedEntity1.LogicalName = "contact";
entrefcolCollection.Add(entrefRelatedEntity1);
request["RelatedEntities"] = entrefcolCollection;


//The relationship schema name in CRM you are using to associate the entities. 
//found in settings - customization - entity - relationships
Relationship relRelationship = new Relationship();
relRelationship.SchemaName = "contact_customer_accounts";
request["Relationship"] = relRelationship;

//execute the request
IOrganizationService service = SilverlightUtility.GetSoapService();

//depending on how you do things your calls will most likely be asynchronous
service.BeginExecute(request, new AsyncCallback(Associate_Callback), service);


You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

Really though once you understand the differences in working with the two concepts it really isn't a tough nut to crack.

-

Monday, March 12, 2012

Associate an Entity Record with Another in Microsoft Dynamics CRM 2011 Using C# in Silverlight

This tutorial will show you how to associate an entity record to another entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

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

Here is the call in C#:

private void AssociateAccount(Guid guidResult)
{
    OrganizationRequest request = new OrganizationRequest() { RequestName = "Associate" };

    //Target is the entity that you are associating your entities to.
    EntityReference entrefTarget = new EntityReference();
    entrefTarget.Id = guidResult;
    entrefTarget.LogicalName = "account";
    request["Target"] = entrefTarget;
     
    //RelatedEntities are the entities you are associating to your target (can be more than 1)
    EntityReferenceCollection entrefcolCollection = new EntityReferenceCollection();
    EntityReference entrefRelatedEntity1 = new EntityReference();
    entrefRelatedEntity1.Id = new Guid("32B365C9-5F0C-E111-BF0B-1CC1DEE89AA8");
    entrefRelatedEntity1.LogicalName = "contact";
    entrefcolCollection.Add(entrefRelatedEntity1);
    request["RelatedEntities"] = entrefcolCollection;
    

    //The relationship schema name in CRM you are using to associate the entities. 
    //found in settings - customization - entity - relationships
    Relationship relRelationship = new Relationship();
    relRelationship.SchemaName = "contact_customer_accounts";
    request["Relationship"] = relRelationship;

    //execute the request
    IOrganizationService service = SilverlightUtility.GetSoapService();

    //depending on how you do things your calls will most likely be asynchronous
    service.BeginExecute(request, new AsyncCallback(Associate_Callback), service);
}

 private void Associate_Callback(IAsyncResult result)
{
    try
    {
        OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
        

        

    }
    catch (Exception ex)
    {
        ReportError(ex);
    }
}

You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

Really though once you understand the differences in working with the two concepts it really isn't a tough nut to crack.

Monday, February 27, 2012

Delete an Entity Record in Microsoft Dynamics CRM 2011 Using Silverlight

This tutorial will show you how to delete an entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

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

Here is the call in C#:

//delete request
OrganizationRequest request = new OrganizationRequest() { RequestName = "Delete" };
EntityReference entrefDeleteTarget = new EntityReference();
entrefDeleteTarget.Id = new Guid("A42D83C5-5E61-E111-AC43-1CC1DEF1B5FF");
entrefDeleteTarget.LogicalName = "account";
request["Target"] = entrefDeleteTarget;


IOrganizationService service = SilverlightUtility.GetSoapService();

service.BeginExecute(request, new AsyncCallback(AccountDelete_ClickCallback), service);

You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

- I hope this helps!!

Thursday, June 23, 2011

Using RetrieveMultiple From Silverlight to Retrieve Entities in Microsoft Dynamics CRM 2011

This tutorial will show you how to retrieve an entity records in Microsoft Dynamics CRM 2011 using C# in Silverlight using RetrieveMultiple.  This  particular call was put together by a colleague of mine named Stephen Walsh by starting from the other examples on my blog, his main interest is mobility so he told me I could use this code in my blog.

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 records in CRM using the following syntax.  In the following example I am retrieving a webresource record wit the name new_mywebresourcename.

Here is the call in C#:

ColumnSet Columns = new ColumnSet();
Columns.Columns = new System.Collections.ObjectModel.ObservableCollection<string>(new string[] { "content" });
QueryExpression Query = new QueryExpression();
Query.EntityName = "webresource";
Query.ColumnSet = Columns;
Query.Criteria = new FilterExpression
{
    FilterOperator = LogicalOperator.And,
    Conditions = 
    {
       new ConditionExpression
       {
           AttributeName = "name",
           Operator = ConditionOperator.Equal,
           Values = { "new_mywebresourcename" }
       }
    }
};
OrganizationRequest Request = new OrganizationRequest() { RequestName = "RetrieveMultiple" };
Request["Query"] = Query;
IOrganizationService Service = SilverlightUtility.GetSoapService();
Service.BeginExecute(Request, new AsyncCallback(GetCertificateResult), Service);

Now here is the call-back code:

private void GetCertificateResult(IAsyncResult Result)
{
    try
    {
        OrganizationResponse Response = ((IOrganizationService)Result.AsyncState).EndExecute(Result);
        EntityCollection EntityResult = (EntityCollection)Response["EntityCollection"];
        Entity wr = new Entity();
        wr = EntityResult.Entities[0];
        byte[] certbytes = Convert.FromBase64String(wr.Attributes[0].Value.ToString());
        X509Certificate MyCert = new X509Certificate(certbytes, "Certname");
        this.Dispatcher.BeginInvoke(DisplayCert);
    }
    catch (Exception ex)
    {
        throw 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 "DisplayCert" in this case because it could do anything in the UI.

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!

-

Wednesday, June 15, 2011

Get Current User ID and Organization ID from Microsoft Dynamics CRM 2011 In SIlverlight Using WhoAmIRequest

This tutorial will show you how to get the currently logged in User ID and Organization ID from Microsoft Dynamics CRM 2011 using C# in Silverlight with WhoAmIRequest

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 set up your call to the CRM Organization service reference.

Here is the call in C#:

private void GetOrganizationandUserID()
{
    try
    {

        OrganizationRequest request = new OrganizationRequest() { RequestName = "WhoAmI" };
        //request["Query"] = query;

        IOrganizationService service = SilverlightUtility.GetSoapService();

        service.BeginExecute(request, new AsyncCallback(CrmGetUserOrgInfo_Callback), service);
    }
    catch (Exception ex)
    {
        this.ReportError(ex);
    }
}

Now here is the call-back code:

private void CrmGetUserOrgInfo_Callback(IAsyncResult result)
{
    try
    {
        OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
        Guid OrgID = (Guid)response["OrganizationId"];
        Guid UserID = (Guid)response["UserId"];
        guidOrganizationID = OrgID;
        guidUserID = UserID;
        this.Dispatcher.BeginInvoke(DisplayOrgAndUserInfo);


    }
    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 "DisplayOrgAndUserInfo" 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 variables guidOrganizationID and guidUserID .

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!

-

Tuesday, June 14, 2011

Retrieve Entity in Microsoft Dynamics CRM 2011 From SIlverlight Using RetrieveRequest

This tutorial will show you how to retrieve an entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight with RetrieveRequest.

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!

-

Thursday, June 9, 2011

Create Entity in Microsoft Dynamics CRM 2011 from SIlverlight

This tutorial will show you how to create an entity record in Microsoft Dynamics CRM 2011 using C# in Silverlight.

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

Now once that is done you can create a record in CRM using the following syntax.  In the following example I am creating a record of type "new_testdata", which is a custom entity I have created.

Here is the call in C#:

//note that you need to specify the type of request here
OrganizationRequest request = new OrganizationRequest() { RequestName = "Create" };
Entity entity = new Entity();
entity.LogicalName = "new_testdata";

//One of the biggest things you deal with in SilverLight is the way you work with properties and Attributes
CrmSdk.KeyValuePair<string, object> attName = new CrmSdk.KeyValuePair<string, object>();
attName.Key = "new_name";
attName.Value = "Data - " + DateTime.Now.ToString();
entity.Attributes = new AttributeCollection();
entity.Attributes.Add(attName);

//request properties need to be explicitly named as strings
request["Target"] = entity; 

IOrganizationService service = SilverlightUtility.GetSoapService();

//depending on how you do things your calls will most likely be asynchronous
service.BeginExecute(request, new AsyncCallback(CrmCreate_Callback), service);

Since this is an asynchronous call my callback method "CrmCreate_Callback" that returns the entities Guid from the response looks like this:


private void CrmCreate_Callback(IAsyncResult result)
{
    try
    {
        OrganizationResponse response = ((IOrganizationService)result.AsyncState).EndExecute(result);
        Guid results = (Guid)response["id"];
        
        this.ReportMessage("id: " + results.ToString());
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

You will notice that the biggest change in thinking and syntax will be that you have to specify your request and response properties as strings explicitly in code.

Really though once you understand the differences in working with the two concepts it really isn't a tough nut to crack.