Tuesday, January 15, 2013

SharePoint Document Library: Add document link or Link to a Document

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:
image

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:
image
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:
image
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.

No comments:

Post a Comment