Omicron Llama

Coding all day, every day.

Build a Simple Web Part for Office 365 that gets data from a List

Cross-Post. Originally posted on

In this article we will create a simple web part in C# code that will access a task list and show a list of pending items. You will then be able to upload this web part to your SharePoint environment on Office 365!

This tutorial combines others on the internet that shows the basics of creating web parts, and the basics of interrogating lists using the SharePoint Object Model, with the final flourish of uploading our finished web part to the cloud for use.

We’ll use Visual Studio 2010 to create the web part, and we’ll assume that you have a development environment available, be it with SharePoint Foundation installed on Windows 7 on your PC, or on a Virtual Machine.
First of all, open up Visual Studio 2010, and go ahead and create an Empty SharePoint Project. For this tutorial, I will use the project name “SharePoint365.WebParts”.

Ensure that the URL to an existing web site on your development environment is selected in the next dialogue box, and that you are deploying as a Sandboxed Solution. The fact this web part will be deployed as a sandbox solution means you can upload the resulting package to your Office 365 environment.

Right click on the project file at the top of the Solution Explorer, select Add and click New Item.

In the Add New Item Dialog Box, ensure that SharePoint 2010 is selected from the left hand Template and select Web Part from the list of installed templates (making sure you don’t select Visual Web Part). Give it a name, for this tutorial I’ll use OutstandingTasks.

When you click Add, Visual Studio will set up the project with everything you need to get started, including adding .NET Assembly references that are needed, a Feature folder for your web part, and a Package folder which contains your feature. Your web part exists in what’s known as a “SharePoint Project Item” folder, and this also includes a .webpart declaration file (which makes your web part available when you add a new web part to a page), the “.cs” source code file for your web part and an Elements.xml file, which tells SharePoint where to copy the .webpart file.

Before we crack on and write some code, lets tidy up some names of things to make the visual aspect a bit more relevant in SharePoint itself.

From the Solution Explorer, click the “Feature1” folder (not the Feature1.feature file), and hit F2. The folder should be highlighted to let you rename it. For this tutorial, I’ll call this “Outstanding Tasks Web Part Feature”.

Once renamed, hit enter, and double click this file to open the Feature Designer. Set the “Title” to the same as the name of the folder (Outstanding Tasks Web Part Feature), and ensure the “Scope” is set to “Site”. Notice your Web Part Project Item is also automatically added to the Feature.

Finally, let’s make sure the web part has a relevant name when we’re adding it to a page.

Double click the OustandingTasks.webpart file in the solution explorer.

When it opens up, you’ll see it’s an XML file. This tells SharePoint what code to use for your web part, and also sets various properties – for here we only have Title and Description, so let’s set these to something more relevant.

Finally, we can now write some code!
Save and close the other windows that are open, and double click the OustandingTasks.cs file if it’s not already open.
Inside the CreateChildControls method, paste the following code:

Let’s go through the code and explain what’s going on.

Here we get an object which represents our Task list. From the right hand side we’re looking at the current website in which the web part will be placed. Inside the square brackets we’re passing in the “Internal Name” of the List (which you can see if you look at the URL whilst you’re in the list):

One thought on “Build a Simple Web Part for Office 365 that gets data from a List

Leave a Reply

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