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/
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/
Will alerts work with these document links?
ReplyDeleteWhat do you do when you get the error that the specified link contains too many characters...?
ReplyDeleteIn my sit when i click on a link for a document say a word document; instead of directly opening; it first shows the pop up asking whether to open or to save file... What i should i do if user can directly open that file without displaying that pop up or prompt box that asks whether to save or to open it?
ReplyDeleteCan this link be modified to use the DOC_ID, rather than the fully qualified path? That way the links will persist even if the source document is moved to another folder.
ReplyDeletethx men..for your guide, you safe my life!!
ReplyDeleteThe library I have to link to has different permissions and not all users can access the linked documents. Is there a work around for that?
ReplyDelete