C# Code to Create Lead from WEB and Attach a Note To Lead in MS CRM 2011

C# Code to Create Lead from WEB and Attach a Note To Lead in MS CRM 2011

To create Web Lead Check My Post “Custom Aspx Page for MS CRM 2011”

To attach Note To Lead use the folloing code.


void btnSubmit_Click(object sender, EventArgs e)


IOrganizationService service =PCI.GetOrgService();

QueryExpression qe = new QueryExpression(“lead”);

ColumnSet set = new ColumnSet(new string[] { “leadid” });

qe.ColumnSet = set;

ConditionExpression cond = new ConditionExpression(“pci_referencenumber”, ConditionOperator.Equal, txtRefNumber.Text);

FilterExpression fl = new FilterExpression();



EntityCollection ec = service.RetrieveMultiple(qe);

if (ec.Entities.Count > 0)


foreach (Entity result in ec.Entities)


//string refNumber = result.GetAttributeValue<string>(“pci_referencenumber”);

leadid = result.GetAttributeValue<




filename =


FileUploadControl.SaveAs(“C:/” + filename);

if (FileUploadControl.HasFile)




Annotation annotation = new Annotation()


Subject =“Example Annotation”,

FileName = filename,

DocumentBody =Convert.ToBase64String(new UnicodeEncoding().GetBytes(filename)),

MimeType =“text/plain”,


_annotationId = service.Create(annotation);

AssociateRequest teamToProfile = new AssociateRequest


Target =new EntityReference(Lead.EntityLogicalName, leadid),

RelatedEntities =new EntityReferenceCollection


                 new EntityReference(Annotation.EntityLogicalName, _annotationId)


Relationship =new Relationship(“Lead_Annotation”)


// Execute the request.


StatusLabel.Text =“Upload status: The file uploaded Successfully”;


catch (Exception ex)


StatusLabel.Text =“Upload status: The file could not be uploaded. The following error occured: “ + ex.Message;







Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s