Monday, 17 March 2014

Sample Discussion Webpart Code in Sharepoint 2013

Discussion.ascx.cs:

namespace Intranet.Discussions
{
    using System;
    using System.ComponentModel;
    using System.Web.UI.WebControls.WebParts;
    using Microsoft.SharePoint;
    using System.Text;
 
    [ToolboxItemAttribute(false)]
    public partial class Discussions : WebPart
    {
        // Uncomment the following SecurityPermission attribute only when doing Performance Profiling on a farm solution
        // using the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
        // for production. Because the SecurityPermission attribute bypasses the security check for callers of
        // your constructor, it's not recommended for production purposes.
        // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
        public Discussions()
        {

        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    string newLatestDisussion = string.Empty;
                    using(SPSite latesNews = new SPSite(SPContext.Current.Web.Url))
                    {
                        using (SPWeb latestWeb = latesNews.OpenWeb())
                        {
                            SPList latestList = latestWeb.Lists["Discussion"];
                            SPQuery latestQuery = new SPQuery();
                            latestQuery.Query = "<GetListItems><Query><OrderBy><FieldRef Name=\"DiscussionLastUpdated\" Ascending=\"False\" /></OrderBy></Query><ViewFields><FieldRef Name=\"Body\" /><FieldRef Name=\"Subject\" /><FieldRef Name=\"DiscussionLastUpdated\" Ascending=\"True\" /><FieldRef Name=\"Modified\" /></ViewFields><QueryOptions /></GetListItems>";
                            SPListItemCollection latestListCollection = latestList.GetItems(latestQuery);
                            foreach (SPListItem item in latestListCollection)
                            {
                                string disussionTemplate = "<div class=\"rows border_none left\"><div class=\"cols-1 left\"> <li class=\"icon-large discussions\"></li> </div><div class=\"cols-2 left\"> <h3>[Body]</h3><p>[Subject][Modified]</p></div></div>";
                                string disussionBody = Convert.ToString(item["Body"]);
                                string discussionSubject = Convert.ToString(item["Subject"]);                          
                                string dicussionModdate = Convert.ToDateTime(item["Modified"]).ToShortDateString();                              
                                disussionTemplate = disussionTemplate.Replace("[Body]", disussionBody);
                                disussionTemplate = disussionTemplate.Replace("[Subject]", discussionSubject);
                                disussionTemplate = disussionTemplate.Replace("[Modified]", dicussionModdate);
                             
                                newLatestDisussion += disussionTemplate;
                             
                             
                            }
                            latestdiscussionTemplate.InnerHtml = newLatestDisussion;

                        }
                    }
                }
                catch (Exception ex)
                {
                 
                }
            }


        }
    }
}

Discussion.ascx:




<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Latest Discussions.ascx.cs" Inherits="Intranet.Discussions.Discussions" %>
<div class="ldiscussions">
               <div class="red_bg">
            <h2> Discussions</h2>
          </div>
         <div id="discussionTemplate" runat="server"></div>                
            <div class="more text-dec">
              <div class="read_more right"><img src="/Style Library/Projectname/images/more.png" width="18" height="17"></div>
              <div class="read_more right"><a href="http://win-sp:100/Lists/Discussion/AllItems.aspx">More</a> </div>
            </div>
          </div>



Element File:


<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/" >
  <Module Name="Discussions" List="113" Url="_catalogs/wp">
    <File Path="Discussions\ Discussions.webpart" Url="Intranet.Discussions.webpart" Type="GhostableInLibrary" >
      <Property Name="Group" Value="Custom" />
    </File>
  </Module>
</Elements>



No comments:

Post a Comment