Creating a 'Link to a Document' Item in a SharePoint Document Library programmatically
One of the new features available in SharePoint 2007 are content types. Content types allow you to classify content to be a particular type. By marking content as a particular content type the metadata, workflow and policies associated with that content type are leveraged instead of default policies that may apply to a standard document. One of the content types available is called 'Link to a Document'. This content type allows you to store a link to a document in a document library instead of the document itself. This comes in handy for documents that are stored in an http(s) referenceable location. When an item is added to a document library and it is classified as a 'Link to a Document' content type an .aspx page is created and stored in the document library as a file. This page is used to redirect the opening of the file to the location where the document link actually resides. The content of the .aspx file is shown below.
<%@ Assembly Name='Microsoft.SharePoint, Version=12.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">https://moss.litwareinc.com/docs/my.xls, https://moss.litwareinc.com/docs/my.xls</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>
If you want to create a 'Link to a Document' item programmatically, you can duplicate this file and replace the following string with the Url that matches the document where you want the item linked.
<mso:URL msdt:dt="string">https://moss.litwareinc.com/docs/my.xls, https://moss.litwareinc.com/docs/my.xls</mso:URL>
Create a template .txt file for this with the code below so that you can easily replace the url with your content link. Notice the reference to {0}, {0}. This will be used as a string replacement placeholder in the code snippet that will create the item in the document library. In this example I have saved the file locally to the c: drive as linktodocumenttemplate.txt.
<%@ Assembly Name='Microsoft.SharePoint, Version=12.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>
Once you have that file in place the following code snippet can be used to create the item in the document library.
using ( SPSite siteCollection = new SPSite( "https://moss.litwareinc.com" ) ) {
using ( SPWeb web = siteCollection.OpenWeb( "docs" ) ) {
SPList list = web.Lists["Sample"];
//link to the file
string fileLinkUrl = "https://moss.litwareinc.com/docs/Shared%20Documents/ConfigureIRMinWSS30.doc";
StringBuilder builder = new StringBuilder();
using ( TextReader reader = new StreamReader( @"C:\linktodocumenttemplate.txt" ) ) {
builder.Append( reader.ReadToEnd() );
}
//replace string template with values
builder.Replace( "{0}", fileLinkUrl );
//should change the name of the .aspx file per item
SPFile file = list.RootFolder.Files.Add( "link_title.aspx", UTF8Encoding.UTF8.GetBytes(builder.ToString()));
//set list item properties
SPListItem item = file.Item;
item["Content Type"] = "Link to a Document";
SPFieldUrlValue itemUrl = new SPFieldUrlValue();
itemUrl.Description = "From sample code";
itemUrl.Url = fileLinkUrl;
item["URL"] = itemUrl;
//persist changes
item.Update();
}
}