Thursday, August 7, 2008

Create Note and Assign to a Entity.

Sample code which will create a note and attach to the specified Entity (Specify Entity Name and GUID of the Entity Record)

CrmService service = GetCrmService("", "ORGANIZATION", "USERNAME", "PASSWORD", "DOMAIN");
annotation note = new annotation();
note.subject = "NOTE SUBJECT";
note.filename = @"schema.xsd";
note.mimetype = "text/html";
string encodedData = string.Empty;
using (FileStream fs = new FileStream(@"c:\schema.xsd", FileMode.Open, FileAccess.Read))
byte[] byteData = new byte[fs.Length];
fs.Read(byteData, 0, byteData.Length);
encodedData = System.Convert.ToBase64String(byteData);
note.documentbody = encodedData;
note.objectid = new Lookup();
note.objectid.type = "SPECIFY ENTITY_NAME";
note.objectid.Value = new Guid("F27A627B-DF1A-DD11-AEA5-0003FF90FBC7");
note.objecttypecode = new EntityNameReference();
note.objecttypecode.Value = "SPECIFY ENTITY_NAME";
Guid annotationId = service.Create(note); 

---- HERE you can Initialize CRM SERVICE Object.....

public static CrmService GetCrmService(string crmServerUrl, string organizationName, string userName, string password, string domain)
    // Get the CRM Users appointments
    // Setup the Authentication Token
    CrmAuthenticationToken token = new CrmAuthenticationToken();
    token.OrganizationName = organizationName;
    CrmService service = new CrmService();
    if (crmServerUrl != null &&
    crmServerUrl.Length > 0)
        UriBuilder builder = new UriBuilder(crmServerUrl);
        builder.Path = "//MSCRMServices//2007//CrmService.asmx";
        service.Url = builder.Uri.ToString();
    service.Credentials = new System.Net.NetworkCredential(userName, password, domain);
    service.CrmAuthenticationTokenValue = token;
    return service;

Happy Coding....


Jeetu said...

I'm using MSCRM 4.0, I'm unable to execute the Plug-in in my application it is throwing an error as "The request failed with HTTP status 401: Unauthorized". Do you have any idea why I'm getting this.
Please reply asap.

Thanks in advance.

Ranjitsingh Raghuwanshi said...

What kind of authentication mechanism are you using when trying to connect to CRM Service?


pearse said...

is there a way to hide the-
notes created on.. by ... and edited by ...- all those details which appears near the title in notes in opportunity