Access Office 365 using a Console Application

Create the Security Principal

Navigate to your Office 365 tenant. This will be of the format https://{your-domain}.sharepoint.com by default.

Open the application page https://{your-domain}.sharepoint.com/_layouts/15/appregnew.aspx

App Security Principal

  • Click on the Generate buttons for both the Client Id and Client Secret.
  • Enter a title
  • Enter the app domain as localhost if you are testing out your app. This allows the request to come from any domain. This should be fine if you are testing out the app from your local machine.
  • The redirect uri is a required field. So, we can enter something like https://localhost/default.aspx. This will not be used since we are accessing from a console application instead of an add in.
  • Click on the Create button.

A confirmation page will be shown:

  • Copy the details as we will be using this later when we create the console application

Set the permission level for the console app

Now, navigate to the application page https://{domain}.sharepoint.com/_layouts/15/appinv.aspx

  • Enter the App Id which was generated previously and click on lookup. This will populate the details of the app which we created earlier.
  • In the permissions request XML text box, enter the required permissions. In this example, since we are going to create a sub web, I have requested for FullControl over the entire site collection.

<AppPermissionRequests AllowAppOnlyPolicy="true">
<AppPermissionRequest Scope="http://sharepoint/content/sitecollection" Right="FullControl" />
</AppPermissionRequests>

You can learn more about permssions in the MSDN article Add-in permissions in SharePoint 2013

  • Click on Create
  • It will prompt to ask if you trust the app for which we have granted permissions. Click on Trust It.

Creating the console application

Now create a console application.
In the App.config add the Client Id and Client Secret as follows:

<appSettings>
<add key="ClientId" value="b3c0421b-c1bd-4858-94fe-ea141c4a4205"/>
<add key="ClientSecret" value="K83uAlwYIQL3p0aZOVesXS9h2jMhXuqaVUbs2aUhvfo="/>
</appSettings>

Now in the Program.cs we can write any client side object model code to access the Office 365 tenant! (Offcourse limited by the permissions which we gave while configuring the app above)

In order to get the TokenHelper, get the Nuget package App for SharePoint Web Toolkit. Below I have listed code to write the root web title and create a sub web.

class Program
{
static void Main(string[] args)
{
Uri siteUri = new Uri("https://{your-domain}.sharepoint.com");
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri);

string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal,
siteUri.Authority, realm).AccessToken;

using (var clientContext = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken))
{
Web currentWeb = clientContext.Web;
clientContext.Load(currentWeb, w =&gt; w.Title, w =&gt; w.Description);
clientContext.ExecuteQuery();
Console.WriteLine(currentWeb.Title);
Console.WriteLine(currentWeb.Description);

WebCreationInformation wci = new WebCreationInformation();
wci.Url = "shinyweb";
wci.Title = "New shiny web";
Web shinyNewWeb = currentWeb.Webs.Add(wci);

// Create the sub web
clientContext.ExecuteQuery();
}

Console.WriteLine("...");
Console.ReadLine();
}
}

Other popular posts:

SharePoint 2016 Installation

Using WebMethods in SharePoint with JQuery

Leave a Reply

Your email address will not be published. Required fields are marked *