1
votes

I want to make this really simple. I have a fresh, brand new asp.net C# web form with the code behind showing as below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

I have a SharePoint 2013 site with a document library that has a few documents in it with a few columns of metadata.

How do I make it show on the web page, a link to each document and the metadata from the columns for each document from the library. I'm super new to any work with integrating SharePoint and ASP.Net.

Please help.

Andy

1

1 Answers

1
votes

Sharepoint has 3 APIs that you could use. Have a look here: https://msdn.microsoft.com/en-us/library/office/jj164060.aspx

You probably want to use the client.svc service via the CSOM Library (Microsoft.SharePoint.Client) just because it is the easiest to get up and running on. Don't use the older asmx api because it is deprecated. There's a third option - REST - but it doesn't provide all the functionality that CSOM does.

Here's some rough code showing the basics. There are a lot of nuances that aren't covered in the code (SharePoint is complicated) so you you'll also want to find some additional information online.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Client;

public partial class _Default : System.Web.UI.Page
{
    protected string SiteUrl = "http://mysite.mydomain.com/site";
    protected string LibraryName = "MyList";

    protected void Page_Load(object sender, EventArgs e)
    {
        var context = new ClientContext(SiteUrl);
        context.Load(context.Site);
        context.ExecuteQuery();

        var list = context.Web.Lists.GetByTitle(LibraryName);

        if (list == null)
        {
            throw new ArgumentException(string.Format("List with name '{0}' not found on site '{1}'", LibraryName, SiteUrl));
        }

        context.Load(list, l => l.RootFolder.ServerRelativeUrl);
        context.ExecuteQuery();

        // Empty query. You probably want to filter on something so
        // do a search on "CAML Query". Also watch out for SharePoint
        // List View Threshold which limits # of items that can be retrieved
        var camlQuery = @"<View Scope='All'><Query></Query></View>";

        var items = list.GetItems(camlQuery);
        context.Load(items, l => l.IncludeWithDefaultProperties(i => i.Folder, i => i.File, i => i.DisplayName)); 
        context.ExecuteQuery();

       // Url for first item
       var url = SiteUrl + "/" + LibraryName + "/" + items[0]["Title"]
    }
}