Introduction
SharePoint 2013 introduces a Representational State Transfer (REST) service that is comparable to the existing SharePoint client object models. This allows the developers to interact remotely with SharePoint data by using any technology that supports REST web requests. This means that developers can perform Create, Read, Update and Delete (CRUD) operations from their apps for SharePoint, solutions and client applications, using REST web technologies and standard Open Data Protocol (OData) syntax. I have a custom list in the host site named “Custom List”. I have an item (Item ID: 1) that has the following attachments.
In this article you will see the following:
- Create an app using NAPA Tool in SharePoint 2013 Online.
- Cross-Domain Requests.
- Get all the attachments for the list item using the REST API.
Endpoint URI
https://c986.sharepoint.com/_api/web/lists/getbytitle('Custom%20List')/items(1)/attachmentfiles
Note: If you are making cross-domain requests then you need to add SP.AppContextSite(@target) and ?@target='<host web url>' to the endpoint URI.
HTTP Request
GET: Read a Resource.
JSON
Use the following procedure to create an app using the NAPA Tool:
- Navigate to the SharePoint 2013 Online site.
- Click on Site Contents in the quick launch bar.
- Click on “Napa” Office 365 Development Tools.
- Click on Add New Project.
- Select App for SharePoint, enter the Project name and then click on Create.
Permissions
Ensure appropriate permission is provided to access the content. Click on Properties button, and then click on Permissions. Set the required permission to access the content.
Default.aspx
Replace the contents of Default.aspx with the following:
<%-- The markup and script in the following Content element will be placed in the <head>of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<!-- Add your CSS styles to the following file -->
<link rel="Stylesheet" type="text/css" href="../Content/App.css" />
<!-- Add your JavaScript to the following file -->
<script type="text/javascript" src="../Scripts/App.js"></script>
</asp:Content>
<%-- The markup in the following Content element will be placed in the TitleArea of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">Page Title</asp:Content>
<%-- The markup and script in the following Content element will be placed in the <body>of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server">REST API Examples</asp:Content>
<%-- The markup and script in the following Content element will be placed in the <body>of the page --%>
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
<div>
<p>
<b>Attachments</b>
<br />
<select style="height:300px; width:500px" multiple="multiple" id="selectAttachments"></select>
</p>
</div>
</asp:Content>
App.js
Replace the contents of App.js with the following:
'use strict';
var hostweburl;
var appweburl;
// Load the required SharePoint libraries.
$(document).ready(function () {
//Get the URI decoded URLs.
hostweburl = decodeURIComponent(
getQueryStringParameter("SPHostUrl"));
appweburl = decodeURIComponent(
getQueryStringParameter("SPAppWebUrl"));
// Resources are in URLs in the form:
// web_url/_layouts/15/resource
var scriptbase = hostweburl + "/_layouts/15/";
// Load the js file and continue to load the page with information.
// SP.RequestExecutor.js to make cross-domain requests
$.getScript(scriptbase + "SP.RequestExecutor.js", loadPage);
});
// Utilities
// Retrieve a query string value.
// For production purposes you may want to use a library to handle the query string.
function getQueryStringParameter(paramToRetrieve) {
var params = document.URL.split("?")[1].split("&");
for (var i = 0; i < params.length; i = i + 1) {
var singleParam = params[i].split("=");
if (singleParam[0] == paramToRetrieve) return singleParam[1];
}
}
function loadPage() {
getAttachments();
}
//Retrieve all of the atachements for the list item
function getAttachments() {
var executor;
// Initialize the RequestExecutor with the app web URL.
executor = new SP.RequestExecutor(appweburl);
executor.executeAsync({
url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Custom%20List')/items(1)/attachmentfiles?@target='" + hostweburl + "'",
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: getAttachmentsSuccessHandler,
error: getAttachmentsErrorHandler
});
}
//Populate the selectattachments control after retrieving all the list item attachments.
function getAttachmentsSuccessHandler(data) {
var jsonObject = JSON.parse(data.body);
var selectAttachments = document.getElementById("selectAttachments");
if (selectAttachments.hasChildNodes()) {
while (selectAttachments.childNodes.length >= 1) {
selectAttachments.removeChild(selectAttachments.firstChild);
}
}
var results = jsonObject.d.results;
for (var i = 0; i < results.length; i++) {
var selectOption = document.createElement("option");
selectOption.value = results[i].FileName;
selectOption.innerText = results[i].FileName;
selectAttachments.appendChild(selectOption);
}
}
function getAttachmentsErrorHandler(data, errorCode, errorMessage) {
alert("Could not get the attachments: " + errorMessage);
}
Deploy the App
- Click on Run Project.
- The app will be packaged, deployed and launched.
- Click on “Click here to launch your app in a new window”.
- Click on Trust it.
- All the attachments for the list item are displayed.
Summary
Thus in this article you saw how to get all the attachments for the list item using the REST API in SharePoint 2013 Online.