Code Monkey home page Code Monkey logo

aspnet-todo-rest-sample's Introduction

Microsoft Graph Excel REST API ASP.NET to-do list sample

This sample shows how to read and write into an Excel document stored in your OneDrive for Business account by using the Excel REST APIs.

Prerequisites

This sample requires the following:

Register the application

  1. Determine your ASP.NET app's URL. In Visual Studio's Solution Explorer, select the Microsoft-Graph-ExcelRest-ToDo project. In the Properties window, find the value of SSL URL. Copy this value.
    Application URL

  2. Navigate to the the Azure portal - App registrations to register your app. Login using a Work or School Account.
    New registration Page

  3. Select New registration. On the Register an application page, set the values as follows.

  • Set Name to Microsoft-Graph-ExcelRest-ToDo.
  • Set Supported account types to Accounts in any organizational directory.
  • Leave Redirect URI empty.
  • Choose Register.
    Application Registration
  1. On the Microsoft-Graph-ExcelRest-ToDo page, copy and save the values for the Application (client) ID.
    Application Registration

  2. Select Certificates & secrets under Manage. Select the New client secret button. Enter a value in Description, select any option for Expires and choose Add.
    Certificates & secrets

  3. Copy the client secret value before leaving the page. You will need it in the next step.
    Certificates & secrets

Configure the app

  1. Open Microsoft-Graph-ExcelRest-ToDo.sln file.
  2. In Solution Explorer, open the Web.config file.
  3. Replace ENTER_YOUR_CLIENT_ID with the client ID of your registered Azure application.
  4. Replace ENTER_YOUR_SECRET with the key of your registered Azure application.

Run the app

  1. Press F5 to build and debug. Run the solution and sign in with your organizational account. The application launches on your local host and shows the starter page. Graph and AAD authentication starter page

    Note: Copy and paste the start page URL address http://localhost:21942/home/index to a different browser if you get the following error during sign in: AADSTS70001: Application with identifier ad533dcf-ccad-469a-abed-acd1c8cc0d7d was not found in the directory.

  2. Choose the Click here to sign in button in the middle of the page or the Sign in link at the top right of the page and authenticate with your Office 365 account.
  3. Select the ToDoList link from the top menu bar.
  4. The application checks to see if a file named ToDoList.xlsx exists in the root OneDrive folder of your O365 account. If it doesn't find this file, it uploads a blank ToDoList.xlsx workbook and adds all of the necessary tables, columns, and rows, along with a chart. After finding or uploading and configuring the workbook, the application then displays the task list page. If the workbook contains no tasks, you'll see an empty list.
  5. If you're running the application for the first time, you can verify that the application uploaded and configured the ToDoList.xlsx file by navigating to https://yourtenant.sharepoint.com, clicking on the App Launcher "Waffle" at the top left of the page, and then choosing the OneDrive application. You'll see a file named ToDoList.xlsx in the root directory, and when you click on the file, you'll see worksheets named ToDoList and Summary. The ToDoList worksheet contains a table that lists each "to-do" item, and the Summary worksheet contains a summary table and a chart.
  6. Select the `Add New' link to add a new task. Fill in the form with the task details.
  7. After you add a task, the app shows the updated task listing. If the newly added task isn't updated, choose the Refresh link after a few moments. Updated task listing
  8. Choose the `Charts' link to see the breakdown of tasks in a pie chart that was created and downloaded by using the Excel REST API.

Breakdown of tasks in a pie chart

Contributing

If you'd like to contribute to this sample, see CONTRIBUTING.MD.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

Questions and comments

We'd love to get your feedback about the Microsoft Graph Excel REST API ASP.NET MVC sample. You can send your questions and suggestions to us in the Issues section of this repository.

Questions about Office 365 development in general should be posted to Stack Overflow. Make sure that your questions or comments are tagged with [MicrosoftGraph].

Additional resources

Copyright

Copyright (c) 2019 Microsoft. All rights reserved.

aspnet-todo-rest-sample's People

Contributors

andrewjmay avatar elinorw avatar irvinesunday avatar jasonjoh avatar nokafor avatar o365devx avatar paulstubbs avatar ricalo avatar titusgicheru avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aspnet-todo-rest-sample's Issues

work with Excel file using sharepoint document library

I downloaded and customized this project. this is working fine without any issue with onedrive excel files/items. but i have to do this using office 365 sharepoint online only. which is not working.
i am able to get file id using service point as follows:
var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items";

but i am not sure how should i define workbook end point url with sharepoint to update/manipulate same excel file data. current workbook end point url as follows(which is not working):
var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook";

my complete method to update cell value in excel file on sharepoint as follows:

public static async Task UpdateExcelCellValue_Sharepoint(string accessToken, string cellAddress, string val) { string worksheetName = "Parameters"; var serviceEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var filesResponse = await client.GetAsync(serviceEndpoint + "?$select=name,id"); if (filesResponse.IsSuccessStatusCode) { var filesContent = await filesResponse.Content.ReadAsStringAsync(); JObject parsedResult = JObject.Parse(filesContent); foreach (JObject file in parsedResult["value"]) { var name = (string)file["id"]; if (name.Contains("173")) { fileId = (string)file["id"]; break; } } } else { //Handle failed response } //Set up workbook and worksheet endpoints var workbookEndpoint = "https://graph.microsoft.com/beta/sites/mydomain.sharepoint.com:/foa/bd:/lists/Budget/items/" + fileId + "/workbook"; var worksheetsEndpoint = workbookEndpoint + "/worksheets"; var patchMethod = new HttpMethod("PATCH"); var summaryTableRowJson = "{" + "'values': '" + val + ".csv'" + "}"; var colNamePatchBody = new StringContent(summaryTableRowJson); colNamePatchBody.Headers.Clear(); colNamePatchBody.Headers.Add("Content-Type", "application/json"); var colNameRequestMessage = new HttpRequestMessage(patchMethod, worksheetsEndpoint + "('" + worksheetName + "')/range(address='Parameters!B2')") { Content = colNamePatchBody }; var colNameResponseMessage = await client.SendAsync(colNameRequestMessage); }
any help would be highly appreciated.

Cannot integrate your example code to ASP.NET Core

Hello everyone. I ran the example application and followed the steps described in the documentation, and the application ran well, I could see the graph explorer and performed some queries well enough.

Now, I have an ASP.NET Core application with Work & School account login, as generated by the Visual Studio 2015 Update 3 wizard. I'm trying to integrate your example, but alas I can't. For instance, copying SessionTokenCache won't work, because of the usage of System.Web.dll, which for some reason I cannot reference in my project.

I created my own version, but I'm not sure whether this will work as intended (see code at the end).

Do you have some guidelines as to how to run the example for ASP.NET Core? Or do you have some example in ASP.NET Core (I haven't been able to find one).

Thanks in advance.

public class SessionTokenCache
    : TokenCache
{
    private HttpContext context;
    private static readonly object FileLock = new object();
    private readonly string CacheId = string.Empty;
    public string UserObjectId = string.Empty;

    public SessionTokenCache(string userId, HttpContext context)
    {
        this.context = context;
        this.UserObjectId = userId;
        this.CacheId = UserObjectId + "_TokenCache";

        AfterAccess = AfterAccessNotification;
        BeforeAccess = BeforeAccessNotification;
        Load();
    }

    public void Load()
    {
        lock (FileLock) {
            Deserialize((byte[])context.Session.Get(CacheId));
        }
    }

    public void Persist()
    {
        lock (FileLock) {

            // Reflect changes in the persistent store.
            var bytes = Serialize();
            var x = System.Text.Encoding.UTF8.GetString(bytes);
            context.Session.Set(CacheId, Serialize());

            // After the write operation takes place, restore the HasStateChanged bit to false.
            HasStateChanged = false;
        }
    }

    public override void Clear(string clientId)
    {
        base.Clear(clientId);
        context.Session.Remove(CacheId);
    }

    // Triggered right before MSAL needs to access the cache.
    // Reload the cache from the persistent store in case it changed since the last access.
    private void BeforeAccessNotification(TokenCacheNotificationArgs args)
    {
        Load();
    }

    // Triggered right after MSAL accessed the cache.
    private void AfterAccessNotification(TokenCacheNotificationArgs args)
    {
        // if the access operation resulted in a cache update
        if (HasStateChanged) {
            Persist();
        }
    }
}

Excel "functions" vs. Rest API "functions" documentation

It is stated that only only excel functions work in MS graph. However,
the Graph/Rest Api has extends its ability in that you can select/copy objects (https://docs.microsoft.com/en-us/graph/excel-write-to-workbook)

Where can I find all the available "abilites" or "functionality" of Graph/Rest API? I have spent ~an hr searching for a list, but am only able to find scatered functionality like selecting a cell or range (https://stackoverflow.com/questions/45782087/sharepoint-2013-rest-api-call-to-extract-excel-specific-cells?rq=1 )

App not listing/adding tasks entries

Hi,
I'm preparing the demo for the SPSNYC tomorrow and the app is not adding/listing entries for tasks.
The issue is caused by the fact that tables('toDoList') gives 404 in the excel helper.
I Had to replace it by tables('1') to get it to work.
I wonder if it's a temporary issue with the beta graph API or if I should submit a PR?
Also it might be related to the fact that the user I'm testing against in office 365 is set up in french.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.