Document ID Hyperlink with QR Code for SharePoint 2010

Document ID Hyperlink with QR-Code for SharePoint 2010

About a month ago, Jan Tielens posted a nice solution on his blog for generating short URL’s for SharePoint 2010 documents. His solutions depends on a URL shortening services like http://bit.ly/urlwiki or http://tinyurl.com/urlwiki.

As Jeroen Ritmeijer points out in his comment on the same page, this might not be a proper solution for confidential files. I wouldn’t want my ‘Plan for World Domination.docx’ leaking to the public. In this blog I try to give an alternative solution for those long hyperlinks.

A new feature in SharePoint 2010 is the Document ID Service. Every document is provided with an unique ID that uniquely identifies the document. Even if the document is moved to another folder or library, the document can still be found with that document ID. If you inspect the URL used by the Document ID service, it looks something like this:

http://<site>/_layouts/DocIdRedir.aspx?ID=<document ID>

image

Although this URL is longer than the URL’s used by the TinyUrl service used by Jan, it is still shorter and more friendly than the original SharePoint URL:

http://<site>/<subsite>/<subsite>/<subsite>/<library>/<folder>/<folder>/<folder>/Document.docx

I adopted the source-code provided by Jan to use the document ID hyperlink. Furthermore, I included a free javascript library for QR-code generation in the solution so there is no need for calls to external services.

image

The new script Elements.xml:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Scripts">
      <File Path="Scripts\jquery-1.7.1.min.js" Url="Scripts/jquery-1.7.1.min.js" />
      <File Path="Scripts\AlainDeKlerk.SP2010.DocumentIdLink.js" Url="Scripts/AlainDeKlerk.SP2010.DocumentIdLink.js" />
      <File Path="Scripts\qrcode.js" Url="Scripts/qrcode.js" />
  </Module>
</Elements>

And the new javascript code (I highlighted the important changes):

function AlainDeKlerk_IsAvailable_CreateDocumentIdLink() {
    var items = SP.ListOperation.Selection.getSelectedItems();
    if (items.length == 1) {
        if (items[0].fsObjType == '0') {
            return true;
        }
    }
}

function AlainDeKlerk_CreateDocumentIdLink() {
    var context = SP.ClientContext.get_current();
    this.web = context.get_web();
    this.site = context.get_site();
    var listId = SP.ListOperation.Selection.getSelectedList();
    var list = this.web.get_lists().getById(listId);
    var items = SP.ListOperation.Selection.getSelectedItems(context);
    var itemId = items[0];
    this.listItem = list.getItemById(itemId.id);
    context.load(this.site);
    context.load(this.listItem, "FileLeafRef", "FileRef", "EncodedAbsUrl", "_dlc_DocIdUrl");
    context.executeQueryAsync(Function.createDelegate(this, this.DocumentIdLinkSuccess), Function.createDelegate(this, this.DocumentIdLinkSuccessFail));
}

var typeNumber;
var errorCorrectLevel;

function DocumentIdLinkSuccess(sender, args) {
    var siteUrl = this.site.get_url();
    var url = siteUrl + "/_layouts" + this.listItem.get_item('_dlc_DocIdUrl').get_url().replace(/^[\s\u3000]+|[\s\u3000]+$/g, '').split("_layouts")[1];
    var docId = this.listItem.get_item("_dlc_DocIdUrl").get_description();

    var qr = qrcode(typeNumber || 4, errorCorrectLevel || 'M');
    qr.addData(url);
    qr.make();

    var dynamicHtml = $("<div><input style='width:394px;' type='text' value='" + url + "'/><br><div id='qrcode' align='center'></div></div>");
    var options = {
        title: docId,
        html: dynamicHtml[0],
        allowMaximize: false,
        showClose: true,
        width: 400,
        height: 320
    }

    SP.UI.ModalDialog.showModalDialog(options);
    $("#qrcode").html(qr.createImgTag()).children("IMG").css("width", "280px").css("height", "280px");
    $("input", dynamicHtml).focus().select();
}

function DocumentIdLinkSuccessFail(sender, args) {
    alert('failed to get data. Error:' + args.get_message());
}

And the last change, this feature cannot be used unless the Document ID Service is activated, so let’s make sure it is by adding an ActivationDependency element to the feature:

<Feature xmlns="http://schemas.microsoft.com/sharepoint/" Id="D24B6C8B-F747-4021-B9EA-BE2A08B3AA29" Scope="Web" Description="Adds a button to the ribbon to generate hyperlinks based on the Document ID." Title="Document ID Link">
  <ActivationDependencies>
    <ActivationDependency FeatureId="B50E3104-6812-424F-A011-CC90E6327318" />
  </ActivationDependencies>
  <ElementManifests>
    <ElementFile Location="Scripts\AlainDeKlerk.SP2010.DocumentIdLink.js" />
    <ElementManifest Location="Scripts\Elements.xml" />
    <ElementFile Location="Scripts\jquery-1.7.1.min.js" />
    <ElementFile Location="Scripts\qrcode.js" />
    <ElementManifest Location="DocumentIdLinkAction\Elements.xml" />
  </ElementManifests>
</Feature>

Now if a user tries to activate this feature while the Document ID Service feature is not activated he or she will receive a warning

image

The final result:

image

Now, that comes pretty close to the solution Jan offers:

This Solution Jan’s Solution
Sandboxed Solution Sandboxed Solution
Uses SharePoint Document ID Service Uses external URL shortening service
Includes QR-code Includes QR-code
Relative short URL Very short URL
Hyperlink still works after document is moved Hyperlink breaks if document is moved

 

Off course, the complete source-code can be downloaded here. This source-code is provided as if, without any guarantees.

About the author

Alain

You can leave a response, or trackback from your own site.

4 Responses to “Document ID Hyperlink with QR Code for SharePoint 2010”

  1. Renaat says:

    Looks great, exactly like I described on my blog.
    I’ll forward it to our technical guys to have a look at it.
    Well done and thanks for your message!

  2. KAS says:

    Thanks Alain!!!
    I am facing a Java Script error “Code Lenght Overflow. (548 > 512)” after clicking on the “Get document ID Link”.
    Pls. help me on this.

  3. Alain Alain says:

    @Kas:
    Try setting the javascript variable ‘typeNumber’ to a higher value (for example: 6) in the file ‘alaindeklerk.sp2010.documentidlink.js’. Using a higher typenumber allows for larger QR-codes.

  4. Cam Degrasse says:

    Hello, many thanks for your piece. It’s Much appreciated.

Leave a Reply