如何从 .Net 8 连接到本地站点的共享点

问题描述 投票:0回答:1

是否有任何选项可以从 .NET 8 连接到本地 SharePoint?我的 SharePoint 版本是 2013 和 2016。大多数 Microsoft 文档似乎都集中在 SharePoint Online。对此最好的身份验证方法是什么?

.net-core sharepoint asp.net-core-mvc
1个回答
0
投票

使用

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

© www.soinside.com 2019 - 2024. All rights reserved.