Applying TreeView to Documents&Files
Need some assistance in setting up a widget to show the libraries and documents in TreeView similar to this link below, is this possible?
demos.telerik.com/.../defaultcs.aspx
Hi
I have added a modified code of the sample provided here:
http://www.sitefinity.com/developer-network/forums/general-discussions-/modifying-download-list-widget
to suit your scenario for document libraries and folders, and the documents themselves:
public
IEnumerable<LibraryTreeModel> GetDocumentLibrariesAndFolders()
LibrariesManager manager =
new
LibrariesManager();
var albums = manager.GetDocumentLibraries();
var data =
new
List<LibraryTreeModel>();
foreach
(var item
in
albums)
var children = manager.GetAllFolders(item);
var model =
new
LibraryTreeModel();
model.Title = item.Title.ToString();
model.Id = item.Id;
model.ParentId = (item.ParentId ==
null
) ?
null
: item.ParentId;
data.Add(model);
foreach
(var child
in
children)
var modelChild =
new
LibraryTreeModel();
modelChild.Title = child.Title.ToString();
modelChild.MediaUrl = child.Title.ToString();
modelChild.Id = child.Id;
modelChild.ParentId = child.ParentId;
data.Add(modelChild);
List<LibraryTreeModel> models =
new
List<LibraryTreeModel>();
foreach
(var item
in
data)
// For folders
IQueryable<Telerik.Sitefinity.Libraries.Model.Document> docsFolders = manager.GetDocuments()
.Where(img => img.FolderId == item.Id && img.Status == ContentLifecycleStatus.Live);
// If folder is actually a Library
IQueryable<Telerik.Sitefinity.Libraries.Model.Document> docsLibraries = manager.GetDocuments()
.Where(img => img.Parent !=
null
&& img.Parent.Id == item.Id && img.FolderId ==
null
&& img.Status == ContentLifecycleStatus.Live);
IQueryable<Telerik.Sitefinity.Libraries.Model.Document> allDocs = docsFolders.Concat(docsLibraries);
foreach
(var file
in
allDocs)
var modelChild =
new
LibraryTreeModel();
modelChild.Title = file.Title.ToString();
modelChild.Id = file.Id;
modelChild.ParentId = item.Id;
modelChild.MediaUrl = file.MediaUrl;
models.Add(modelChild);
IEnumerable<LibraryTreeModel> ds = models.Concat(data);
return
ds;
protected
override
void
InitializeControls(GenericContainer container)
var data =
this
.GetDocumentLibrariesAndFolders();
TreeView.DataFieldParentID =
"ParentId"
;
TreeView.DataFieldID =
"Id"
;
TreeView.DataTextField =
"Title"
;
TreeView.DataValueField =
"Title"
;
TreeView.DataSource = data;
TreeView.DataBind();
<%@ Control Language="C#" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<
telerik:RadTreeView
ID
=
"RadTreeView"
runat
=
"server"
Width
=
"300px"
Height
=
"250px"
>
<
NodeTemplate
>
<
div
>
<
a
href='<%# DataBinder.Eval(Container.DataItem, "MediaUrl") %>'><%# DataBinder.Eval(Container.DataItem, "Title") %></
a
>
</
div
>
</
NodeTemplate
>
</
telerik:RadTreeView
>
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
Telerik.Sitefinity;
namespace
SitefinityWebApp.LibraryTreeView
public
class
LibraryTreeModel
public
string
Title
get
;
set
;
public
Guid? ParentId
get
;
set
;
public
Guid Id
get
;
set
;
public
string
MediaUrl
get
;
set
;
Thanks Nikola!
Two questions. Is there a way to show only the libraries the logged in user has access to? Also when I click on the library itself it takes me to back to the homepage, is that normal? Once again thanks for your assistance with this!
Hi
The permissions should be applied automatically by the manager when the queries are made and when clicking on a document, you should not be able to download it or view it, if you do not have permissions. Please, check the Url set to the link when you are being redirected to the home page, I am not reproducing this on my end.
Regards,
Nikola Zagorchev
Telerik
Hi Nikola,
That is correct. The files that a user does not have access to it does not let me download them it gives me the page in the image attached. What I am asking is if its possible to not show those libraries at all when the user logs in and just show the libraries that the user has access to.
Hi
If the user does not have access to the library, at all, this should be handled by the manager (if you are not suppressing security). If they can view the library, just not the documents the library will be returned from the query.
You can also try enabling the filtering by view permissions globally or per provider. You can set that for the application in Advanced settings -> Security -> Enable filtering queries by view permissions.
Regards,
Nikola Zagorchev
Telerik
Nikola,
I already have that setting applied to filter queries by view permission but when I login with a user who doesn't have access to a specific album i can still see it and its content. However, it does not let me download the content, what I want is to not show the library the the user does not have access to at all in the TreeView.
Thanks,
Hector Rodriguez
Hello
Could you please check the "View document library" permission set globally for the documents and files and "View this document library" for the particular library permission? Let us know and the "View document" permission set. If the first two are set for everyone, for instance, you would be able to get the libraries. Check also whether you are not manually suppressing security checks or using elevated mode region.
Regards,
Nikola Zagorchev
Telerik
Hello Nikola,
The permission settings for "Documents & Files" is set to "Everyone" but the actual individual libraries are set to only "View document library" and "View document" based on a specific role which is what I want. In the regular downloads list widget it is set like this and it shows me only the documents of the library that the user role has access to. There is no way to show the parent library and sub library that documents are in using the download list widget is there? something similar to how "Documents and Files" is shown on the back-end because if there is that would be great seeing as permission wise it works wonderfully.
Thanks,
Hector Rodriguez
Hello
You can access the Document Library using the Document property:
public
DocumentLibrary Library
public
Guid? FolderId
Imports System.IO.DirectoryInfoImports System.IO Partial Class treeview1 Inherits System.Web.UI.PageProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load'\\PFS1-DATA\Epic Implementation\CIS Corner\Tip Sheets 'set the initial Value of the TreeNode to the C drive, but we will change this on Page LoadTreeview1.Nodes(0).Value = Server.MapPath("tipsheets")End SubProtected Sub Treeview1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles Treeview1.TreeNodePopulateIf IsCallback = True ThenIf e.Node.ChildNodes.Count = 0 ThenLoadChildNode(e.Node)End IfEnd IfEnd SubPrivate Sub LoadChildNode(ByVal node As TreeNode)Dim directory As DirectoryInfo directory = New DirectoryInfo(node.Value)For Each subtree As DirectoryInfo In directory.GetDirectories()Dim subNode As TreeNode = New TreeNode(subtree.Name) subNode.Value = subtree.FullNameTryIf subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then subNode.SelectAction = TreeNodeSelectAction.SelectExpand subNode.PopulateOnDemand = True subNode.NavigateUrl = "#"End IfCatch ex As ExceptionEnd Try node.ChildNodes.Add(subNode)NextFor Each fi As FileInfo In directory.GetFiles()Dim subNode As TreeNode = New TreeNode(fi.Name) node.ChildNodes.Add(subNode) subNode.NavigateUrl = "tipsheets/" & directory.Name.ToString & "/" & fi.Name.ToString()'If hosting this web application on a server, you will want to use the Server.MapPath line that is currently commented out below. 'subNode.NavigateUrl = Server.MapPath(fi.Name.ToString())NextEnd SubEnd Class
Hi
Could you please specify what you are trying to achieve and what error you are experiencing, so we can assist further?
Regards,
Nikola Zagorchev
Telerik