Accessing SharePoint Online with CSOM using a console app

Before we construct our calls directly to the REST APIs SharePoint exposes, let's do a quick exercise using the traditional CSOM approach. This is part of the SDK, and depending on your usage, it will access SharePoint through the SOAP-based Web Services, such as sites.asmx that resides in /_vti_bin/sites.asmx.

Let's try creating a simple console application first to see how things were working until now:

  1. Open Visual Studio 2015/2017 and create a new project from File | New | Project...:
  1. Select a Console App from the list of available templates, as follows:
  1. Name the app as you like, and click OK.
  2. If you choose C# as the programming language, a Program.cs open with a skeleton class. You'll need to get the SharePoint Online Client Components SDK first; to do it, click on Tools |NuGet Package Manager | Package Manager Console:
  1. The Package Manager Console opens in a separate window; by default, it opens in the bottom area of Visual Studio. Type the following command to initiate the installation of the Client Components SDK for your project:
Install-Package Microsoft.SharePointOnline.CSOM 
  1. If you're not sure what the NuGet package name is, you can always search for the correct package within the Package Manager Console with the following command:
Find-Package keyword 

To find all packages that are intended for SharePoint development, use Find-Package sharepoint:

  1. Its installation takes a moment and confirms that packages are now added to your project:
  1. Verify within Solution Explorer that your project now has references to additional libraries:

We are now ready to start implementing the actual code for our small console app; to do that, follow these steps:

  1. Add a using statement for Microsoft.SharePoint.Client at the beginning of Program.cs:
using Microsoft.SharePoint.Client; 
  1. Next, to keep our sample code clean and simple, let's introduce a few variables in the Main() method of our program to hold the credential information:
var username = "[email protected]"; 
var password = "password123"; 
var tenanthost = "domain";
  1. Replace the credential information with proper values for your own tenant. You'll also need to convert the password to a SecureString, so let's add that directly underneath:
var securePassword = new SecureString(); 
foreach (var c in password) 
{ 
securePassword.AppendChar(c); 
} 
  1. Then, you'll just need to put these two together with a SharePointOnlineCredentials() method, which is part of the Microsoft.SharePoint.Client namespace:
var credentials = new SharePointOnlineCredentials(username, securePassword); 
var tenant = "https://" + tenanthost + ".sharepoint.com"; 
  1. Now that we have your credentials, in order to authenticate with our SharePoint Online tenant, the only thing needed is a ClientContext, which helps us to retrieve the data we need. In the following bit of code, we will use the ClientContext to query for the Title property of the root website of our tenant:
using (var cc = new ClientContext(tenant)) 
{ 
cc.Credentials = credentials; 
      cc.Load(cc.Web, w => w.Title); 
      cc.ExecuteQuery(); 
 
      Console.WriteLine(cc.Web.Title); 
} 

When we run this, the output is a simple printout of our SharePoint site's title, as follows:

Looking at this through Fiddler, a tool that allows us to inspect the traffic we're generating, we will note that the call goes directly to

The following is the request we will get:

The preceding screenshot shows us that we only retrieve the Title property of the SP.Web-object.

So, with this simple exercise, we've started calling SharePoint Online directly with the classic SOAP-based Web Services. Let's next move on to REST-based APIs that are a bit more modern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.118.250