Code Snippet
protected void SendToCRM(string documentBody)
{
string orgName = String.Empty;
// setup the discovery service
CrmDiscoveryService crmDiscoveryService = new CrmDiscoveryService();
crmDiscoveryService.UseDefaultCredentials =
true;
crmDiscoveryService.Url =
ConfigurationManager.AppSettings["discoveryServicePath"];
// retrieve the organization name and endpoint Url from the rmDiscoveryService web service
RetrieveOrganizationsRequest orgRequest = new RetrieveOrganizationsRequest();
RetrieveOrganizationsResponse orgResponse = (RetrieveOrganizationsResponse)crmDiscoveryService.Execute(orgRequest);
OrganizationDetail orgInfo = null;
foreach (OrganizationDetail orgDetail in orgResponse.OrganizationDetails)
{
if (orgDetail.OrganizationName.Equals(orgName))
{
orgInfo = orgDetail;
break;
}
}
if (orgInfo == null)
throw new Exception("The organization name is invalid.");
// create and configure an instance of the CrmService web service
CrmAuthenticationToken token = new CrmAuthenticationToken();
token.AuthenticationType =
AuthenticationType.AD;
token.OrganizationName = orgInfo.OrganizationName;
CrmService crmService = new CrmService();
crmService.Url = orgInfo.CrmServiceUrl;
crmService.CrmAuthenticationTokenValue = token;
crmService.Credentials =
new System.Net.NetworkCredential("name", "password");
// invoke CrmService web service methods
WhoAmIRequest whoRequest = new WhoAmIRequest();
WhoAmIResponse whoResponse = (WhoAmIResponse)crmService.Execute(whoRequest);
// change the url of the CrmService
//crmService.Url = ConfigurationManager.AppSettings["crmServicePath"];
// setup the annotation
annotation anno = new annotation();
anno.objectid =
CrmTypes.CreateLookup("Opportunity", new Guid("3BD650B4-82F5-49F4-9899-004F4A700AB2"));
anno.mimetype =
"application/xml";
anno.filename =
"TestFileName.xml";
// anno.filesize = 5000;
anno.documentbody =
Convert.ToBase64String(Encoding.UTF8.GetBytes(documentBody));
anno.objecttypecode =
new EntityNameReference("Opportunity");
anno.subject =
"SubjectOfTheAttachment";
anno.isdocument =
new CrmBoolean(true);
// save the annotation
crmService.Create(anno);
}