In SharePoint, there’s a way to add document link. Many of use not aware of it. To add the document link you need to add “Link to a Document” Content type in your library. By default content type modification is disabled in any list/library. To enable editing content type you need to go to list/library settings. Once you have enabled the content type editing, you can add/remove content type.
Enable Content Type Modification in List/Library
To enable content type editing go to list/library settings page. Then from the list settings page, Click “Advanced Settings” and in the advanced settings page, click “Yes” for option “Allow Management of Content Types”. Enabling this option will allow you to modify content type settings. Click Ok after selecting Yes option.
After coming back to list settings page you will find the content modifying option as shown below:
Add Link To Document Content Type to the library
Now from the ‘Add from Existing site content types’ link as shown in the image above, you can add the “Link to Document” content type. click “Add from existing site content types” and from the page you have landed you can the content type as shown below:
After adding the content type go back to document library page. You’ll find that when you’ll try to add new item, you’ll get the ‘Link to a Document’ option as shown below:
With this link to document item, you can add links to content not only in sharepoint but non-SharePoint content from other sites.
Programmatically Add Link to a document using SharePoint Object Model
Once you enable the ‘link to document’ support in any document library you can add the document programmatically. The following code snippet shows how to add a link to a document content in a library.
public static void AddDocumentLink(string webUrl, string libraryName,string documentPath, string documentName, string documentUrl) { using (var site = new SPSite(webUrl)) { using (var web = site.OpenWeb()) { var contentType = web.AvailableContentTypes["Link to a Document"]; var docLibrary = web.Lists[libraryName]; //get full path of the document to add var filePath = docLibrary.RootFolder.ServerRelativeUrl; if(!string.IsNullOrEmpty(documentPath)) { filePath += "/" + filePath; } var currentFolder = web.GetFolder(filePath); var files = currentFolder.Files; var urlOfFile = currentFolder.Url + "/" + documentName + ".aspx"; const string format = @"<%@ Assembly Name='{0}' %> <%@ Register TagPrefix='SharePoint' Namespace='Microsoft.SharePoint.WebControls' Assembly='Microsoft.SharePoint' %> <%@ Import Namespace='System.IO' %> <%@ Import Namespace='Microsoft.SharePoint' %> <%@ Import Namespace='Microsoft.SharePoint.Utilities' %> <%@ Import Namespace='Microsoft.SharePoint.WebControls' %> <html> <head> <meta name='progid' content='SharePoint.Link' /> </head> <body> <form id='Form1' runat='server'> <SharePoint:UrlRedirector id='Redirector1' runat='server' /> </form> </body> </html>"; var builder = new StringBuilder(format.Length + 400); builder.AppendFormat(format, typeof(SPDocumentLibrary).Assembly.FullName); var properties = new Hashtable(); properties["ContentTypeId"] = contentType.Id.ToString(); var file = files.Add(urlOfFile, new MemoryStream(new UTF8Encoding().GetBytes(builder.ToString())), properties, false, false); var item = file.Item; item["URL"] = documentUrl + ", "; item.UpdateOverwriteVersion(); } } }
The above code snippet is the modified version of what SharePoint does when you add a document link from UI. I have used reflector to view the code and placed here a modified version. I have tested this against SharePoint 2010.
Even though there are SharePoint features that many users are unaware of, they might not always meet the needs of your business. This is where SharePoint web parts can make your life even simpler. There are options of organisational charts that help with communication in a business, and web parts that allow you to include maps and videos on your SharePoint site.
ReplyDeleteThanks Sohel. This blog is a life saver! I'm a naive and disinterested user who needs to link to documents, and I was totally stumped until I found this blog posting (http://ranaictiu-technicalblog.blogspot.com/2010/07/sharepoint-document-library-add.html).
ReplyDeletea life saver. thanks
ReplyDeleteyou said you have used a reflector to see this code. Can you please explain how to do this? in future for other problems, it may prove to be helpful.
ReplyDelete@Sumeet, its very easy. Just download .net reflector, run it and then drag the Microsoft.SharePoint.dll (or any other .net developed dll) on the reflector.
ReplyDeleteHi, i used this content type but found a strange behavior when I click the send to button to send to a record library. the link was broken. do you know is this expected?
ReplyDeletethanks
Please check the SharePoint log for error details.
ReplyDeleteHi Sohel, the Link to Document Content Type is not available in my list of Content Types to add to a library. How do I make it available?
ReplyDeleteI'm not sure but the option might be available only in SharePoint server?
ReplyDeleteHi Sohel - great info. I added as suggested but, was limited in the number of charecters in the URL link to the shared document (on the same sharepoint site in a different folder). Any suggestions here? Thanks!
ReplyDeletewhat's the limitation in url? If it's public site you can try tinyurl.
ReplyDeleteHi,
ReplyDeleteNice post!
Btw, do you realise that a document with normal "Document" content type then change to "Link to a Document" content type, there is a require column named "URL" to fill up.
In fact, is not a need for a normal document item to have the URL value. Is SharePoint bugs or not?
What is your opinion?
As soon as you change the type from 'document' to 'link to document', the URL field gets required. I think this is expected.
ReplyDeleteYe, that's how the column works.
ReplyDeleteMay i say that there is a simple way for linking documents between libraries.
Check this free feature in codeplex.
http://easylinkdocs.codeplex.com/
liked the easylinkdocs but need the enhance but couldn't find the source code.. anyone knows how to get the source code.
ReplyDeleteSohel,
ReplyDeleteHow did you test you code programmatically? Can you give me some idea? Don't want to use any custom action.
Please help me.
Thanks in advance
Thanks so much for the information! It was truly a great help!!!
ReplyDeleteThanks for this. I was having trouble adding a link and after reading your tip I was done in about two minutes. Thank you.
ReplyDeleteThanks very much but I want to create a templates library and I can't add several times "link to a document"...only 1 time ...
ReplyDeleteany idea or really impossible???
Many thanks in advance for your support
Hi Sohel,
ReplyDeleteIs it true that a link to a document is only read only and in order to edit the doc you need to edit the original?
I did not know if there was a way to change the setting to edit a link to another document within SharePoint.
The metadata (columns in document library) are not editable for the content type 'link to document'. I can hack it and edit some columns in the datasheet view, but is there a way to see/edit the fields on the 'edit properties' screen?
ReplyDeleteIs it possible to have the link open in a new window?
ReplyDeleteI viewed the ASPX file it creates, but the url is in the following tag format: http://www.google.com.
Thanks so much for the information! It was truly a great help!!!
ReplyDeleteshare tips