My specifications:

Windows Server 2003
Visual Studio 2008
Windows Sharepoint Services 3.0
visual studio 2008 extension for Sharepoint services 3.0

I'm creating a web part with a SPGridView. I want one of the columns of the SPGridView to display links to documents of SharedDocumentLibrary.

How to read contents of SharedDocumentLibrary and display them as links in my webpart ?

I think we can achieve this through 'Link to Document' feature of SharePoint. But I don't know how? Can somebody give me some example.

My requirement is exactly this. Linking documents in SharePoint. How do I achieve it through source code ?


You could develop a web-part in Visual Studio which gets the list or document library you need and displays the content in a repeater or grid view, if you need an example of how that code might look, see this link.


Loads of examples of how to acccess a list using the various ways you can do it..

  1. Web services
  2. JSON
  3. Silverlight etc
  4. Rest
  5. Javascript

Hope this helps.


  • Thanks for the reply. Its really good list. It could have been better if I could open them in VS2008. :( Mar 19 '13 at 7:00

When I upload a file to Sharepoint, I save a record in my App's database with the file name and location, usually this will be in the format http://198.mySharepointServer/LibraryName/Filename.txt. This is then much easier and quicker than accessing Sharepoint programatically, running code and then retrieving the files url because you can just access a local database.

Similarly if you have the file's filename and you are only using 1 Sharepoint library the url shouldn't change so you can just use that url throughout and simply attach /filename.txt to the end of that url.

Here's a short extract of some code I used to retrieve the url programatically by searching with a document ID. Please note this was coded by a colleague so I just followed the logic, you can do the same and see what pieces you could use.

using (ClientContext spclient = new ClientContext(libraryUrl))
    CamlQuery query = new CamlQuery();
    libraryName = Properties.Settings.Default.Members;
    spclient.Credentials = creds;
    Web site = spclient.Web;
    List docList = site.Lists.GetByTitle(libraryName);


    var filterList = new FilterOptionList();
    var DocID = new FilterOptions();
    AddFilters(spclient, docList, filterList, DocID, new string[] { DocumentID }, "Doc Ref No");

    StringBuilder q1 = new StringBuilder();
    query.ViewXml = queryString.ToString();
    ListItemCollection collListItem = docList.GetItems(query);


    if (collListItem.Count != 0)
    spclient.Load(docList.Fields.GetByTitle("Doc Ref No"));
    var DocRefNumColName = docList.Fields.GetByTitle("Doc Ref No").InternalName;

    foreach (ListItem item in collListItem)
        if (item.FileSystemObjectType == FileSystemObjectType.File)

        domain = item.File.ServerRelativeUrl;
        if (domain != "")
            DocumentURL = libraryUrl + domain;
            IsInMembers = true;
        else IsInMembers = false;
    catch (Exception ex)
        DocumentURL = "";

Please vote as an answer if this helped you.

  • Hi, thanks for the reply. I really couldn't understand this code. Mar 20 '13 at 6:17

In order to to add "Link to document" you need to do a couple things.

1) Turn ON the "Management of Content Types". This will be in the List/Library Settings -> Advanced Settings -> Allow management of content types? -> Yes

2) Add the content type "Link to a Document". You can go to your List/Library Settings and click Add from existing site content types -> select "Link to Document" then click Add.

3) You can now have a list/library item that links to another document! Go to your list/library and click New Document drop-down in the ribbon, then choose "Link to a Document"

  • thanks for the reply. but I want it through source code as I'm developing a web part. Mar 19 '13 at 5:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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