使用 CSOM 与共享点网站连接

本文关键字:网站 连接 共享 CSOM 使用 | 更新日期: 2023-09-27 18:33:43

我的代码如下所示

  try
    {
        string strUserName="abc";
        string strPassword="123";
        ClientContext context = new ClientContext(siteurl);
        var credentials = new NetworkCredential(strUserName, strPassword,"Ext");

        context.Credentials = credentials;
        // The SharePoint web at the URL.
        Web web = context.Web;
        // We want to retrieve the web's properties.
        context.Load(web);
        // Execute the query to the server.
        context.ExecuteQuery();
        // Now, the web's properties are available and we could display 
        // web properties, such as title. 
        System.Console.WriteLine("Web Title");
        System.Console.WriteLine(web.Title);
    }
    catch (Exception ex)
    {
    }

它给出错误"无法连接远程服务器"

使用 CSOM 与共享点网站连接

首先要确保正确传递以下实体:

  • 网站网址
  • str用户名
  • strPassword
  • "分机"

如果您尝试连接到SharePoint Online 2013,则必须将"NetworkCredential"替换为"SharePointOnlineCredentials"。下面是它的代码片段:

    string strUserName="abc";
    string strPassword="123";
    SecureString ssPwd = new SecureString();
    strPassword.ToList().ForEach(ssPwd.AppendChar);
    ClientContext context = new ClientContext(siteurl);
    SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);
    context.Credentials = credentials;
    // The SharePoint web at the URL.
    Web web = context.Web;
    // We want to retrieve the web's properties.
    context.Load(web);
    // Execute the query to the server.
    context.ExecuteQuery();

要接受自签名证书:

ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;