Link to a document in a document library when the document is stored/Updated in document library
Follow the below steps to do Link to a document using OOTB.
We use the Content Type “
Link to a Document” to link to a document that could be stored in another document library.
If the document library where you want to
use the “Link to a Document” hasn’t been setup to use Content Types,
you’ll have to click
Settings,
Document Library Settings, there you’ll need to access the
Advanced Settings.
We need to add a Content Type, but before we can do this we’ll have
to tell our Document Library that it can make use of different Content
Types. Select “
Yes” for the “
Allow management of Content Types“.
When you return to the
Document Library Settings, you’ll see a new section called
Content Types, this is where we’ll add a new Content Type, click “
Add from exisiting Content Type“.
The content type we need is “
Link to a Document“, add it here.
When you return to your
Document Library and click
new, you’ll see that you have a new option to choose from, “
Link to a Document“.
Let’s make a link to a Document in another Document Library, click “Link to a Document”. You’ll have to enter a
name for the Link and enter the
path to the Document you want to link to.
After confirming this is what your link looks like.
We made link to a document that’s saved in another document library,
whenever you make a change to the master document, you don’t need to
worry about the link, it will point to the changed document.
In the same way we can put "link to a document" links in another folder with in the Documnet library using Object Mode, Here i am using Event handler to Create Link to document when new item is Added in Document Library
using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.Text;
namespace LinkdocumentUrl.linktodocs
{
/// <summary>
/// List Item Events
/// </summary>
public class linktodocs : SPItemEventReceiver
{
/// <summary>
/// An item was added.
/// </summary>
public override void ItemAdded(SPItemEventProperties properties)
{
base.ItemAdded(properties);
base.DisableEventFiring();
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite site = new SPSite(properties.SiteId))
{
using (SPWeb web = site.AllWebs[properties.RelativeWebUrl])
//using (SPWeb web = site.OpenWeb(properties.RelativeWebUrl))
{
SPListItem _item = properties.ListItem;
//SPField field = properties.ListItem.Fields.GetFieldByInternalName("Make a Link");
SPFieldBoolean boolField = _item.Fields["Make a Link"] as SPFieldBoolean;
bool CheckBoxValue = (bool)boolField.GetFieldValue(_item["Make a Link"].ToString());
//if (_item["Make a Link"].ToString() == "true")
if (CheckBoxValue== true)
{
string documentUrl = web.Url + "/" + properties.ListItem.Url;
string documentName = properties.ListItem.DisplayName.ToString();
SPList list = web.Lists["TestDocs"];
if (list is SPDocumentLibrary)
{
SPDocumentLibrary docLib = (SPDocumentLibrary)list;
//string libraryRelativePath = list.RootFolder.ServerRelativeUrl;
//string libraryPath = site.MakeFullUrl(libraryRelativePath);
SPFolder folder = web.GetFolder(site.MakeFullUrl(list.RootFolder.ServerRelativeUrl) + "/Public");
SPFileCollection filecoll = folder.Files;
if (docLib.ContentTypesEnabled)
{
SPContentType myCType = list.ContentTypes["Link to a Document"];
if (myCType != null)
{
//replace string template with values
string redirectAspx = RedirectAspxPage();
redirectAspx.Replace("{0}", documentUrl);
SPFile file = folder.Files.Add(documentName + ".aspx", UTF8Encoding.UTF8.GetBytes(redirectAspx));
SPListItem item = file.Item;
item["ContentTypeId"] = myCType.Id;
SPFieldUrlValue fieldUrl = new SPFieldUrlValue() { Description = documentName, Url = documentUrl };
item["URL"] = fieldUrl;
item.Update();
item.UpdateOverwriteVersion();
folder.Update();
base.EnableEventFiring();
}
}
}
}
}
}
});
base.ItemAdded(properties);
}
public static string RedirectAspxPage()
{
//StringBuilder builder = new StringBuilder();
const string format = @"<%@ Assembly Name='Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' %>
<%@ 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 xmlns:mso='urn:schemas-microsoft-com:office:office' xmlns:msdt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'>
<Head> <META Name='progid' Content='SharePoint.Link'>
<!--[if gte mso 9]><xml>
<mso:CustomDocumentProperties>
<mso:URL msdt:dt='string'>{0}, {0}</mso:URL>
<mso:ContentType msdt:dt='string'>Link to a Document</mso:ContentType>
</mso:CustomDocumentProperties>
</xml><![endif]-->
</head>
<body>
<form id='Form1' runat='server'>
<SharePoint:UrlRedirector id='Redirector1' runat='server' />
</form>
</body>
</html>";
StringBuilder builder = new StringBuilder(format.Length + 400);
builder.AppendFormat(format, typeof(SPDocumentLibrary).Assembly.FullName);
return builder.ToString();
}
}
}
Reference from
http://www.sharepointology.com/general/link-to-a-document-in-another-document-library/