SharePoint登录凭据

时间:2019-06-27 03:16:02

标签: c# asp.net sharepoint sharepoint-2016

目前,我尝试通过使用SharePoint从列表中检索数据来开发应用程序

现在我尝试开发登录功能,错误显示如下

基础连接已关闭:发送中发生意外错误。

此处包含我的源代码

string WebUrl = "https://xxx.sharepoint.com/sites/devspace";
            string username = txtUsername.Text;
            string pwd = txtPassword.Text;

            ClientContext context = new ClientContext(WebUrl);
            Web web = context.Web;

            SecureString passWord = new SecureString();
            foreach (char c in pwd.ToCharArray())
                passWord.AppendChar(c);
            context.Credentials = new SharePointOnlineCredentials(username, passWord);
            try
            {
                context.Load(web);
                context.ExecuteQuery();
                lblStatus.Text = "Olla " + web.Title;

            }catch(Exception ex)
            {
                lblStatus.Text = ex.Message.ToString();
            }

对此有何建议?谢谢。

已更新:

我正在使用SharePoint 2016,通过Windows Server AD登录。

1 个答案:

答案 0 :(得分:0)

如前所述,您正在使用SharePoint 2016,然后请下载并安装带有SharePoint 2016版本Nuget程序包而非SharePoint Online的CSOM:

Microsoft.SharePoint2016.CSOM

并使用以下代码段:

      ClientContext clientContext = new ClientContext("https://example.com/sites/testsite/");  
      clientContext.Credentials = new NetworkCredential("user", "password", "domain");
            // Get the SharePoint web  
            Web web = clientContext.Web;  

            // Get the SharePoint list collection for the web  
            ListCollection listColl = web.Lists;  

            // Retrieve the list collection properties  
            clientContext.Load(listColl);  

            // Execute the query to the server.  
            clientContext.ExecuteQuery();  

            // Loop through all the list  
            foreach (List list in listColl)  
            {  
                // Display the list title and ID  
                Console.WriteLine("List Name: " + list.Title + "; ID: " + list.Id);  
            }  
            Console.ReadLine();

SharePoint 2016 Premise不需要使用SharePointOnlineCredentials类,只需将用户名,密码,域作为上面的代码段传递即可。