String Builder using C# in SharePoint 2013

 string imagepath;
        string attachmentAbsoluteURL;
        private void GetClientTestimonial()
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Attachments", typeof(string));
            dt.Columns.Add("Title", typeof(string));
            dt.Columns.Add("Description", typeof(string));
            dt.Columns.Add("htmlable");
            using (SPSite osite = new SPSite(SPContext.Current.Web.Url))
            {
                using (SPWeb oweb = osite.OpenWeb())
                {
                    string currentYears = DateTime.Now.Year.ToString();
                    SPList listclienttestmonial = oweb.Lists.TryGetList("Client Testimonials");
                    SPQuery qurryclienttestmonialcollection = new SPQuery();
                    string stringQueryclienttestmonial = string.Format(@"<Where><Eq><FieldRef Name='FiscalYear' /><Value Type='Lookup'>{0}</Value></Eq></Where>", currentYears);
                    qurryclienttestmonialcollection.Query = stringQueryclienttestmonial;
                    SPListItemCollection collListItems = listclienttestmonial.GetItems(qurryclienttestmonialcollection);
                    StringBuilder sb = new StringBuilder();
                    if (collListItems.Count > 0)
                    {
                        foreach (SPListItem item in collListItems)
                        {
                            DataRow dr = dt.NewRow();
                            string attachmentAbsoluteURL = string.Empty;
                            if (item.Attachments.Count > 0)
                            {
                                foreach (String attachmentname in item.Attachments)
                                {
                                    attachmentAbsoluteURL = item.Attachments.UrlPrefix + attachmentname;
                                    dr["Attachments"] = attachmentAbsoluteURL.ToString();
                                }
                            }
                            else
                            {
                                dr["Attachments"] = "/_layouts/15/VrtxIntranetStyles/Images/NoImage.jpg";
                            }
                            dr["Title"] = item["Title"] != null ? item["Title"].ToString() : string.Empty;
                            dr["Description"] = item["Description"] != null ? item["Description"].ToString() : string.Empty;

                            SPFieldUser userField = (SPFieldUser)item.Fields.GetField("Team");
                            userFieldValueCollection = (SPFieldUserValueCollection)userField.GetFieldValue(item["Team"].ToString());

                            foreach (SPFieldUserValue user in userFieldValueCollection)
                            {
                                string uservalue = user.User.ToString().Split('\\')[1];
                                SPList empbasicinfolist = oweb.Lists.TryGetList("EmpBasicInfo");
                                SPQuery q = new SPQuery();
                                string stringQuery = string.Format(@"<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>{0}</Value></Eq></Where>", uservalue);
                                q.Query = stringQuery;
                                SPListItemCollection employebasicinfocollListItems = empbasicinfolist.GetItems(q);
                                foreach (SPListItem items in employebasicinfocollListItems)
                                {
                                    if (items.Attachments.Count > 0)
                                    {
                                        foreach (String attachmentname in items.Attachments)
                                        {
                                            attachmentAbsoluteURL = items.Attachments.UrlPrefix + attachmentname;
                                            imagepath = attachmentAbsoluteURL.ToString();
                                        }
                                    }
                                    else
                                    {
                                        imagepath = "/_layouts/15/VrtxIntranetStyles/Images/NoImage.jpg";
                                    }
                                    string Name = items["UserName"] != null ? items["UserName"].ToString() : string.Empty;
                                    sb.Append("<div> ");
                                    sb.Append("<table style=\"float:left\">");
                                    sb.Append("<tr>");
                                    sb.AppendFormat("<td style=\"padding-left: 10px;\">{0}</td>", "<img src='" + imagepath + "'/>");
                                    sb.Append("</tr>");
                                    sb.Append("<tr>");
                                    sb.AppendFormat("<td style=\"text-align: center;\"><a href=\"/SitePages/ODCServiceProfiles.aspx?username={0}\" Target='_blank'>{0}</a></td>", Name);
                                    sb.Append("</tr>");
                                    sb.Append("</table>");
                                }
                            }
                            dr["htmlable"] = sb.Append("</div>");
                            dt.Rows.Add(dr);
                        }
                    }
                    else
                    {
                        lblerrormessage.Visible = true;
                        lblerrormessage.Text = "Currently we dont have any  client testimonials";
                    }
                }
                gvClientTestimonials.DataSource = dt;
                gvClientTestimonials.DataBind();
            }
        }

Comments

Popular posts from this blog

My Interview Experience

React Checkbox Control SPFx