Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, February 16, 2012

Binding Top 5 Announcements to Simple HTML Table

 try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPList lst = currentWeb.Lists["Announcements"];
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Geq><FieldRef Name='Expires' /><Value Type='DateTime'><Today /></Value></Geq></Where>";
                    sQuery.RowLimit = 5;
                    SPListItemCollection AnnouncementsColl = lst.GetItems(sQuery);
                    StringBuilder strAnnouncements = new StringBuilder();
                    strAnnouncements.Append("<Table width='100%' cellpadding='0' cellspacing='0'>");
                    if (AnnouncementsColl.Count > 0)
                    {
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2'>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2' align='Center'>");
                        strAnnouncements.Append("<Strong>");
                        strAnnouncements.Append("Announcements RollUp");
                        strAnnouncements.Append("</Strong>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td  Colspan='2'>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6'>");
                        strAnnouncements.Append("<Strong>");
                        strAnnouncements.Append("Title");
                        strAnnouncements.Append("</Strong>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6'>");
                        strAnnouncements.Append("<Strong>");
                        strAnnouncements.Append("Created By");
                        strAnnouncements.Append("</Strong>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2'>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        foreach (SPListItem item in AnnouncementsColl)
                        {
                            string strTitle = string.Empty;
                            strTitle = item.Title.ToString();
                            string strCreatedBy = string.Empty;
                            SPFieldLookupValue strValue = new SPFieldLookupValue(item["Author"].ToString());
                            strAnnouncements.Append("<Tr>");
                            strAnnouncements.Append("<Td>");
                            strAnnouncements.Append(strTitle);
                            strAnnouncements.Append("</Td>");
                            strAnnouncements.Append("<Td>");
                            strAnnouncements.Append(strValue.LookupValue);
                            strAnnouncements.Append("</Td>");
                            strAnnouncements.Append("</Tr>");
                            strAnnouncements.Append("<Tr>");
                            strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2'>");
                            strAnnouncements.Append("</Td>");
                            strAnnouncements.Append("</Tr>");
                        }
                    }
                    else
                    {
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2'>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td Colspan='2' align='Center' bgcolor='#add8e6'>");
                        strAnnouncements.Append("<Strong>");
                        strAnnouncements.Append("Currently there are no Announcements....!");
                        strAnnouncements.Append("</Strong>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                        strAnnouncements.Append("<Tr>");
                        strAnnouncements.Append("<Td bgcolor='#add8e6' Colspan='2'>");
                        strAnnouncements.Append("</Td>");
                        strAnnouncements.Append("</Tr>");
                    }
                    strAnnouncements.Append("</Table>");
                    writer.Write(strAnnouncements);
                });
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }

No comments:

Post a Comment