The Help Desk demo, Part 6–SharePoint Announcements

The Help Desk dashboard needs to be able to display any current Announcements from the Operations team. The idea is that the Operations team has a SharePoint team site and has added a standard Announcements app.  When useful bits of information need to be made available, they add a new announcement to the app.  It would be useful to the Help Desk Operators if those announcements could be displayed in the dashboard so they’d have up to date information on the status of various systems.  If we were building a typical SharePoint app, one option would be to just embed a view of that Announcement app in our own custom app and move on to the next fun component.  However, we’re not doing that.  We’re building a stand alone web site.  We could still probably embed the Announcement app directory in our own app, but then we have to deal with cross domain problems.  Embedding the Announcement app also means that we’re more or less limited to the styling and layout of the SharePoint site, which may or may not look anything like our app.  Ok, admittedly, we could alter the CSS of the embedded app, but who wants to do that?

The Announcements app within the SharePoint site looks like this:

image

Instead, since we conveniently already have all of our authorization and access control already in place, we’ll just query the list directory, retrieve the data that’s relevant to us, and then display it however we please.  Again we’ll add a helper file to the Helpers folder and call it ‘SharePointHelper.cs’.  The SharePointHelper.cs will leverage the AuthenticationHelper.cs for the necessary permissions and then just make the call into the Announcements list.  The SharePointHelper.cs class looks like this:

using BusinessApps.HelpDesk.Models;
using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;

namespace BusinessApps.HelpDesk.Helpers
{
    public class SharePointHelper
    {
        public async Task<IEnumerable<Announcement>> GetAnnouncements()
        {
            ClientContext context = await GetClientContext(ConfigurationManager.AppSettings["OperationsWebsite"]);

            context.Load(context.Web);
            context.Load(context.Web.Lists);
            context.ExecuteQuery();

            List list = context.Web.Lists.Where(l => l.Title == "Announcements").FirstOrDefault();
            ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());

            context.Load(listItems);
            context.ExecuteQuery();

            return listItems.Select(l => new Announcement() { Title = l["Title"].ToString(), Timestamp = DateTime.Parse(l["Created"].ToString()) });
        }

        public async Task<ClientContext> GetClientContext(string url)
        {
            AuthenticationHelper authHelper = new AuthenticationHelper();

            ClientContext clientContext = TokenHelper.GetClientContextWithAccessToken(url, (await authHelper.GetToken(SettingsHelper.SharePointResource)).AccessToken);

            return clientContext;
        }
    }
}

The GetAnnouncements() function first creates a ClientContext to the SharePoint site.  This ClientContext is the same context that you’ve no-doubt seen in other blog posts around the Internet.  The function then loads the web, list, and list items information for the Announcements list and converts those to convenient C# business objects.  Calling into the function looks like this:

IEnumerable<Announcement> announcements = await sharepointHelper.GetAnnouncements();

No real pain here, right?

The Help Desk Demo

The entire source code for the Help Desk demo can be found here https://github.com/OfficeDev/PnP/tree/dev/Solutions/BusinessApps.HelpDesk/, in the Office 365 Dev PnP GitHub repository.

Leave a Reply

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