locked
how to create a Note Using Plugins In Microsoft Dynamics CRM 2013 RRS feed

  • Question

  • hi all am trying to create note when account Creates using Plugin ,i dont want to attach any files in Notes 

    Just i want to trying a adding a Note text .when account Create NoteText also Creates ,

    Please Find the Code:

     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    // Obtain the target entity from the input parameters.
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "account")
                    {
                        try
                        {
                            string strMessage = "this is a demo";
                            byte[] filename = Encoding.ASCII.GetBytes(strMessage);
                            string encodedData = System.Convert.ToBase64String(filename);
                            Entity Annotation = new Entity("annotation");
                            Annotation.Attributes["objectid"] = new EntityReference("account", new Guid("9ed7e828-fa70-e411-ba71-5453ed23c515"));
                            Annotation.Attributes["objecttypecode"] = 1;
                            Annotation.Attributes["subject"] = "account Demo File";
                            Annotation.Attributes["documentbody"] = Convert.ToBase64String(new UnicodeEncoding().GetBytes("Sample Annotation Text"));
                            Annotation.Attributes["mimetype"] = @"text/plain";
                            Annotation.Attributes["notetext"] = "Sample attachment.";
                           // Annotation.Attributes["filename"] = "Demo.txt";
                            service.Create(Annotation);
                        }

    Presently am Using CRM 2013 

    Please Give Some Suggestion 

    thank U 

    Friday, January 2, 2015 12:34 PM

Answers

  • Hi 

     Please Try this Code

     
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.ServiceModel;
    using Microsoft.Xrm.Sdk;
    using Xrm;
    
    public class Plugin: IPlugin
    {
    public void Execute(IServiceProvider serviceProvider)
    {
    IPluginExecutionContext context = (IPluginExecutionContext)
    serviceProvider.GetService(typeof(IPluginExecutionContext));
    
    Entity entity;
    
    // Check if the input parameters property bag contains a target
    // of the create operation and that target is of type Entity.
    if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
    {
    // Obtain the target business entity from the input parameters.
    entity = (Entity)context.InputParameters["Target"];
    
    // Verify that the entity represents a contact.
    if (entity.LogicalName != "contact") { return; }
    }
    else
    {
    return;
    }
    
    try
    {
    IOrganizationServiceFactory serviceFactory = 
        (IOrganizationServiceFactory)serviceProvider.GetService(
    typeof(IOrganizationServiceFactory));
    IOrganizationService service = 
    serviceFactory.CreateOrganizationService(context.UserId);
    
    var id = (Guid)context.OutputParameters["id"];
    
    AddNoteToContact(service, id);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
    throw new InvalidPluginExecutionException(
    "An error occurred in the plug-in.", ex);
    }
    }
    
    private static void AddNoteToContact(IOrganizationService service, Guid id)
    {
    using (var crm = new XrmServiceContext(service))
    {
    
    var contact = crm.ContactSet.Where(
    c => c.ContactId == id).First();
    Debug.Write(contact.FirstName);
    
    var note = new Annotation
    {
    Subject = "Created with plugin",
    NoteText = "This Note was created by the example plug-in",
    ObjectId = contact.ToEntityReference(),
    ObjectTypeCode = contact.LogicalName
    };
    
    crm.AddObject(note);
    crm.SaveChanges();
    }
    }
    }
    • Marked as answer by katikidala Saturday, January 3, 2015 7:54 AM
    Saturday, January 3, 2015 7:54 AM