The SharePoint rest api is used in Windows Store applications.
In the previous article, we introduced the use of official tools for Office 365 REST APIs. In this article, we will take a look at the description, structure and usage of SharePoint REST APIs, as well as some usage experience.
First, let's take a look at the overview of SharePoint rest api:
The rest api service was introduced in SharePoint 2013. It is officially believed that the rest api service can rival the existing SharePoint client object model. Developers can use any technology that supports REST Web requests (C #, javascript, java, oc, etc.) to remotely interact with SharePoint data. This 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 applications, you must first obtain access permissions to use SharePoint data resources.
Before the emergence of REST APIs, we need to use the client. svc WCF Service to request list data of SharePoint, and only to obtain data. Data modification is not reflected in this service. With the rest api, we can complete the CRUD operation mentioned above: using the OData standard to construct HTTP requests that can implement REST, corresponding to the corresponding request method, you can read or operate the resource. For example, you can use the GET method to retrieve data, use the POST method to create data, use the PUT or MERGE method to update data, and use the DELETE method to DELETE data.
By default, the rest api will return data in Atom format, but we can also set the data in JSON format as needed. I personally prefer to process JSON format, and convert JSON data into C # objects. There is a great Website: http://json2csharp.com /. Through this website, we can directly convert JSON data into C # objects, saving a lot of time for writing C # basic code.
Next, let's take a look at the structure of the rest api:
The basic URL of the endpoint is https: // server/site/_ api, which is the basis of all SharePoint REST APIs, where server represents the name of the server, site indicates the name or path of a specific website.
If you want to access a specific website set, the URL is: https: // server/site/_ api/site. If you want to access a specific website, the URL is: https: // server/site/_ api/web. These two are the most common APIs, and several other APIs can implement some specific functions, such as using the search service: https: // server/site/_ API/search, access the user configuration file: http: // server/site/_ api/SP. userProfiles. peopleManager.
The following uses a specific website as an example to illustrate the common operations that REST APIs can perform:
(1) operations on list 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 set:
URL: http: // <website URL>/web/lists (guid '<list ID>')/items or http: // <website URL>/web/lists/getbytitle ('title of list')/items
List items of the specified ID:
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 preceding API address, you can use the GUID or Title of the List to obtain the data of the List or List item set. To insert a new item to the list, we need to use the list item set API and use the POST method to insert data. When you need to modify an item in the list, you need to use the specified list item API and use the MERGE method to update data.
When it comes to data modification, we need to add the method type, such as POST and MERGE, as the value of the X-HTTP_Method key to the request header. The Digest value of the request form must be passed as the value of X-RequestDigest. The value can be obtained through http: // website URL/_ api/contextinfo.
Send a POST request with an empty body and extract the value of the d: FormDigestValue node from the XML returned by the contextinfo endpoint. In addition, the IF-MATCH key value in the header must be assigned the etag value of the list or list item. If the value is "*", the concurrency is not considered.
In addition, data operations require POST:
When updating an object, it must contain _ metadata: type and fields to be updated. Other fields that do not need to be updated can be excluded. When a record is deleted, the URL points to the record. When adding a record, the URL points to the record set. The POST content should contain _ metadata: type and mandatory fields to be inserted.
(2) operations on 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> ')
With the above API operations, we can read, upload, and modify files and folders.
When you need to insert a document to the document library, the URL is: http: // <site url>/_ api/web/GetFolderByServerRelativeUrl ('/Shared documents')/Files/add(url+'a.txt ', overwrite = true). In this way, we insert the file named a.txt into the Shared Documents library. The document content upload process is: the file is read as Stream and put into the POST content. There are many other methods to operate files, such as update, check-out, check-in, and delete.
(3) operations on users, groups, and roles
GROUP:
URL: http: // <website URL>/_ api/web/sitegroups (<group ID>)
User:
URL: http: // <website URL>/_ api/web/siteusers (@ v )? @ V = '<Logon Name>'
Role:
URL: http: // <website URL>/_ api/web/roledefinitions (<role definition ID>)
(4) operations on user configuration files
URL: http: // <website url>/_ api/sp. userprofiles. lelemanager
(5) operations on fields
URL: http: // <website url>/_ api/web/fields ('<field id>') or http: // <website url>/_ api/web/lists (guid '<list id>')/fields ('<field id> ')
We have introduced the list, folders, and other types of API address construction. Next we will look at the query parameters allowed in these request addresses:
(1) $ select Parameter
For example, in SQL, $ Select is used to determine the fields to be included in the returned results of the request. The usage is: _ api/web/lists/getByTitle ('books ') /items? $ Select = Author, Title, ISBN. In this way, only the Author, Title, and ISBN fields are valid when the Books list is obtained.
(2) $ filter parameter
For example, in SQL Where, $ filter is used to filter data. The usage is: _ api/web/lists/getByTitle ('books ')/items? $ Filter = Author eq 'Mark Twain ', so that we can get the data in the Books list where Author is Mark Twain.
(3) $ expand Parameter
This parameter is used to specify which projection fields in the returned join list. How to Use: _ api/web/lists/getByTitle ('books ')/items? $ Select = Title, PublishedBy/Name & $ expand = PublishedBy. The projection field here is PublishedBy.
(4) $ top Parameter
This parameter specifies the first N items in the returned result set. The usage is: _ api/web/lists/getByTitle ('books ')/items? $ Top = 2, then we will return the first two items in the result set (if there are less than two items, all data will be returned ).
(5) $ skip Parameters
This parameter is used to skip a specified number of items in the result set and return the remaining items. How to Use: _ api/web/lists/getByTitle ('books ')/items? $ Skip = 2. In this way, the first two items are skipped in the returned results and the remaining items are returned. If less than two results are returned, an empty result set is returned.
(6) $ orderby Parameter
This parameter is used to sort the result set. For example, the method used in SQL is divided into two methods: ascending order and descending order, which are identified by the asc and desc keywords. How to Use: _ api/web/lists/getByTitle ('books ')/items? $ Orderby = ID desc. In this way, the returned result set is sorted in descending order based on the ID.
The following table covers almost all query conditions, comparison Keywords of numbers, characters, and so on in the SharePoint rest api. For example, $ filter = Author eq 'Mark Twain 'is used to filter items whose Author is equal to Mark Twain, while Author ne 'Mark Twain' is used to filter different items. This is a string comparison operation, while numbers include lt (less than), le (less than or equal to), gt (greater than), ge (greater than or equal to), and eq (equal) and ne (not equal ). There are also comparison operators for date and time, as described in the table.
Finally, an authorization problem is also involved. As mentioned above, remote access requires authorization before accessing resources. We can complete application authorization and Identity Authentication through OAuth authorization. Add the obtained OAuth access token to the request header. We will not go into detail here. There is no difference between the process of using APIs and other APIs. I will not detail them here. Is a process of sending a request to obtain the returned result (Atom or JSON) and then parsing the result.
In this way, we have finished introducing the composition and basic usage of SharePoint REST APIs. I hope it will be helpful for you to use SharePoint REST APIs in Windows Store applications. Thank you.