是否有任何选项可以从 .NET 8 连接到本地 SharePoint?我的 SharePoint 版本是 2013 和 2016。大多数 Microsoft 文档似乎都集中在 SharePoint Online。对此最好的身份验证方法是什么?
使用
packages
安装这些
NuGet Package Manager Console
:
Install-Package Microsoft.SharePoint.Client
Install-Package Microsoft.SharePoint.Client.Runtime
连接到本地 SharePoint 2016
using Microsoft.SharePoint.Client;
using System;
class Program
{
static void Main(string[] args)
{
// Replace with your SharePoint site URL
string siteUrl = "https://your-sharepoint-server/sites/yoursite";
// Replace with your SharePoint credentials
string username = "your_username";
string password = "your_password";
// Create a client context
using (var context = new ClientContext(siteUrl))
{
// Provide credentials
context.Credentials = new SharePointOnlineCredentials(username, ConvertToSecureString(password));
// Perform actions with the context
Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
// Example: Print web title
Console.WriteLine("Title: " + web.Title);
}
}
// Helper method to convert plain text password to SecureString
private static System.Security.SecureString ConvertToSecureString(string password)
{
var securePassword = new System.Security.SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
return securePassword;
}
}
将
https://your-sharepoint-server/sites/yoursite
替换为您的 SharePoint 2016 网站集的 URL。
将
your_username
和 your_password
替换为 the credentials that have access to the SharePoint site
。