Using the SharePoint REST API in Windows store apps

Source: Internet
Author: User
Tags oauth

In the previous article, we introduced the use of the official tools for the Office 365 Rest API, and this article looks at the description, structure, and usage of the SharePoint Rest API itself, as well as some usage experience.

Let's take a look at an overview of the SharePoint REST API:

The Rest API service was introduced in SharePoint 2013, and officials believe that the rest API service can rival the existing SharePoint client object model. Developers can use any of the technologies that support RESTful WEB requests (C#,JAVASCRIPT,JAVA,OC, and so on) to interact remotely with SharePoint data. This also means that developers can use REST Web technology and standard Open Data Protocol (ODATA) syntax to perform CRUD operations from their SharePoint-related applications, solutions, and client applications. For remote Web or mobile apps, you must first gain access before you can use SharePoint data resources.

Before the REST API came into being, we needed to request SharePoint's list data by client.svc this WCF service, and was limited to getting the data. Changes to the data are not reflected in this service. With the rest API, we can do the CRUD operations mentioned earlier: using the OData standard to construct an HTTP request that implements REST, and corresponding to the corresponding request method, you can read or manipulate the resource. For example, use the Get method to get the data, use the POST method to create the data, use the PUT or MERGE method to update the data, and delete the data using the Delete method.

The REST API returns data in Atom format by default, but we can also return data in JSON format as required. I personally prefer to work with JSON format, JSON data into C # objects, and have a great website: http://json2csharp.com/. Through this site, we can convert JSON data directly into C # objects, saving a lot of time to write C # base code.

Next, let's look at the structure of the REST API:

The base URL for the endpoint is: Https://server/site/_api, which is the basis for all SharePoint REST APIs, where server represents the name of the server and site represents the name or path of a particular site.

If you want to access a specific site collection, the URL is: https://server/site/_api/site, if you want to access a specific Web site, the URL is: Https://server/site/_api/web. These are the most commonly used APIs, and there are several APIs that can implement specific functions, such as: using the Search service: Https://server/site/_api/search, accessing User profiles:/http/SERVER/SITE/_API/SP . Userprofiles.peoplemanager and so on.

Let's take a specific website as an example to see what the REST API can do:

(1) Actions on lists and list items

List:

url:http://< website url>/web/lists (GUID '< list id> ') or HTTP///< website url>/web/lists/ Getbytitle (' title of List ')

List item Collection:

url:http://< website url>/web/lists (GUID '< list id> ')/items or HTTP//< website url>/web/lists/ Getbytitle (' title of List ')/items

Specifies the ID of the list item:

url:http://< website url>/web/lists (GUID '< list id> ')/items (item ID) or HTTP//< website url>/web/ Lists/getbytitle (' title of List ')/items (item id)

As shown in the API address above, we can get the list, or the list's item collection data, through the GUID or Title of the list itself. When you need to insert a new item into the list, we need to use the list item collection API to insert the data using the POST method. When you need to modify an item in the list, you need to use the MERGE method to update the data using the specified list item API.

When it comes to data modification, we need to add the method type, such as Post,merge, as the value of the X-http_method key to the request header. And you need to pass the Request form digest value as the value of x-requestdigest. This value is obtained by Url/_api/contextinfo to http://website
Sends a POST request with an empty body and extracts the value of the D:formdigestvalue node in the XML returned by the ContextInfo endpoint. There is also the value of the If-match key in the header, which needs to be assigned the ETag value of the list or list item. If you assign a value of "*", concurrency issues are not considered.

In addition about the data operation is required POST content:

When updating, you must include __metadata:type and the fields that need to be updated. Other fields that do not need to be updated can be joined. When deleted, the URL points to the record. When added, theURL points to the record collection,and the POST content should contain __metadata:type and required fields that need to be inserted

(2) Operation of files and folders

File:

url:http://< website url>/_api/web/getfilebyserverrelativeurl ('/< folder name >/< file name > ')

File list:

url:http://< website url>/_api/web/getfolderbyserverrelativeurl ('/< folder name > ')/files

Folder:

url:http://< website url>/_api/web/getfolderbyserverrelativeurl ('/< folder name > ')

Through the above API operation, we can complete the file and folder read, upload and modify operations.

When you need to insert a document into a document library, the URL is: Http://<site url>/_api/web/getfolderbyserverrelativeurl ('/shared documents ')/files/add ( Url= ' A.txt ', overwrite=true), so that we insert the file in the Shared documents library with the A.txt as the name of the store. The upload process for the document content is: The file is read as Stream and placed in the POST content. There are many other ways of working with files, such as updating, checking out, checking in, deleting, etc.

(3) Actions for users, groups, and roles

Group:

url:http://< website url>/_api/web/sitegroups (< group id>)

User:

url:http://< website url>/_api/web/siteusers (@v) [email protected]= '< login name > '

Role:

url:http://< website url>/_api/web/roledefinitions (< role definition id>)

(4) Operation of the user profile

url:http://< website Url>/_api/sp.userprofiles.peoplemanager

(5) Operation of the field

url:http://< website url>/_api/web/fields ('< field id> ') or HTTP///< website url>/_api/web/ Lists (GUID '< list id> ')/fields ('< field id> ')

We've covered the address constructs for the list, folder, and other types of APIs, and then we'll look at what the query parameters are allowed in these request addresses:

(1) $select parameters

As with the use of Select in SQL, the $select here is to determine which fields are included in the results returned by the request, using the following method: _api/web/lists/getbytitle (' Books ')/items? $select =author, TITLE,ISBN, so that we will get the books this list, only the author, title and ISBN three valid fields.

(2) $filter parameters

such as where in SQL, where the $filter is used to filter the data filter criteria, using: _api/web/lists/getbytitle (' Books ')/items? $filter =author eq ' Mark Twain ', so that we can take the Books list, Author the data for Mark Twain.

(3) $expand parameters

This parameter is used to specify which cast fields in the join list are returned. How to use: _api/web/lists/getbytitle (' Books ')/items? $select =title,publishedby/name& $expand =publishedby, where the projection field is Publishedby.

(4) $top parameters

This parameter specifies that the first n items in the result set are returned, using the following method: _api/web/lists/getbytitle (' Books ')/items? $top = 2 so that we return the first two items in the result set (if less than two return all data).

(5) $skip parameters

This parameter is used to skip a specified number of items in the result set and return the remaining items. To use: _api/web/lists/getbytitle (' Books ')/items? $skip = 2 so that we can skip the first two items in the returned result and return the remaining items, and return an empty result set if the result is less than two.

(6) $orderby parameters

This parameter is used to sort the result set, such as the method used in SQL, in ascending and descending ways, identified by the ASC and DESC keywords. How to use: _api/web/lists/getbytitle (' Books ')/items? $orderby =id Desc. In this way, the result set we return will be sorted in descending order with ID as standard.

The following table covers almost all the query criteria and comparison keywords for numbers, characters, and so on in the SharePoint REST API. For example $filter =author eq ' Mark Twain ', which is filtering Author equals Mark Twain, while Author NE ' Mark Twain ', is a filter of unequal items. This is a comparison operation for strings, while for numbers includes LT (less than), le (less than equals), GT (greater than), GE (greater than equals), eq (equals), and NE (not equal). There are also comparators for date and time, as explained in the table.

Finally, there is an issue of authorization, as previously mentioned, where remote access requires authorization before access to resources. We can do app authorization and authentication through OAuth authorization. Adds the acquired OAuth access token to the request header. Here we don't do a detailed introduction. There is no difference between the use of the API itself and other APIs, and this is not a detailed introduction. is the process of making a request, getting a return result (atom or JSON), and then parsing the result.

So we're done with the composition and basic usage of the SharePoint Rest API, and we want to help you with the SharePoint Rest API in Windows store apps.

Using the SharePoint REST API in Windows store apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.