Welcome back to my continuing series on SharePoint development!
If youāve been following along with lesson one and lesson two then this post has probably been a long time coming, but weāre now at the point where weāve built a pretty useful tool for calculating shipping costs, and weāve integrated it into our existing SharePoint site to make it easily accessible to everyone in our team who might benefit from it.
Hereās the thing, though. If your organization is anything like mine, then SharePoint is a tool that theyāve made available to everybody. Youāre using it for some cool stuff, but writing code probably isnāt your day job ā youāre the techy guy or gal in your group whoās found an opportunity to make everybodyās life a little easier with technology, and the beautiful part is that you can do it all without needing to engage your companyās IT team (who are busy with large-scale projects involving a contribution to your companyās bottom line, which your idea for a shipping calculator would need to be prioritized against).
Thereās nothing wrong with any of that. Something like this really shouldnāt be a thing that your companyās IT team get involved with in the exact same way that helping you craft an especially complex excel formula shouldnāt be a job for them either. If youāve ever crafted an especially complex excel formula in a workbook thatās shared throughout your group though then you may already have identified the downside to this approach: things change.
Our tool is built on a fixed model for calculating shipping costs of $19.99 for the first 20lbs and $3 for every 5lbs (or part thereof) over and above that. That rule is embedded within your code now, youāre the only one around with the necessary technical knowledge to update it when shipping costs change, and you have a day job to worry about too. If updating SharePoint tools is not how you like to spend your weekends, then we need a different approach.
What we need, then, is a solution where the average SharePoint user can make changes to key pieces of data, and our tool needs to be smart enough to read that data so that it can be used in calculations. And, while weāre at it, letās expand the tool so that it can handle a few different shipping profiles (which could represent different couriers or, in our example, destinations).
Enter the SPServices library. SPServices is a jQuery plugin thatās used to expose SharePoint data to our jQuery apps, including (amongst other functions) reading from and writing to SharePoint lists in SharePoint 2007, 2010 and 2013.
This is a significant step up from where we were at the end of lesson two (which is probably why Iāve been procrastinating over writing it for so many weeks). Iāve split it into parts. Today weāre going to set the stage and prepare our data, and this time next week (I promise!) weāre going to get our hands dirty with some code.
Nevertheless, both this post and its successor are probably going to be longer than those that have gone before, so be forewarned, go grab yourself a cup of coffee, and letās dive in!
Creating the List
First things first, we need a list to hold our data. This list is where our less-technical colleagues will come when changes need to be made and weāll keep it fairly straightforward.
Much like we did to create our document library in lesson one, go to Site Actions > View All Site Content and hit the Create button. This time weāre going to choose Custom List as the type of entity weāre going to create. We need to give the list a name, so letās call it āShipping Prices.ā For the time being weāll leave the description blank, and weāll hide our list from the Quick Launch bar. We can always change these options later.
When you hit the Create button the list will be created and will have a single column (āTitleā). We need to add a few more columns, so choose List Settings from the toolbar or the Actions menu (depending on your version of SharePoint). The first thing weāre going to do is rename the āTitleā column to āProvinceā by clicking it in the list, then weāre going to add four more columns by clicking the Create Column link and adding them one by one. Hereās where we want to end up:
An Important Note Regarding Column Names
With this the basic framework for our data is in place. You may notice that none of our column names have spaces in them. Thatās because SharePoint in the backend does strange things with spaces. As youāll see later we can programmatically read from a column called āBasePriceā by referring to it exactly in that way, whereas a column called āBase Priceā would need to be referred to in our code as āBase_x0020_Price.ā
That being said, we can leverage a bit of trick here if want to improve readability for people who will interact with this list directly (our less-technical colleagues, remember). Behind the scenes (and in our code) SharePoint will always know the column by its original name, even if itās subsequently been renamed. If we go back and spaces now, the internal name of the āBasePriceā column will remain āBasePrice,ā even if its display name is changed to āBase Price.ā
This is helpful in this scenario, but can easily be a bit of a gotcha ā you need to remember the original name of all your columns, because thatās how your code will reference them. Remember the āTitleā column we renamed to āProvince?ā Itās still āTitleā behind the scenes.
Populating the Data
There are many ways to get data into a SharePoint list, and Iām not going to go into a great amount of detail here. You can add each item row by row with the built-in list forms, you can use SharePointās datasheet view to edit many rows at once, or you can use a third party tool. Our example is probably a little basic to warrant breaking out a special third-party tool for, but nevertheless as you do more complex stuff in the future Iām a fan of SharePoint List Item Editor. It does exactly what its name suggests, gives you a spreadsheet-like interface for editing items in SharePoint lists, and makes it easy to copy and paste many rows at once.
Regardless of how we do it, hereās the data Iām going to put into my list for the purposes of this example.
With this data in place youāre probably starting to get a sense of where things are going with this example. The BasePrice is the cost of shipping the first BaseWeight pounds, and the AdditionalPrice is the cost of each AdditionalWeight pounds or part thereof.
In many ways itās no different from what weād created by the end of lesson two, but with one critical difference ā none of these variables are going to live in our code anymore. Theyāre all factored out into the list where theyāre easily editable when things need adjusting in the future.
Find The GUID of the List
Everything weāve done so far is fairly standard SharePointy stuff, but youāll notice weāve had one eye on the end goal of programmatically interfacing with this data throughout. Finding the GUID of the list weāve created is an important step in this process.
A GUID is a globally unique identifier, and every SharePoint list (or calendar, or document library, etc) on our SharePoint site collection has one. There are several ways we can connect our front-end custom interface to our back-end data, but the GUID is probably the most reliable because as the name implies, itās globally unique. Itās also not affected if our list gets renamed later.
There are several ways to find it, but my favourite is to use a simple tool I found for the purpose. Open the app and plug in the URL of your SharePoint site. Hit the Display List Titles and IDs button, and grab the relevant ID (including the opening and closing braces).
For me {4883AC18-E2A5-4EAF-8446-23B15B43861A} is what I need. For you it will be different. You may notice that the tool can also find the GUID of a list view. We donāt need this because weāre going to use the default view. Iām not going to get into it right now, but if youāre unfamiliar SharePoint views define things like the sort order and filter thatās applied to a list, and each list can have multiple views defined. If you want to programmatically access data thatās filtered out of your default view then the simplest method is probably to create a new, unfiltered, public view and reference it by its GUID in your code. The documentation for SPServices will tell you more.
ā¦and Talking of SPServices
Now is probably a good time to download the library and place the minified javascript file in the āwebā document library we created in lesson one, in the ājsā subfolder alongside the jQuery library thatās already there. At the time of writing this file is called jquery.SPServices-2014.01.min.js.Ā With that our stage is set and weāre ready to rewrite the code in our content editor web part to interface with it, but that was a lot to take in so weāll get to that next week.