1

I am using SP2013 on-premise.

I have two document libraries. Some files in DocLibA have a relationship with DocLibB's documents. I would like to create a column in DocLibA and link to 0-1 files in DocLibB. So that when a user browse DocLibA's view, he can directly download the related document with a single click.

I have tried to setup a lookup column to DocLibB's title column. But it just gives me a link to open listform.aspx page (/_layouts/15/listform.aspx?PageType=4&ListId={5475cxxxxx...}) which shows the file's properties. I want to directly link to the document (i.e. /DocLibB/file.pdf).

Could you please advice?

3
  • You could use some Client Side Rendering to change the URL from listform.aspx to the URL of the document. You can probably get the URL to the document from the context object.
    – Submits
    May 26, 2016 at 10:10
  • Do have any sample code? I can find sample querying single item but not for entire list view.
    – Mark L
    May 27, 2016 at 8:08
  • Added answer with sample code.
    – Submits
    May 27, 2016 at 10:10

2 Answers 2

2

Assume you have a column in your DocLibA view that is a single value lookup column to a column in DocLibB (it actually doesn't matter which column in this case).

You can use Client Side Rendering to change the default link, which is to the display form of the item in DocLibB, to the item itself.

Place this code on the view page of DocLibA. The only change you need to make is to the name of the lookup column YourLookupColumnInternalName:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", CSR_Setup);

function CSR_Setup() {
    var overridectx = {};
    overridectx.Templates = {};
    overridectx.Templates.Fields = {
        "YourLookupColumnInternalName": { "View": CSR_AddLinkToFile }
    };
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(overridectx);
}

function CSR_AddLinkToFile(ctx) {
    return String.format("<a href='{0}'>{1}</a>", ctx.CurrentItem.FileRef, ctx.CurrentItem.FileLeafRef);
}
5
  • Oh, you mean I have to insert this code in Content Editor webpart, not in JSLink?
    – Mark L
    May 27, 2016 at 10:23
  • Add the code in any way you like. Either add a script editor web part and wrap the code in <script type="text/javascript"></script> or create a JavaScript file in your site's Site Assets library and link to it with a script editor web part with <script type="text/javascript" src="link/to/your/scriptfile"></script> or edit the document library's webpart, and add the link to your script file to the JSLink property of the web part.
    – Submits
    May 27, 2016 at 10:28
  • Could you take a look on my code: sharepoint.stackexchange.com/questions/181202/…
    – Mark L
    May 27, 2016 at 10:30
  • If I don't get it wrong, your code will only return the FileRef of DocLibA's currentItem, not DocLibB's.
    – Mark L
    May 27, 2016 at 10:44
  • Yes correct. Its a sample CSR code as you asked. You're on the right track with your other question, although you'll probably find sync/asynch issues.
    – Submits
    May 27, 2016 at 10:47
0

You can setup a calculated column in DocLibA that builds the URL to the document in DocLibB, or alternatively, build the URL from a workflow that is setup to start at creation/edit of the item in DocLibA

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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