So In this post we will learn aboutAuthenticating SharePoint Online Website using Client Side Object Model(CSOM) using C# in Windows Forms .
Authenticating any SharePoint website is first step in any application for using SharePoint Features .After authenticating you can do CRUD operations on Lists,Library and any other operations as per your need .
- Open Visual Studio
- Select New Project > Select C# Template > Windows Forms Application .Give a Suitable Name To Your Solution for example (AuthenticateSharepointOnlineUsingCSOM) and Click on OK.
- Your Project Will Be created and you will be presented with a Solution Explorer which represents the structure of your project .
- The file which is of your Concern is Form1[Design] and its Form1.cs(in which code for button click and event handlers will be written).
- For using any feature of SharePoint you have to include two basic Dynamic Link Library(dll) files which are Microsoft.SharePoint.Client and Microsoft.SharePoint.Client.Runtime . To add these dll Right click on References>Add Refrences>Extensions .Look for these 2 dll with the latest version(current version is 16.0.0.0) .Select and Add them. Be Sure to include latest versions because some classes are not there in previous versions(for example) SharePointOnlineCredentials is not there in 14.0.0.0 .So its better to use latest versions.If latest version is not there you can install by going into Tools>NugetPackageManager >Package Manager Console
- Package Manager Console window opens type Install-Package Microsoft.Sharepoint.2013.Client.16
- Open Form1[Design] and Design the form like below SnapShot.
- Make your Form1.CS code like below
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;
using Microsoft.SharePoint.Client;
using System;
using System.Data;
using System.Linq;
using System.Net;
using System.Security;
namespace AuthenticatingSharePointOnline
{
public partial class Form1 : Form
{
string URL;
string USERNAME;
string PASSWORD;
ClientContext clientContextSource;
public Form1()
{
InitializeComponent();
}
private void CONNECT_Click(object sender, EventArgs e)
{
URL = SourceURLInput.Text;
USERNAME = SourceUsernameInput.Text;
PASSWORD = SourcePasswordInput.Text;
using (clientContextSource = new ClientContext(URL))
{
/* For Converting Password to SecureString for Authenticating User To Sharepoint Online */
SecureString encodedSourcepassWord = new SecureString();
foreach (char c in PASSWORD) encodedSourcepassWord.AppendChar(c);
clientContextSource.Credentials = new SharePointOnlineCredentials(USERNAME, encodedSourcepassWord);
/* For Converting Password to SecureString for Authenticating User To Sharepoint Online */
/* For Defining the Security Channel you can see in the browser in which site is open */
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
/* For Defining the Security Channel you can see in the browser in which site is open */
// Execute the query to the server.
clientContextSource.ExecuteQuery();
Console.WriteLine("Connected");
}
}
}
}
10.Run the Code Enter Url ,UserName and Password and click on Connect . If the execution is successfull it will display Connected in Output.