2017-02-14 119 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"] 
    } 
} 
相關問題