Developer forum

Forum » CMS - Standard features » Files on userpages

Files on userpages

Dan Lundgren
Reply

Hello.

The scenario is that i want to be able to have user-specific files, that is shown to the user when they are logged in.
We've used the DW Solutionset as a base for our solution.
We've found that you can do it by setting the right permissions on a paragraph that point to a user-specific map in Files, but we want it to be less administration.
Is there a way to do this through maybe a templateextender to limit the amount of administration? Or some other way?

Best regards
Dan


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Dan

You can just create a Razor paragraph template, map a customer specific folder in that template an iterate the files in the folder:

DirectoryInfo directory = new DirectoryInfo(Server.MapPath(@"~\customers\" + Dynamicweb.Frontend.PageView.Current().User.CustomerNumber)); 

var files = directory.GetFiles().ToList(); 

And then just loop the files and list them.

BR Nicolai

 
Dan Lundgren
Reply

Hello again.

Thanks for the reply.
Have another question: Is the map secure then? Or do i need to set something in the backend for it to be?
 

Best regards
Dan

 
Nicolai Høeg Pedersen
Reply

Hi Dan

All files placed in /Files are available through direct url - that is handled by the IIS and not Dynamicweb.

So you need to remove the read access to the customer folders and have a .net based handler manage the access to the files.

Dynamicweb comes with a handler for this /Admin/Public/Download.aspx?File=files/images/somedocument.pdf. It will check the Dynamicweb permissions set in the file manager, so they need to be set as well.

BR Nicolai

 
Dan Lundgren
Reply

Hi again.

Ive tried doing this in a few different ways, but i have some issues.
I created a PageTemplateExtender for listing the files on a page.
I debugged locally, i see that that it loops through the files, it adds the loop so i can access it on the DW-side, but i dont get anything in my loop.
My loop for files looks like this:

                foreach (var file in new DirectoryInfo(HttpContext.Current.Server.MapPath(root)).GetFiles().Where(f => f.Name.StartsWith(userid)))
                {
                        var fileName = file.Name;
                        var fileurl = string.Format("{0}/{1}", root, fileName);

                        loop.SetTag("UserFilesExtenderPath", fileurl.ToString());
                        loop.SetTag("UserFilesExtenderFileName", fileName.ToString());
                        loop.CommitLoop();                
                }

I then tried doing it in a Razor paragraph template, but then i cant access LINQ, i just get the following error:
"System.Array does not contain a definition for 'where'".

So, i got two questions:

What am i doing here that doesnt make my loop commit? Do i have to use another type of extender or something?
How can i get the loop to commit?
and
Is LINQ not supported in DW for Razortemplates?

Best regards
Dan

 
Mikkel Ricky
Reply

You can use LINQ in Razor templates, but you may have to add @using System.Linq to your template:

@using System.IO
@using System.Web
@using System.Linq
@{
var userId = Pageview.User.UserID;
userId = 1;
if (userId > 0) {
    var root = "/Files/Customers";
    var directory = new DirectoryInfo(HttpContext.Current.Server.MapPath("~"+root));
    if (directory.Exists) {
        var files = directory.GetFiles().Where(f => f.Name.StartsWith(userId.ToString()));
        if (files.Any()) {
            foreach (var file in files) {
                var fileName = file.Name;
                var fileUrl = string.Format("{0}/{1}", root, fileName);
                <pre><a href="@fileUrl">@fileName</a></pre>
            }
        }
    }
}
}

Alternatively, you can use directory.GetFiles(userId+"*") rather than using LINQ, but using LINQ is much nicer.

In your template extender you have to add

var loop = «some template object».GetLoop(«loop name»);

inside your foreach loop to make the loop work as expected.

Best regards,
Mikkel

 

 
Dan Lundgren
Reply

Hi Mikkel.

Sorry, didnt include all of my code from the templateExtender.
I have declared the loop variable already.
Ive also tried including System.Linq in my razortemplate, but then i get the error:
"The type or namespace name 'Linq' does not exist in the namespace "System"".

This is the full code:

       var currentUserId = User.GetCurrentUserID();
            string tag = "UserFilesExtenderList";
            var loop = template.GetLoop(tag);
            string root = "/Files/Files/UserFiles";
            string userid = currentUserId.ToString();

            if(currentUserId != 0)
            {

                foreach (var file in new DirectoryInfo(HttpContext.Current.Server.MapPath(root)).GetFiles().Where(f => f.Name.StartsWith(userid)))
                {
                        var fileName = file.Name;
                        var fileurl = string.Format("{0}/{1}", root, fileName);

                        loop.SetTag("UserFilesExtenderPath", fileurl.ToString());
                        loop.SetTag("UserFilesExtenderFileName", fileName.ToString());
                        loop.CommitLoop();                
                }

            }
            else
            {
                template.SetTag("UserFilesExtenderID", "none");
            }


Is Linq not included? Can i include it somehow?
How can i get the loop to commit?

Best regards
Dan

 
Mikkel Ricky
Reply

Your template extender code looks ok. I assume you're using the loop in a layout template (otherwise it will not work), but what does "i dont get anything in my loop" exactly mean? Does the loop not exist or is is empty? What does

@TemplateTags("UserFilesExtenderList")

report?

I'm not sure why Linq is not available for use in your Razor templates, but it may have something to do with the Web.config in your custom solution. Try comparing it with the Web.config from an official Dynamicweb release matching your custom solution version.

Perhaps these articles will be helpful: http://forums.asp.net/t/1264863.aspx?The+type+or+namespace+name+Linq+does+not+exist+in+the+namespace+System+Data+are+you+missing+an+assembly+reference+ and http://www.codeproject.com/Questions/706230/The-type-or-namespace-name-Linq-does-not-exists-in

Best regards,
Mikkel

 

You must be logged in to post in the forum