首页 > 解决方案 > microsoft.sharepoint.client.dll 中发生了 system.net.webException 类型的未处理异常

问题描述

点击这里查看照片

我正在使用visual studio 2015......我需要通过visual stuido连接到sharepoint。

我在 vs 2015 中添加了 microsoft sharepoint 在线 csom 参考

但由于错误我无法执行代码.....

标签: visual-studio-2015sharepoint-onlinecsom

解决方案


如果您使用的是 SharePoint Online,请使用 SharePointOnlineCredentials 类将凭据设置为 ClientContext 对象,以下是供您参考的代码片段:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;

namespace CSOMWinApp
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            string password = "xxxxxx";
            string account = "user@Tenant.onmicrosoft.com";
            var secret = new System.Security.SecureString();
            foreach (char c in password)
            {
                secret.AppendChar(c);
            }

            var credentials = new SharePointOnlineCredentials(account, secret);
            using (ClientContext ctx = new ClientContext("https://zheguo.sharepoint.com/sites/dev"))
            {

                ctx.Credentials = new SharePointOnlineCredentials(account, secret);
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
            }
        }
    }
}

参考:

带有 csom 的 SharePoint Online 控制台应用程序


推荐阅读