2

I wonder if there is a simple solution for the requirement to have a link to the location of the file in a custom view of a document library?

I have a view that shows all files of a library without folders and sorts the newest on top. Often there is the need to jump to the location of a file. This is only possible by opening the file properties and using the breadcrump in this properties view. On site search level the results typically have a link in the flyout menu that sais "View Library". I don´t understand why this is not a standard(?).

I read something about "FileDirRef" but cannot seem to find information on how to use this.

1

I don't know if there are better ways to do this, but there are definitely other ways to do this.

Here's a possible approach, using CSR + jslink (basically adding FileDirRef and transforming it into a hyperlink):

1) add the FileDirRef field to the view

        string viewFieldToAdd = "FileDirRef";
        List list = context.Web.Lists.GetByTitle(listTitle);
        View view = list.DefaultView;
        context.Load(list);
        context.Load(list.Fields);
        context.Load(view);
        context.Load(view.ViewFields);
        context.ExecuteQuery();
        if(!view.ViewFields.Any(x => x == viewFieldToAdd))
        {
            Console.WriteLine("Adding viewfield: " + viewFieldToAdd);
            view.ViewFields.Add(viewFieldToAdd);
            view.Update();
            context.ExecuteQuery();
        }

2) open the list view e.g. http://.../Documents/Forms/AllItems.aspx

3) enter edit mode by adding ?ToolPaneView=2 to the url,

e.g. http://.../Documents/Forms/AllItems.aspx?ToolPaneView=2

4) go into webpart edit mode and paste the jslink url, ensuring the destination js file exists, e.g. ~sitecollection/Style Library/csr/FolderLink.js

5) write the csr script (FolderLink.js)

SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
  Templates: {
        Fields: {
           'FileDirRef': {'View':function(ctx) {
                  if(ctx.CurrentItem.FileDirRef != null)
                  {
                      var url = ctx.CurrentItem.FileDirRef;
                      return String.format('<a href="{0}">{1}</a>', url, url);
                  }
           }}
       }
  }
});
  • Question to 1): Where to put the mentioned code? In a script editor on that same page? – Worn Jan 30 at 9:39
  • so the idea is to add a view field that normally isn't available in the UI. it's a one-time execution that you need to run either server side or client side (jsom, csom, etc.). in the example I'm running a console application with csom. you can try js instead. example: sharepoint.stackexchange.com/questions/240786/… – Tiago Duarte Jan 30 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.