Click here to Skip to main content
15,868,141 members
Articles / Programming Languages / C#

Access Office 365 using a Console Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Sep 2015CPOL2 min read 19.9K   2   3
Create the Security Principal Navigate to your Office 365 tenant. This will be of the format https://{your-domain}.sharepoint.com by default.

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
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:

  • App Security ConfirmationCopy 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

Set Permissions

  • 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.

Trust the app

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)

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

The post Access Office 365 using a Console Application appeared first on SharePoint Guide.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseThis is a great article Pin
Russell Griffith20-Jul-17 11:51
Russell Griffith20-Jul-17 11:51 
QuestionNuget Package Information Pin
Member 120040806-May-16 9:04
Member 120040806-May-16 9:04 
SuggestionTitle should be changed Pin
M Rayhan29-Sep-15 18:49
M Rayhan29-Sep-15 18:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.