2.3 Data Integration
At the heart of every application is
data, which is typically what users want to work with within your
application. SharePoint provides a number of out-of-the-box options for
storing and working with data. These options fall into two categories:
- Storing and manipulating data within SharePoint
- Working with data that lives external to SharePoint
From the very first version of SharePoint, the
goal has been to make working with data simple and straightforward for
users. The simplest example of this is the concept of list data. Users
are able to store and work with tabular style data via a common Web
interface. Many see using lists analogous to using a table of data in a
database. SharePoint applications can also take advantage of these same
data storage capabilities natively. By using lists SharePoint offers
developers the ability to take advantage of many of the data storage
capabilities that SharePoint provides without having to reinvent the
wheel. If used properly, SharePoint can save time and effort and
potentially reduce the management and support costs of your operation.
At the core of the data storage capabilities within SharePoint are the following:
- Lists: For storing structured data, much like in a table
- Libraries: For storing unstructured data, such as in a document or file
SharePoint provides a comprehensive set of APIs
for developers to use within applications to interact with and
manipulate data that resides in SharePoint. For SharePoint applications
those APIs are exposed in the Client-Side Object Model (CSOM).
To get a better feel for the data storage
capabilities SharePoint provides, try out storing data in lists within
SharePoint in the following exercise.
TRY IT OUT: Storing Data in SharePoint Using the Client-Side Object Model (DataIntegration.zip)
In this exercise you create new items
in a SharePoint list using the CSOM API set. You must have completed
the “Building Your First SharePoint Application” in the “App Parts and
Pages” section before starting this example.
1. Ensure you have the MyFirstSharePointApp solution open in Visual Studio 2012.
2. If you
haven’t already done so in a previous exercise create a new custom list
in your site called My Custom List (choose Site Contents ⇒ Add an App ⇒
Custom List. Call it My Custom List and then click Create.)
3. Open the Default.aspx file under the MyFirstSharePointAppWeb project in Visual Studio.
4. Open the
Default.aspx.cs file and replace the
Button1_Click method with the following:
protected void Button1_Click(object sender, EventArgs e)
{
var contextToken = ((Button)sender).CommandArgument;
var hostWeb = Page.Request["SPHostUrl"];
using (var clientContext =
TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken,
Request.Url.Authority))
{
Microsoft.SharePoint.Client.List list =
clientContext.Web.Lists.GetByTitle("My Custom List");
for (int i = 0; i < 10; i++)
{
ListItemCreationInformation itemCreateInfo =
new ListItemCreationInformation();
Microsoft.SharePoint.Client.ListItem newListItem =
list.AddItem(itemCreateInfo);
newListItem["Title"] = "New Item " + i.ToString();
newListItem.Update();
}
clientContext.ExecuteQuery();
}
}
5. Press F5 to package and deploy your application.
6. After the
app deploys, add your app’s App Part to the homepage of your site by
selecting Page ⇒ Edit ⇒ Insert ⇒ App Part. Select MyAppPart from the list, and then click Add.
7. Inside your App Part you should now see a button called Do Something. Click it. A
ServerUnauthorizedAccessException error appears, as shown in Figure 15.
This is because your application currently only has Read access to your
SharePoint site. You need to modify your app’s permissions.
8. Click Stop Debugging in Visual Studio.
9. Double-click the AppManifest.xml file under MyFirstSharePointApp.
10. Under Permission Settings, add the following options:
- Scope: List
- Permission: Write
11. Press F5 to package and deploy your application.
12. Notice
that SharePoint now asks you which list you want to allow it access to
edit or delete items in. Select My Custom List from the drop-down menu.
Click Trust It to continue.
13. You might need to re-add your App Part due to the change in app security settings.
14. Click Do Something in your App Part.
15. After the
operation completes, open My Custom List in the SharePoint site. You
should see that ten items have been added to the list, as shown in Figure 16.
How It Works
In this exercise you added list items
to a SharePoint list. You configured your application to ask for
permission to have edit rights on the list. SharePoint apps can ask for
access to various security scopes of varying levels. Your application
should only demand the permissions it needs to operate correctly. An
app asking for too many permissions might make a user unwilling to
grant them and therefore unable to use the app. Additionally, users
cannot grant applications permission to resources that they themselves
do not have appropriate access to.
NOTE You can read more about all the available permission scopes and rights on MSDN (http://msdn.microsoft.com/en-us/library/fp142383(v=office.15).aspx).
Additionally, SharePoint offers
developers a range of mechanisms and APIs for integrating with data
that lives outside of SharePoint. Although the data exists external to
SharePoint, these APIs and features offer the ability to integrate with
data much the same way you would with data stored inside SharePoint
itself.