Document ID Hyperlink with QR-Code for SharePoint 2010
January 9th, 2012 Alain
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 likehttp://bit.ly/urlwiki orhttp://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>
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.
The new script Elements.xml:
1
2
3
4
5
6
7
8
| <? xml version = "1.0" encoding = "utf-8" ?> < 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 > |
1
|
And the new javascript code (I highlighted the important changes):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
| 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:
1
2
3
4
5
6
7
8
9
10
11
12
| < 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
The final result:
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 |
No comments:
Post a Comment