Integrate with portal to CRM 2011 with File Attachement

--------btn FIle Upload

protected void upload_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.FileName == string.Empty)
        {
            MessageBox("Please Upload the File");
            loanDetails.Visible = false;
            return;
        }
        else
        {
            //save the file
            //restrict user to upload other file extenstion
            string[] FileExt = FileUpload1.FileName.Split('.');
            string FileEx = FileExt[FileExt.Length - 1];
            int dd=0;
            if (FileEx.ToLower() == "csv")
            {
                var random = new Guid().ToString().Split('-')[0];
                Random ran = new Random();
                int no = ran.Next();

                FileUpload1.SaveAs(Server.MapPath("CSVLoad//" + FileUpload1.FileName));
                //string fileName = ".csv";
                //string fileUpload = FileUpload1+no.ToString();
                //FileUpload1.SaveAs(Server.MapPath("CSVLoad//" + fileUpload + fileName));
                Guid estId = new Guid(Request["estId"]);
                uploadFile(FileUpload1.PostedFile, "TExt: " + Session["spName"], "newentity", estId);
                loanDetails.Visible = true;
                readCSV(true);
            }
            else
            {
                MessageBox("Please Upload the CSV File");
                return;
            }
        }
    }

------File Upload
 private Guid uploadFile(HttpPostedFile file, string title, string entityName, Guid guid)
    {
        Entity ann = new Entity("annotation");

        byte[] fileBytes;
        using (var br = new BinaryReader(file.InputStream))
        {
            fileBytes = br.ReadBytes(file.ContentLength);
        }

        ann.Attributes.Add("subject", title);
        ann.Attributes.Add("filename", file.FileName);
        ann.Attributes.Add("documentbody", Convert.ToBase64String(fileBytes));
        ann.Attributes.Add("objectid", new EntityReference(entityName, guid));

        return clsXrmConnection._service().Create(ann);
    }

-------------btn Submit

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            readCSV(true);
         
        }
        catch (Exception)
        {
         
            throw;
        }
    }








protected bool readCSV(bool isSubmit)
    {
        try
        {
            List<string> outPut = new List<string>();
            string id;
            //if (listBoxApp.Text == "" || listBoxApp.Text == null) return false;
            //string fileStr = listBoxApp.Text;
            //string[] arrLines = fileStr.Split(new string[] {"\r\n"}, StringSplitOptions.None);
            string fileStr = File.ReadAllText(Server.MapPath("CSVLoad//" + FileUpload1.FileName));
            //string[] arrLines = fileStr.Split('\n');
            string[] arrLines = fileStr.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            
            Guid estID = new Guid(Request["userId"]);
            for (var i = 0; i < arrLines.Length; i++)
            {
                arrVals = arrLines[i].Split(',');
                est = new Entity("application");
                id = arrVals[0];
                try
                {
                    defaultVals.Add(decimal.Parse(arrVals[1]));
                }
                catch (Exception)
                {

                    defaultVals.Add(-1);
                    err.Add("");
                }
                QueryExpression q = new QueryExpression()
                {
                    EntityName = "mis_establishmentapplication",
                    ColumnSet = new ColumnSet(true),
                    Criteria = new FilterExpression(LogicalOperator.And)
                };
                q.Criteria.AddCondition("mis_serviceprovidercontractid", ConditionOperator.Equal, estID);
                q.Criteria.AddCondition("new_id", ConditionOperator.Equal, id);

                _coll = clsXrmConnection._service().RetrieveMultiple(q);
                if (_coll.Entities.Count == 1)
                {
                    foundIds.Add(_coll[0].Id);
                    compelte.Add(arrLines[i]);
                }
                else
                {
                    foundIds.Add(Guid.Empty);
                    err.Add(arrLines[i]);
                }
            }
            estApplication.InnerHtml = "";
           
        }
        catch (Exception)
        {
            
            throw;
        }

        if (isSubmit)
        {
            //if (err.Count != 0)
            //{
            //    MessageBox("Please check the its should be 9 digit");
            //   // return false;
            //}
            //else
            //{
                for (int i = 0; i < foundIds.Count; i++)
                {
                    newGUID = (Guid)foundIds[i];
                    if (newGUID != Guid.Empty)
                    {
                        Entity estEnt = clsXrmConnection._service().Retrieve(est.LogicalName, newGUID , new ColumnSet(new string[] { "mis_defaultvalue" }));
                        xrm.set(fieldTypes.Money, estEnt, "entity", defaultVals[i].ToString());
                        clsXrmConnection._service().Update(estEnt);
                    }
                }
            //}
            
        }
//Here use Dynamicsa Table Called estApplication
        foreach (var ent in err)
        {
            string Value = "N/A";
            string[] arrValsErro = ent.Split(',');
            if(arrValsErro[0]!="")
            {
                if (arrValsErro.Length > 0)
                {
                    if (arrValsErro.Length >= 1)
                    {
                        string id = arrValsErro[0];
                        if (arrValsErro.Length >= 2)
                        {
                            Value = arrValsErro[1].ToString();
                        }
                        estApplication.InnerHtml += "<tr>";
                        estApplication.InnerHtml += "<td align='left'>" + id + "</td>";
                        estApplication.InnerHtml += "<td align='left'>" + Value + "</td>";
                        estApplication.InnerHtml += "<td align='left'>" + "InValid" + "</td>";
                        estApplication.InnerHtml += "</tr>";
                    }
                }
            }
        }
        foreach (var ent in compelte)
        {
            string Value = "N/A";
            string[] arrValsErro = ent.Split(',');
            if (arrValsErro[0] != "")
            {
                if (arrValsErro.Length > 0)
                {
                    if (arrValsErro.Length >= 1)
                    {
                        string id = arrValsErro[0];
                        if (arrValsErro.Length >= 2)
                        {
                            Value = arrValsErro[1].ToString();
                        }
                        estApplication.InnerHtml += "<tr>";
                        //estApplication.InnerHtml += "<a href='financeDetails.aspx?appId=" + estGUID.ToString() + "&rrr=" + Request["rr"] + "'>";
                        estApplication.InnerHtml += "<td align='left'>" + id;
                        //estApplication.InnerHtml += "</a>";
                        estApplication.InnerHtml += "</td>";
                        estApplication.InnerHtml += "<td align='left'>" + Value + "</td>";
                        estApplication.InnerHtml += "<td align='left'>" + "Valid" + "</td>";
                        estApplication.InnerHtml += "</tr>";
                    }
                }
            }
        }
        return false;
    }

 protected void MessageBox(string msg)
    {
        Literal str_message = new Literal();
        str_message.Mode = LiteralMode.PassThrough;
        str_message.Text = "<br />" + msg;
        this.Page.Controls.Add(str_message);
    }

Comments