首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Visual Studio 2005 建立主控台應用程式並連接 SharePoint 完整步驟

     編輯:關於.NET

前言

本文記錄在 Visual Studio 2005 環境下,從零建立主控台應用程式、加入 Microsoft.SharePoint 參考、撰寫程式碼列舉站台集合下所有網站與清單數量的完整步驟,適用於早期 SharePoint 開發入門參考。

一、建立專案

  1. File → New → Project
  2. 語言選 Visual C#(或 VB),專案類型選 Windows
  3. 範本選 Console Application
  4. 設定位置與名稱,確定

二、加入 SharePoint 組件參考

  1. 方案總管右鍵 References → Add Reference
  2. .NET 標籤下選取 Microsoft.SharePoint(通常位於 `C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll`)
  3. 確定

三、撰寫程式碼

3.1 C# 版本

using System;
using Microsoft.SharePoint;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite siteCollection = new SPSite("https://Server_Name"))
            {
                SPWebCollection webs = siteCollection.AllWebs;
                foreach (SPWeb web in webs)
                {
                    Console.WriteLine($"Site: {web.Name} Lists: {web.Lists.Count}");
                    web.Dispose(); // 重要:釋放非受控資源
                }
            }
            Console.WriteLine("Press ENTER to continue");
            Console.ReadLine();
        }
    }
}

3.2 VB.NET 版本

Imports Microsoft.SharePoint

Module Module1
    Sub Main()
        Dim siteCollection As New SPSite("https://Server_Name")
        Dim webs As SPWebCollection = siteCollection.AllWebs
        For Each web As SPWeb In webs
            Console.WriteLine("Site: " + web.Name + " Lists: " + web.Lists.Count.ToString())
            web.Dispose()
        Next
        Console.WriteLine("Press ENTER to continue")
        Console.ReadLine()
    End Sub
End Module

四、執行與驗證

  1. Debug → Start Debugging (F5)
  2. 主控台輸出所有網站名稱與清單數量
  3. 確認無 `FileNotFoundException`(通常為 x64/x86 平台不符或 SharePoint 未安裝在同一台機器)

五、常見問題排查

錯誤原因解決
FileNotFoundException專案目標平台為 x64,SharePoint 組件為 x86專案屬性 → Build → 勾選 "Prefer 32-bit" 或明確設定 Platform target = x86
SPRequest not disposed未呼叫 `web.Dispose()` 或 `siteCollection.Dispose()`使用 `using` 區塊或 `finally` 確保釋放
Access Denied執行身分無 SharePoint 權限以農場管理員/網站集合管理員身分執行,或設定 AppPool 身分

六、延伸應用

  • 列舉清單項目:`web.Lists["ListName"].Items`
  • CAML 查詢:`SPQuery` + `list.GetItems(query)`
  • 批次更新:`SPListItemCollection` + `item.Update()`

結語

雖然 VS2005/SharePoint 2007 已過支援週期,但此主控台應用程式範本仍為理解 SharePoint 伺服器端物件模型(SSOM)基礎架構的最佳切入點。現代開發請改用 .NET Core + CSOM/REST API 或 SPFx。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved