SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a web application which requires to upload files to a SharePoint document library. I got that working. Now, I need to know how to code using C# in my web application to create a "Link to a Document" to the file that I just uploaded. I know how to create a "Link to a Document" manually in SharePoint document library but i don't know how to do that programmingly.

The reason is that we want a single document to display in multiple categories when we group them in the Sharepoint library view.

Please help. Thank you in advance.

To help clarify my question please see code snippet and comments below:

            string libURL = string.Empty;  
            TASite taSite = TASiteController.GetByID(new Guid(doc.TASiteId.ToString()));
            libURL = taSite.SharePointURL;
            if (libURL == string.Empty)
                return null;

            ClientContext remoteCtx = new ClientContext(libURL);
            remoteCtx.AuthenticationMode = ClientAuthenticationMode.Default;
            string sharePointUsername = ConfigurationManager.AppSettings["SharePointUsername"];
            string sharePointUserpwd = ConfigurationManager.AppSettings["SharePointUserPassword"];
            remoteCtx.Credentials = new System.Net.NetworkCredential(sharePointUsername, sharePointUserpwd);
            Web site = remoteCtx.Web;


            byte[] filecontent;
            List<Attachment> attachments = AttachmentController.GetAttachmentsByDocId(doc.DocumentId, Sections.DocInfo);
            if (attachments.Count == 0) return null;

            filecontent = (byte[])attachments[0].Data;

            FileCreationInformation docFile = new FileCreationInformation();
            docFile.Content = filecontent;
            docFile.Url = attachments[0].Name;

            docFile.Overwrite = true;

            List docLib = site.Lists.GetByTitle("Procedure"); //<-- the name of the SharePoint library
            File uploadFile = docLib.RootFolder.Files.Add(docFile);          
            Microsoft.SharePoint.Client.ListItem listItem = uploadFile.ListItemAllFields;

            string libName = "Procedure";
            listItem["DocType"] = libName;
            //listItem["Owner"] = doc.Owner;
            listItem["RefNo"] = doc.RefNo;

            // Get category information
            List<Category> categories = CategoryController.GetCategoriesByDocId(doc.DocumentId);
            //foreach (Category category in categories)


            listItem["Library"] = categories[0].Library;
            listItem["Bookcase"] = categories[0].Bookcase;
            listItem["Bookshelf"] = categories[0].Bookshelf;           


            string procedureURL = libURL + "/" + libName + "/" + docFile.Url; // url to the document I just uploaded.

         // But how do I create a "Link to a Document" or shortcut place in the same library as the document.

            return procedureURL;
        catch (Exception ex)
            return string.Empty;
share|improve this question
Did you get this working? – Steve Lineberry Aug 18 '14 at 15:43

This has worked for me in the past:

private SPListItem CreateLinkToDocumentPage(SPWeb web, SPList docLibrary, string pageName, string url)
    SPContentType contentType = web.AvailableContentTypes[SPBuiltInContentTypeId.LinkToDocument];

    //get full path of the document to add
    string filePath = docLibrary.RootFolder.ServerRelativeUrl;
    SPFolder currentFolder = web.GetFolder(filePath);

    SPFileCollection files = currentFolder.Files;
    string urlOfFile = currentFolder.Url + "/" + pageName + ".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' %>
                    <meta name='progid' content='SharePoint.Link' /> 
                <form id='Form1' runat='server'>
                    <SharePoint:UrlRedirector id='Redirector1' runat='server' />

    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"] = url + ", ";

    return item;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.