In ControlTemplates Create A Visual Web Part
<%@ 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="VDRIndex.ascx.cs" Inherits="VDRMyDocsTreeViewIndex.ControlTemplates.VDRMyDocsTreeViewIndex.VDRIndex" %>
<script src="ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.9.1/jquery.tablesorter.min.js"></script>
<%-- <link href="http://mottie.github.io/tablesorter/css/theme.default.css" rel="stylesheet">--%>
<link href="../Style%20Library/GridHeader.CSS" rel="stylesheet">
<script type="text/javascript">
$(document).ready(function () {
//Get the watermark in search text box
var watermark = 'File Name';
$('#txtSearch').blur(function () {
if ($(this).val().length == 0)
$(this).val(watermark).addClass('watermark');
}).focus(function () {
if ($(this).val() == watermark)
$(this).val('').removeClass('watermark');
}).val(watermark).addClass('watermark');
var rows;
var coldata;
//$("#gvPictureLibrary").tablesorter({ widgets: ['zebra'] });
$("#gvPictureLibrary").tablesorter({ sortList: [[0, 0], [2, 0]] });
$('#txtSearch').keyup(function () {
SearchGridData();
});
});
function SearchGridData() {
var counter = 0;
//Get the search text
var searchText = $('#txtSearch').val().toLowerCase();
//Hide No record found message
$('#<%=lblMessage.ClientID %>').hide();
//Hode all the rows of gridview
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').hide();
if (searchText.length > 0) {
//Iterate all the td of all rows
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').children().each(function () {
var cellTextValue = $(this).text().toLowerCase();
//Check that text is matches or not
if (cellTextValue.indexOf(searchText) >= 0) {
$(this).parent().show();
counter++;
}
});
if (counter == 0) {
//Show No record found message
$('#<%=lblMessage.ClientID %>').show();
}
}
else {
//Show All the rows of gridview
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').show();
}
}
</script>
<table style="margin-left: 70px" >
<tr>
<td>
<div>
<div style="border-width: 1px; border-style: ridge; border-color: red; ">
<table>
<tr>
<td runat="server" >
<span>Search : </span><input type="text" id="txtSearch" style="width: 248px" />
<td >
<asp:Button ID="buttonExportUsers" style="font-weight: 700;background-color:#EF5836;color:white" runat="server" Text="Export to Excel" OnClick="ButtonExportUsers_Click" OnClientClick="window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 100);" />
</td>
</tr>
</table>
<asp:Label ID="lblMessage" runat="server" Text="No Record Found" Font-Bold="true" ForeColor="#EF5836" style="display:none"></asp:Label>
</div>
<asp:GridView ID="gvPictureLibrary" class="tablesorter" AutoGenerateColumns="False" ClientIDMode="Static" AllowSorting="false"
runat="server" BackColor="White" BorderColor="#EF5836" BorderStyle="Solid" ShowFooter="True"
BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#EF5836" ForeColor="#330099" />
<HeaderStyle BackColor="#EF5836" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#EF5836" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#EF5836" />
<SortedAscendingHeaderStyle BackColor="#EF5836" />
<SortedDescendingCellStyle BackColor="#EF5836" />
<SortedDescendingHeaderStyle BackColor="#EF5836" />
<Columns>
<asp:TemplateField HeaderText="Sr.No" SortExpression="Sr.No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Index" HeaderText="Index" />
<%--<asp:TemplateField HeaderText="Icon">
<ItemTemplate>
<asp:Image ID="IconImage" Height="13" Width="13" ImageUrl='<%# Bind("Icon") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Name" HeaderStyle-Width="235px" >
<ItemTemplate>
<asp:Image ID="IconImage" Height="13" Width="13" ImageUrl='<%# Bind("Icon") %>' runat="server" />
<asp:HyperLink ID="myHyperlink" Target="_blank" ClientIDMode="Static" Text='<%#Eval("Name")%>' NavigateUrl='<%# Eval("fol") %>' runat="server">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="Doc Type" HeaderText="Doc Type" />--%>
<asp:BoundField DataField="Doc Type" HeaderText="Doc Type">
<ControlStyle Width="100px"></ControlStyle>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Uploaded On" HeaderText="Uploaded On">
<ControlStyle Width="120px"></ControlStyle>
<HeaderStyle Width="120px" />
<ItemStyle Width="120px" />
</asp:BoundField>
<%-- <asp:BoundField DataField="Modified" HeaderText="Modified" >
<ControlStyle Width="100px"></ControlStyle>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:BoundField>--%>
<asp:BoundField DataField="Modified By" HeaderText="Modified By">
<ControlStyle Width="140px"></ControlStyle>
<HeaderStyle Width="140px" />
<ItemStyle Width="140px" />
</asp:BoundField>
<%--<asp:BoundField DataField="ServerRelativeUrl" HeaderText="ServerRelativeUrl"/>--%>
</Columns>
</asp:GridView>
</div>
</td>
<td></td>
</tr>
</table>
********************************CS Page****************
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Globalization;
using System.Web;
namespace VDRMyDocsTreeViewIndex.ControlTemplates.VDRMyDocsTreeViewIndex
{
public partial class VDRIndex : UserControl
{
ErrorLog objErrorLog = new ErrorLog();
string documentLibrary = string.Empty;
//string _strCurrentUser = string.Empty;
SPSite spSite = SPContext.Current.Site;
SPWeb spWeb;
public DataTable dtrip = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Retriving the document library name from the custom web part property //
intitable();
RetrieveDocLibName();
gvPictureLibrary.DataSource = dtrip;
gvPictureLibrary.DataBind();
gvPictureLibrary.UseAccessibleHeader = true;
gvPictureLibrary.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
[WebMethod]
public void RetrieveDocLibName()
{
try
{
MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex parent = (MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex)this.Parent;
documentLibrary = parent.ListName.ToString();
string ReturnDynamicUrl = GetRedirectUrl(documentLibrary);
if (documentLibrary != null)
{
//lblprojectName.Text = "Project >" + documentLibrary;
// lblProjectPath.Text = documentLibrary;
SPSite site = SPContext.Current.Site;
spWeb = site.OpenWeb();
// tvDocumentLibrary.Nodes.Clear();
// Creating an object of the document library //
//Commented on 29 oct 2015
SPDocumentLibrary spDocumentLibrary = (SPDocumentLibrary)spWeb.Lists[documentLibrary.Trim()];
// Creating an object of the root folder of the document library //
SPFolder spFolder = spDocumentLibrary.RootFolder;
// Creating the parent node of the tree view //
TreeNode treeNodeRoot = new TreeNode(spFolder.Name.Trim(), spFolder.Name.Trim(), @"/_layouts/15/images/folder.gif", spFolder.ServerRelativeUrl.Trim(), "IPFrame");
// Calling the custom method to get all the folders //
GetAllFilesFolders(spFolder, treeNodeRoot, ReturnDynamicUrl);
// Adding the tree nodes to the tree view control //
//tvDocumentLibrary.Nodes.Add(treeNodeRoot);
}
else
{
objErrorLog.WriteLog("Please mention a document library.");
}
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
/// <summary>
/// This custom menthod is used to get all the files and folders
/// </summary>
/// <param name="spFolder"></param>
/// <param name="parentNode"></param>
protected void GetAllFilesFolders(SPFolder spFolder, TreeNode parentNode,string SplitUrl)
{
string[] DynamicUrl = SplitUrl.Split(',');
if (DynamicUrl != null)
{
string DynamicLibName = DynamicUrl[0];
string DynamicRedirectPageName = DynamicUrl[1];
SPQuery spQuery = new SPQuery();
//spQuery.Query = @"<OrderBy><FieldRef Name='Indexer' Ascending='False' /></OrderBy>";
spQuery.Folder = spFolder;
string strCurrentUserName = spWeb.CurrentUser.Name.ToString();
SPListItemCollection spListItemCollection = spWeb.Lists[spFolder.ParentListId].GetItems(spQuery);
foreach (SPListItem spListItem in spListItemCollection)
{
// Checking whether the item is a file or a folder //
if (spListItem.Folder != null)
{
// Creating the node for the folder //
//Commented on 29 oct 2015
TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/15/images/folder.gif", spListItem.Folder.ServerRelativeUrl.Trim(), "IPFrame");
// TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/images/folder.gif");
string folder = spListItem.Folder.Name.Trim();
string folderspListItem = Convert.ToString(spListItem.ID).Trim();
string folderServerRelativeUrl = spWeb.Url.Trim() + spListItem.Folder.ServerRelativeUrl.Trim();
//string fol = spListItem.Folder.ServerRelativeUrl.Trim();
// string fol = spWeb.Url.Trim() + "/PeguformVDRTest/Forms/VDRView.aspx" + "?fol=" + spListItem.Folder.ServerRelativeUrl.Trim();
string fol = spWeb.Url.Trim() + "/" + DynamicLibName + "/" + DynamicRedirectPageName + "?fol=" + spListItem.Folder.ServerRelativeUrl.Trim();
SPListItem _item = spListItem.ListItems[0];
string Icon = spWeb.Url.Trim() + @"/_layouts/15/images/folder.gif";
DataRow newRow = dtrip.NewRow();
newRow["Icon"] = Icon;
newRow["Name"] = folder;
newRow["Index"] = _item["Indexer"];
newRow["ServerRelativeUrl"] = folderServerRelativeUrl;
newRow["Doc Type"] = "Folder";
newRow["fol"] = fol;
//ToString("MMMM dd, yyyy");
newRow["Uploaded On"] = Convert.ToDateTime(_item["Created"].ToString()).ToString("MMMM dd, yyyy");
newRow["Modified"] = Convert.ToDateTime(_item["Modified"].ToString()).ToString("MMMM dd, yyyy");
string[] ModifiedBy = _item["Modified By"].ToString().Split('#');
if (ModifiedBy != null)
{
newRow["Modified By"] = ModifiedBy[1];
}
else
{
newRow["Modified By"] = "";
}
dtrip.Rows.Add(newRow); ;
parentNode.ChildNodes.Add(treeNodeChildFolder);
// Calling the custom method to get all the subfolder and files within the folder //
GetAllFilesFolders(spListItem.Folder, treeNodeChildFolder, SplitUrl);
}
else
{
// Creating and object of the file //
SPFile spFile = spListItem.File;
SPListItem item = spFile.Item;
// Setting the display URL of the file //
string displayURL = spWeb.Url.Trim() + @"/" + spFile.Url.Trim();
// Setting the icon URL of the file //
string iconURL = spFile.IconUrl.Trim();
int y = iconURL.LastIndexOf("/") + 1;
iconURL = @"/_layouts/15/images/" + iconURL.Substring(y, iconURL.Length - y);
// Creating the node of the file //
TreeNode treeNodeChildFile = new TreeNode(spFile.Name.Trim(), spFile.Name.Trim(), iconURL.Trim(), displayURL.Trim(), "IPFrame");
string folspFile = spFile.Name.Trim();
string folderspFileName = spFile.Name.Trim();
string folderdisplayURL = displayURL.Trim();
// string fol = @"/" + spFile.Url.Trim();
string fol = spWeb.Url.Trim() + @"/" + spFile.Url.Trim() + "?fol=" + @"/" + spFile.Url.Trim();
string Icon = spWeb.Url.Trim() + iconURL;
DataRow newRow = dtrip.NewRow();
newRow["Icon"] = Icon;
newRow["Name"] = folspFile;
newRow["Index"] = item["Indexer"];
newRow["ServerRelativeUrl"] = folderdisplayURL;
newRow["fol"] = fol;
newRow["Doc Type"] = "Document";
newRow["Uploaded On"] = Convert.ToDateTime(item["Created"].ToString()).ToString("MMMM dd, yyyy");
newRow["Modified"] = Convert.ToDateTime(item["Modified"].ToString()).ToString("MMMM dd, yyyy");
string[] ModifiedBy = item["Modified By"].ToString().Split('#');
if (ModifiedBy != null)
{
newRow["Modified By"] = ModifiedBy[1];
}
else
{
newRow["Modified By"] = "";
}
dtrip.Rows.Add(newRow);
parentNode.ChildNodes.Add(treeNodeChildFile);
}
}
}
}
public void intitable()
{
try
{
dtrip.Columns.Add("Icon");
dtrip.Columns.Add("Name");
dtrip.Columns.Add("Index");
dtrip.Columns.Add("ServerRelativeUrl");
dtrip.Columns.Add("fol");
dtrip.Columns.Add("Doc Type");
dtrip.Columns.Add("Uploaded On");
dtrip.Columns.Add("Modified");
dtrip.Columns.Add("Modified By");
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
public string GetRedirectUrl(string documentLibraryName)
{
string ListName = "";
string ReturnUrlNamed = "";
if (documentLibraryName != null)
{
SPSite site1 = SPContext.Current.Site;
spWeb = site1.OpenWeb();
SPDocumentLibrary spDocumentLibrary = (SPDocumentLibrary)spWeb.Lists[documentLibraryName.Trim()];
SPFolder spFolder = spDocumentLibrary.RootFolder;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["VDRPageLocation"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
// query.RowLimit = 1;
SPListItemCollection items = list.GetItems(query);
if (items.Count > 0)
{
for (int i = 0; i < items.Count; i++)
{
SPListItem _item = items[i];
if (spFolder.ToString().ToLower() == _item["Title"].ToString().Trim().ToLower())
{
ListName = _item["Title"].ToString();
ReturnUrlNamed = _item["RedirectPage"].ToString();
break;
}
}
}
}
}
}
ReturnUrlNamed = ListName + "," + ReturnUrlNamed;
return ReturnUrlNamed;
}
protected void ButtonExportUsers_Click(object sender, EventArgs e)
{
try
{
MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex parent = (MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex)this.Parent;
string filename = parent.ListName.ToString() + "_" + DateTime.Now + ".xls";
string attachment = "attachment; filename=" + filename;
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
Response.AddHeader("content-disposition", attachment);
//Response.ContentType = "application/ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gvPictureLibrary.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gvPictureLibrary);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
}
}
<%@ 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="VDRIndex.ascx.cs" Inherits="VDRMyDocsTreeViewIndex.ControlTemplates.VDRMyDocsTreeViewIndex.VDRIndex" %>
<script src="ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.9.1/jquery.tablesorter.min.js"></script>
<%-- <link href="http://mottie.github.io/tablesorter/css/theme.default.css" rel="stylesheet">--%>
<link href="../Style%20Library/GridHeader.CSS" rel="stylesheet">
<script type="text/javascript">
$(document).ready(function () {
//Get the watermark in search text box
var watermark = 'File Name';
$('#txtSearch').blur(function () {
if ($(this).val().length == 0)
$(this).val(watermark).addClass('watermark');
}).focus(function () {
if ($(this).val() == watermark)
$(this).val('').removeClass('watermark');
}).val(watermark).addClass('watermark');
var rows;
var coldata;
//$("#gvPictureLibrary").tablesorter({ widgets: ['zebra'] });
$("#gvPictureLibrary").tablesorter({ sortList: [[0, 0], [2, 0]] });
$('#txtSearch').keyup(function () {
SearchGridData();
});
});
function SearchGridData() {
var counter = 0;
//Get the search text
var searchText = $('#txtSearch').val().toLowerCase();
//Hide No record found message
$('#<%=lblMessage.ClientID %>').hide();
//Hode all the rows of gridview
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').hide();
if (searchText.length > 0) {
//Iterate all the td of all rows
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').children().each(function () {
var cellTextValue = $(this).text().toLowerCase();
//Check that text is matches or not
if (cellTextValue.indexOf(searchText) >= 0) {
$(this).parent().show();
counter++;
}
});
if (counter == 0) {
//Show No record found message
$('#<%=lblMessage.ClientID %>').show();
}
}
else {
//Show All the rows of gridview
$('#<%=gvPictureLibrary.ClientID %> tr:has(td)').show();
}
}
</script>
<table style="margin-left: 70px" >
<tr>
<td>
<div>
<div style="border-width: 1px; border-style: ridge; border-color: red; ">
<table>
<tr>
<td runat="server" >
<span>Search : </span><input type="text" id="txtSearch" style="width: 248px" />
<td >
<asp:Button ID="buttonExportUsers" style="font-weight: 700;background-color:#EF5836;color:white" runat="server" Text="Export to Excel" OnClick="ButtonExportUsers_Click" OnClientClick="window.setTimeout(function() { _spFormOnSubmitCalled = false; }, 100);" />
</td>
</tr>
</table>
<asp:Label ID="lblMessage" runat="server" Text="No Record Found" Font-Bold="true" ForeColor="#EF5836" style="display:none"></asp:Label>
</div>
<asp:GridView ID="gvPictureLibrary" class="tablesorter" AutoGenerateColumns="False" ClientIDMode="Static" AllowSorting="false"
runat="server" BackColor="White" BorderColor="#EF5836" BorderStyle="Solid" ShowFooter="True"
BorderWidth="1px" CellPadding="4">
<FooterStyle BackColor="#EF5836" ForeColor="#330099" />
<HeaderStyle BackColor="#EF5836" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#EF5836" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#EF5836" />
<SortedAscendingHeaderStyle BackColor="#EF5836" />
<SortedDescendingCellStyle BackColor="#EF5836" />
<SortedDescendingHeaderStyle BackColor="#EF5836" />
<Columns>
<asp:TemplateField HeaderText="Sr.No" SortExpression="Sr.No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Index" HeaderText="Index" />
<%--<asp:TemplateField HeaderText="Icon">
<ItemTemplate>
<asp:Image ID="IconImage" Height="13" Width="13" ImageUrl='<%# Bind("Icon") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>--%>
<asp:TemplateField HeaderText="Name" HeaderStyle-Width="235px" >
<ItemTemplate>
<asp:Image ID="IconImage" Height="13" Width="13" ImageUrl='<%# Bind("Icon") %>' runat="server" />
<asp:HyperLink ID="myHyperlink" Target="_blank" ClientIDMode="Static" Text='<%#Eval("Name")%>' NavigateUrl='<%# Eval("fol") %>' runat="server">
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<%-- <asp:BoundField DataField="Doc Type" HeaderText="Doc Type" />--%>
<asp:BoundField DataField="Doc Type" HeaderText="Doc Type">
<ControlStyle Width="100px"></ControlStyle>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:BoundField>
<asp:BoundField DataField="Uploaded On" HeaderText="Uploaded On">
<ControlStyle Width="120px"></ControlStyle>
<HeaderStyle Width="120px" />
<ItemStyle Width="120px" />
</asp:BoundField>
<%-- <asp:BoundField DataField="Modified" HeaderText="Modified" >
<ControlStyle Width="100px"></ControlStyle>
<HeaderStyle Width="100px" />
<ItemStyle Width="100px" />
</asp:BoundField>--%>
<asp:BoundField DataField="Modified By" HeaderText="Modified By">
<ControlStyle Width="140px"></ControlStyle>
<HeaderStyle Width="140px" />
<ItemStyle Width="140px" />
</asp:BoundField>
<%--<asp:BoundField DataField="ServerRelativeUrl" HeaderText="ServerRelativeUrl"/>--%>
</Columns>
</asp:GridView>
</div>
</td>
<td></td>
</tr>
</table>
********************************CS Page****************
using Microsoft.SharePoint;
using Microsoft.SharePoint.Navigation;
using Microsoft.SharePoint.WebControls;
using System;
using System.Collections;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.Services;
using System.IO;
using System.Data;
using System.Globalization;
using System.Web;
namespace VDRMyDocsTreeViewIndex.ControlTemplates.VDRMyDocsTreeViewIndex
{
public partial class VDRIndex : UserControl
{
ErrorLog objErrorLog = new ErrorLog();
string documentLibrary = string.Empty;
//string _strCurrentUser = string.Empty;
SPSite spSite = SPContext.Current.Site;
SPWeb spWeb;
public DataTable dtrip = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Retriving the document library name from the custom web part property //
intitable();
RetrieveDocLibName();
gvPictureLibrary.DataSource = dtrip;
gvPictureLibrary.DataBind();
gvPictureLibrary.UseAccessibleHeader = true;
gvPictureLibrary.HeaderRow.TableSection = TableRowSection.TableHeader;
}
}
[WebMethod]
public void RetrieveDocLibName()
{
try
{
MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex parent = (MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex)this.Parent;
documentLibrary = parent.ListName.ToString();
string ReturnDynamicUrl = GetRedirectUrl(documentLibrary);
if (documentLibrary != null)
{
//lblprojectName.Text = "Project >" + documentLibrary;
// lblProjectPath.Text = documentLibrary;
SPSite site = SPContext.Current.Site;
spWeb = site.OpenWeb();
// tvDocumentLibrary.Nodes.Clear();
// Creating an object of the document library //
//Commented on 29 oct 2015
SPDocumentLibrary spDocumentLibrary = (SPDocumentLibrary)spWeb.Lists[documentLibrary.Trim()];
// Creating an object of the root folder of the document library //
SPFolder spFolder = spDocumentLibrary.RootFolder;
// Creating the parent node of the tree view //
TreeNode treeNodeRoot = new TreeNode(spFolder.Name.Trim(), spFolder.Name.Trim(), @"/_layouts/15/images/folder.gif", spFolder.ServerRelativeUrl.Trim(), "IPFrame");
// Calling the custom method to get all the folders //
GetAllFilesFolders(spFolder, treeNodeRoot, ReturnDynamicUrl);
// Adding the tree nodes to the tree view control //
//tvDocumentLibrary.Nodes.Add(treeNodeRoot);
}
else
{
objErrorLog.WriteLog("Please mention a document library.");
}
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
/// <summary>
/// This custom menthod is used to get all the files and folders
/// </summary>
/// <param name="spFolder"></param>
/// <param name="parentNode"></param>
protected void GetAllFilesFolders(SPFolder spFolder, TreeNode parentNode,string SplitUrl)
{
string[] DynamicUrl = SplitUrl.Split(',');
if (DynamicUrl != null)
{
string DynamicLibName = DynamicUrl[0];
string DynamicRedirectPageName = DynamicUrl[1];
SPQuery spQuery = new SPQuery();
//spQuery.Query = @"<OrderBy><FieldRef Name='Indexer' Ascending='False' /></OrderBy>";
spQuery.Folder = spFolder;
string strCurrentUserName = spWeb.CurrentUser.Name.ToString();
SPListItemCollection spListItemCollection = spWeb.Lists[spFolder.ParentListId].GetItems(spQuery);
foreach (SPListItem spListItem in spListItemCollection)
{
// Checking whether the item is a file or a folder //
if (spListItem.Folder != null)
{
// Creating the node for the folder //
//Commented on 29 oct 2015
TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/15/images/folder.gif", spListItem.Folder.ServerRelativeUrl.Trim(), "IPFrame");
// TreeNode treeNodeChildFolder = new TreeNode(spListItem.Folder.Name.Trim(), Convert.ToString(spListItem.ID).Trim(), @"/_layouts/images/folder.gif");
string folder = spListItem.Folder.Name.Trim();
string folderspListItem = Convert.ToString(spListItem.ID).Trim();
string folderServerRelativeUrl = spWeb.Url.Trim() + spListItem.Folder.ServerRelativeUrl.Trim();
//string fol = spListItem.Folder.ServerRelativeUrl.Trim();
// string fol = spWeb.Url.Trim() + "/PeguformVDRTest/Forms/VDRView.aspx" + "?fol=" + spListItem.Folder.ServerRelativeUrl.Trim();
string fol = spWeb.Url.Trim() + "/" + DynamicLibName + "/" + DynamicRedirectPageName + "?fol=" + spListItem.Folder.ServerRelativeUrl.Trim();
SPListItem _item = spListItem.ListItems[0];
string Icon = spWeb.Url.Trim() + @"/_layouts/15/images/folder.gif";
DataRow newRow = dtrip.NewRow();
newRow["Icon"] = Icon;
newRow["Name"] = folder;
newRow["Index"] = _item["Indexer"];
newRow["ServerRelativeUrl"] = folderServerRelativeUrl;
newRow["Doc Type"] = "Folder";
newRow["fol"] = fol;
//ToString("MMMM dd, yyyy");
newRow["Uploaded On"] = Convert.ToDateTime(_item["Created"].ToString()).ToString("MMMM dd, yyyy");
newRow["Modified"] = Convert.ToDateTime(_item["Modified"].ToString()).ToString("MMMM dd, yyyy");
string[] ModifiedBy = _item["Modified By"].ToString().Split('#');
if (ModifiedBy != null)
{
newRow["Modified By"] = ModifiedBy[1];
}
else
{
newRow["Modified By"] = "";
}
dtrip.Rows.Add(newRow); ;
parentNode.ChildNodes.Add(treeNodeChildFolder);
// Calling the custom method to get all the subfolder and files within the folder //
GetAllFilesFolders(spListItem.Folder, treeNodeChildFolder, SplitUrl);
}
else
{
// Creating and object of the file //
SPFile spFile = spListItem.File;
SPListItem item = spFile.Item;
// Setting the display URL of the file //
string displayURL = spWeb.Url.Trim() + @"/" + spFile.Url.Trim();
// Setting the icon URL of the file //
string iconURL = spFile.IconUrl.Trim();
int y = iconURL.LastIndexOf("/") + 1;
iconURL = @"/_layouts/15/images/" + iconURL.Substring(y, iconURL.Length - y);
// Creating the node of the file //
TreeNode treeNodeChildFile = new TreeNode(spFile.Name.Trim(), spFile.Name.Trim(), iconURL.Trim(), displayURL.Trim(), "IPFrame");
string folspFile = spFile.Name.Trim();
string folderspFileName = spFile.Name.Trim();
string folderdisplayURL = displayURL.Trim();
// string fol = @"/" + spFile.Url.Trim();
string fol = spWeb.Url.Trim() + @"/" + spFile.Url.Trim() + "?fol=" + @"/" + spFile.Url.Trim();
string Icon = spWeb.Url.Trim() + iconURL;
DataRow newRow = dtrip.NewRow();
newRow["Icon"] = Icon;
newRow["Name"] = folspFile;
newRow["Index"] = item["Indexer"];
newRow["ServerRelativeUrl"] = folderdisplayURL;
newRow["fol"] = fol;
newRow["Doc Type"] = "Document";
newRow["Uploaded On"] = Convert.ToDateTime(item["Created"].ToString()).ToString("MMMM dd, yyyy");
newRow["Modified"] = Convert.ToDateTime(item["Modified"].ToString()).ToString("MMMM dd, yyyy");
string[] ModifiedBy = item["Modified By"].ToString().Split('#');
if (ModifiedBy != null)
{
newRow["Modified By"] = ModifiedBy[1];
}
else
{
newRow["Modified By"] = "";
}
dtrip.Rows.Add(newRow);
parentNode.ChildNodes.Add(treeNodeChildFile);
}
}
}
}
public void intitable()
{
try
{
dtrip.Columns.Add("Icon");
dtrip.Columns.Add("Name");
dtrip.Columns.Add("Index");
dtrip.Columns.Add("ServerRelativeUrl");
dtrip.Columns.Add("fol");
dtrip.Columns.Add("Doc Type");
dtrip.Columns.Add("Uploaded On");
dtrip.Columns.Add("Modified");
dtrip.Columns.Add("Modified By");
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
public string GetRedirectUrl(string documentLibraryName)
{
string ListName = "";
string ReturnUrlNamed = "";
if (documentLibraryName != null)
{
SPSite site1 = SPContext.Current.Site;
spWeb = site1.OpenWeb();
SPDocumentLibrary spDocumentLibrary = (SPDocumentLibrary)spWeb.Lists[documentLibraryName.Trim()];
SPFolder spFolder = spDocumentLibrary.RootFolder;
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["VDRPageLocation"];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
// query.RowLimit = 1;
SPListItemCollection items = list.GetItems(query);
if (items.Count > 0)
{
for (int i = 0; i < items.Count; i++)
{
SPListItem _item = items[i];
if (spFolder.ToString().ToLower() == _item["Title"].ToString().Trim().ToLower())
{
ListName = _item["Title"].ToString();
ReturnUrlNamed = _item["RedirectPage"].ToString();
break;
}
}
}
}
}
}
ReturnUrlNamed = ListName + "," + ReturnUrlNamed;
return ReturnUrlNamed;
}
protected void ButtonExportUsers_Click(object sender, EventArgs e)
{
try
{
MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex parent = (MyDocsTreeViewVdrIndex.MyDocsTreeViewVdrIndex)this.Parent;
string filename = parent.ListName.ToString() + "_" + DateTime.Now + ".xls";
string attachment = "attachment; filename=" + filename;
Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = "";
Response.AddHeader("content-disposition", attachment);
//Response.ContentType = "application/ms-excel";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
HtmlForm frm = new HtmlForm();
gvPictureLibrary.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gvPictureLibrary);
frm.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch (Exception ex)
{
objErrorLog.WriteLog(ex.ToString());
}
}
}
}
**********************************Web Part***********************
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace VDRMyDocsTreeViewIndex.MyDocsTreeViewVdrIndex
{
[ToolboxItemAttribute(false)]
public class MyDocsTreeViewVdrIndex : Microsoft.SharePoint.WebPartPages.WebPart
{
private string docLibName = string.Empty;
public string ContactValue = null;
private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/VDRMyDocsTreeViewIndex/VDRIndex.ascx";
/// <summary>
///
/// </summary>
///
public MyDocsTreeViewVdrIndex()
{
// retrieveAllIP();
// retrieveAllListLibrary();
}
protected override void CreateChildControls()
{
Control control = Page.LoadControl(_ascxPath);
Controls.Add(control);
}
//DropDownList custom properties
public string ListName
{
get
{
return _listName;
}
set
{
_listName = value;
}
}
string _listName;
//public string ListValue
//{
// get
// {
// return _listValue;
// }
// set
// {
// _listValue = value;
// }
//}
//string _listValue;
public override ToolPart[] GetToolParts()
{
ToolPart[] allToolParts = new ToolPart[3];
WebPartToolPart standardToolParts = new WebPartToolPart();
CustomPropertyToolPart customToolParts = new CustomPropertyToolPart();
allToolParts[0] = standardToolParts;
allToolParts[1] = customToolParts;
allToolParts[2] = new VDRCustomToolPart();
return allToolParts;
}
}
}
***********************************Error Class*********************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VDRMyDocsTreeViewIndex
{
public class ErrorLog
{
public void WriteLog(string strLog)
{
System.IO.StreamWriter log;
FileStream fileStream = null;
DirectoryInfo logDirInfo = null;
FileInfo logFileInfo;
//string Folderpath = WebConfigurationManager.AppSettings["ErrorFolderpath"].ToString();
string logFilePath = "C:\\Logs\\";
// string logFilePath = Folderpath;
logFilePath = logFilePath + "VDRIndex" + System.DateTime.Today.ToString("dd-MM-yyyy") + "." + "txt";
logFileInfo = new FileInfo(logFilePath);
logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
if (!logDirInfo.Exists) logDirInfo.Create();
if (!logFileInfo.Exists)
{
fileStream = logFileInfo.Create();
}
else
{
fileStream = new FileStream(logFilePath, FileMode.Append);
}
log = new StreamWriter(fileStream);
log.WriteLine("*******************************" + DateTime.Now + "********************************");
log.WriteLine(strLog);
log.Close();
}
}
}
No comments:
Post a Comment