Showing posts with label Web Services. Show all posts
Showing posts with label Web Services. Show all posts

Wednesday, August 10, 2011

Connect To Microsoft Dynamics CRM 4.0 Online (and CRM 2011 Online?) Using Java

I will probably in the near future make this a goal of mine to revamp this for CRM 2011, but in the mean time I have found what appears to be a workable solution for CRM 4.0 Online that should that should also work for CRM 2011 for the time being because you can still use the old CRM 4.0 web services in CRM 2011.

Here is a link to the dynamics wrapper.  It's free and on GitHub, make sure to download the entire   Make sure you click the downloads button and get the zip file though so you get all the other files the main java file references also.

https://github.com/gooddata/GoodData-CL/tree/master/connector/src/main/java/com/gooddata/msdynamics

The credit for this library goes to GoodData Corporation.

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.

Tuesday, April 5, 2011

Get Full WSDL Schema For Organization Service in Microsoft Dynamics CRM 2011

When I first downloaded CRM 2011 I remember thinking it would be a lot of fun to take a crack at the new 2011 CRM web schemato see what all was in there and what the new schema looked like.  Much to my dissapointment there was very little in there.  Where were the entities?  Where were the new messages?  This frustration has repeated itself several times now in the forums as well.

It turns out that there is a quick 2 step process you can do to get exactly what you are looking for. 

Here is how it works:

1. First, download the WSDL for the Organization Service from Settings - Customizations - Developer Resources



Now, once you have saved the WSDL to a location open it in any XML editor or viewer.

 Examine the wsdl to find the <wsdl:import> tag and the url you want browse to obtain the entire WSDL will be in the "location=" attribute of the tag.

It appears to take this form:
http://<<servername>>/<<orgname>>/XRMServices/2011/Organization.svc?wsdl=wsdl0

Here is an example of finding it in my WSDL:




Now just browse to the location highlighted above and you will have the complete WSDL for the Organization Service.

I hope this helps!

Monday, October 13, 2008

Get User Roles from User ID Using C# in Microsoft CRM 4.0

It is fairly easy to use the web service to get the roles for a particular user based on their GUID.
This methodology is based on a post by Jim Wang at http://jianwang.blogspot.com/2008/01/crm-40-check-current-users-security.html in client-side jscript ; there isn’t much good information out on the web on how to do this in C#, and it is a bit more difficult than a normal retrievemultiple call considering it is a many-to-many relationship, so… here it is!

On a side note, congratulations Jim on getting awarded with the honor of CRM MVP.

public BusinessEntityCollection GetUserRoles(Guid guidUserID, ICrmService serv)
{
try
{
QueryExpression qe = new QueryExpression();
qe.EntityName = "role";
qe.ColumnSet = new AllColumns();

LinkEntity le = new LinkEntity();
le.LinkFromEntityName = "role";
le.LinkFromAttributeName = "roleid";
le.LinkToEntityName = "systemuserroles";
le.LinkToAttributeName = "roleid";

ConditionExpression ce = new ConditionExpression();
ce.AttributeName = "systemuserid";
ce.Operator = ConditionOperator.Equal;
ce.Values = new object[] { guidUserID };

LinkEntity le2 = new LinkEntity();
le2.LinkFromEntityName = "systemuserroles";
le2.LinkFromAttributeName = "systemuserid";
le2.LinkToEntityName = "systemuser";
le2.LinkToAttributeName = "systemuserid";

le2.LinkCriteria = new FilterExpression();
le2.LinkCriteria.Conditions.Add(ce);

le.LinkEntities.Add(le2);
qe.LinkEntities.Add(le);

RetrieveMultipleRequest rmRequest = new RetrieveMultipleRequest();
rmRequest.ReturnDynamicEntities = true;
rmRequest.Query = qe;

RetrieveMultipleResponse response = (RetrieveMultipleResponse)serv.Execute(rmRequest);

if (response.BusinessEntityCollection.BusinessEntities.Count > 0)
{
return response.BusinessEntityCollection;
}
else
{
return null;
}
}
catch (Exception ex)
{

WriteToFile("error: " + ex.ToString());

return null;

}
}

This post is provided as-is and implies no warranty; Jamie Miley does not assume any responsibility for problems arising from the use of this information.

Monday, October 6, 2008

Programmatically rendering a SQL Server Reporting Services (SSRS) report from Microsoft CRM 4.0 and capturing output in a file.

Programmatically running a SQL Server Reporting Services report and capturing output in a file for use from CRM.


 


It is possible in CRM to use SQL Server Reporting Services in order to run a report and capture the output in a file.  The first problem is that you only see GUIDs listed for CRM reports under the SSRS report manager.  To actually be able to run the report on the report server from the CRM report name you must publish the report for external use. 


 


1.       Go to “Workplace” and then “Reports” in CRM and click on the report you want to edit and click “Edit Report”.


2.       Now click “Actions” and then “Publish Report for External Use”


 



 


 If you now go into Sql Report Manager at http://myreportserver.mydomain.com/reports and go to your specific org database name, you will see the report you published for external use and any sub reports that are utilized by it.  This now allows you to use the web-service and call it by name.


 



 


 


Now, create a plug-in or other application you want to run the CRM report.


In Visual Studio, make a web service reference to the SQL Server Reporting Services web service(. And use something similar to the following snippet of code to render the file and save the output to a file.


If you are using a separate application you would probably want to hardcode credentials, but if you were doing this in a plug-in or custom workflow assembly you would want to use the default credentials.


I also left some commented out code in this example to show how to populate report parameters.


 




ReportExecutionService rs = new ReportExecutionService();

 

//rs.Credentials = System.Net.CredentialCache.DefaultCredentials;

rs.Credentials = new System.Net.NetworkCredential("username", "password", "domain");

rs.Url = "http://MySqlServer.MyDomain.com/reportserver/ReportExecution2005.asmx";

 

// Render arguments

byte[] result = null;

// Make sure you use your correct org database name of the following line

string reportPath = "/jmiley_MSCRM/Account Overview";

string format = "PDF";

string historyID = null;

string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

 

// Prepare report parameter.

//ParameterValue[] parameters = new ParameterValue[3];

//parameters[0] = new ParameterValue();

//parameters[0].Name = "EmpID";

//parameters[0].Value = "288";

//parameters[1] = new ParameterValue();

//parameters[1].Name = "ReportMonth";

//parameters[1].Value = "6"; // June

//parameters[2] = new ParameterValue();

//parameters[2].Name = "ReportYear";

//parameters[2].Value = "2004";

 

DataSourceCredentials[] credentials = null;

string showHideToggle = null;

string encoding;

string mimeType;

string extension;

Warning[] warnings = null;

ParameterValue[] reportHistoryParameters = null;

string[] streamIDs = null;

ExecutionInfo execInfo = new ExecutionInfo();

ExecutionHeader execHeader = new ExecutionHeader();

rs.ExecutionHeaderValue = execHeader;

execInfo = rs.LoadReport(reportPath, historyID);

//rs.SetExecutionParameters(parameters, "en-us");
String SessionId = rs.ExecutionHeaderValue.ExecutionID;

Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);

try
{
result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

    execInfo = rs.GetExecutionInfo();
Console.WriteLine("Execution date and time: {0}",    execInfo.ExecutionDateTime);

 }
 catch (SoapException err)
 {
    Console.WriteLine(err.Detail.OuterXml);
 }
 // Write the contents of the report to an PDF file.
 try
 {
    FileStream stream = File.Create(@"c:\upload_report.pdf", result.Length);
    Console.WriteLine("File created.");
    stream.Write(result, 0, result.Length);
    Console.WriteLine("Result written to the file.");
    stream.Close();
  }
  catch (Exception error)
  {
     Console.WriteLine(error.Message);
  }

You can view full documentation on the SSRS web service from MSDN at:


http://msdn.microsoft.com/en-us/library/ms152787(SQL.90).aspx


 


Instead of PDF files, you can also create other types of files by changing the format string:


-          MHT web archive – “MHTML”


-          XML – “XML”


-          CSV – “CSV”


-          IMAGE – “IMAGE”


-          EXCEL – “EXCEL”


-          HTML – “HTML4.0”, “HTML3.2”, “HTMLOWC”


 


There are a few things to consider:


 


1.       The file copy operation above will use the app domain’s credentials for creating the output file.


If you are using a plug-in or workflow assembly this will be the CRM app pool security account.


If you want the file to be created on a different server, you may want to change the app pool security account to an Active Directory account instead of the default “network service”.


 


2.       Default credentials should be used for the web-service for plug-ins and workflow assemblies as it uses CRM security and will not let people access data through the reports that they do not have read access to in CRM.



 

This post is provided as-is and implies no warranty; Jamie Miley does not assume any responsibility for problems arising from the use of this information.