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.

Monday, March 11, 2013

My First WebPart in SharePoint 2013


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="TestSp2013.ascx.cs" Inherits="TestSp2013.TestSp2013.TestSp2013" %>
<asp:GridView ID="dgvTasks" runat="server" CellPadding="4" ForeColor="#333333" AutoGenerateColumns="False" width="100%">
    <AlternatingRowStyle BackColor="White" />
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <sortedascendingcellstyle backcolor="#F5F7FB" />
    <sortedascendingheaderstyle backcolor="#6D95E1" />
    <sorteddescendingcellstyle backcolor="#E9EBEF" />
    <sorteddescendingheaderstyle backcolor="#4870BE" />
    <columns>
        <asp:BoundField DataField="Title" HeaderText="Title"></asp:BoundField>
        <asp:BoundField DataField="Status" HeaderText="Status"></asp:BoundField>
        <asp:BoundField DataField="Priority" HeaderText="Priority"></asp:BoundField>
        <asp:BoundField DataField="StartDate" DataFormatString="{0:G}"
HeaderText="Start Date"></asp:BoundField>
        <asp:BoundField DataField="DueDate" DataFormatString="{0:G}"
HeaderText="End Date"></asp:BoundField>
        <asp:BoundField DataField="AssignedTo" HeaderText="Assigned To"></asp:BoundField>
    </columns>
</asp:GridView>
Ascx.cs
using System;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;

namespace TestSp2013.TestSp2013
{
    [ToolboxItemAttribute(false)]
    public partial class TestSp2013 : WebPart
    {
        public TestSp2013()
        {
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            InitializeControl();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetData();
            }

        }

        public void GetData()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPQuery sQuery = new SPQuery();
                    sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                    SPListItemCollection myColl = SPContext.Current.Web.Lists["Tasks"].GetItems(sQuery);
                    if (myColl.Count > 0)
                    {
                        dgvTasks.DataSource = myColl.GetDataTable();
                        dgvTasks.DataBind();
                    }
                });
            }
            catch (Exception Ex)
            {
                Page.Response.Write(Ex.ToString());
            }
        }

    }
}



No comments:

Post a Comment