2017-02-14 105 views
1

我想使其非常简单。我有一个新的,全新的asp.net C#web表单,后面的代码显示如下。C#从SharePoint 2013文档库中检索文档列表

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) 
    { 

    } 
} 

我有一个SharePoint 2013网站,其文档库中包含几个文档,其中包含几列元数据。

如何使它显示在网页上,指向每个文档的链接以及来自库中每个文档的列的元数据。对于集成SharePoint和ASP.Net的任何工作,我都是超级新手。

请帮忙。

Andy

回答

1

Sharepoint有3个可以使用的API。看看这里:https://msdn.microsoft.com/en-us/library/office/jj164060.aspx

您可能想通过CSOM库(Microsoft.SharePoint.Client)使用client.svc服务,只是因为它是最容易启动和运行的。不要使用旧的asmx API,因为它已被弃用。还有第三个选项 - REST - 但它不提供CSOM所有的功能。

下面是一些粗略的代码,显示基础知识。代码中没有涉及很多细微差别(SharePoint非常复杂),因此您还需要在线查找一些其他信息。

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"] 
    } 
} 
相关问题