📅  最后修改于: 2023-12-03 14:47:26.091000             🧑  作者: Mango
SharePoint API is a set of web services and APIs that allow developers to access and manipulate SharePoint data from external applications. SharePoint is a popular content management system developed by Microsoft, use by many organizations for document management, collaboration, and workflow automation.
SharePoint API is grouped into the following categories:
Example:
To get items from a SharePoint list using REST API, use the following URL:
https://<sitecollection>/<site>/_api/web/lists/getbytitle('ListTitle')/items
Example:
To get items from a SharePoint list using CSOM API, use the following code:
ClientContext context = new ClientContext("http://<sitecollection>/<site>");
List list = context.Web.Lists.GetByTitle("ListTitle");
CamlQuery query = CamlQuery.CreateAllItemsQuery();
ListItemCollection items = list.GetItems(query);
context.Load(items);
context.ExecuteQuery();
Example:
To get the URL of a SharePoint site using Server-Side API, use the following code:
using Microsoft.SharePoint.Administration;
Uri siteUrl = new Uri("http://<sitecollection>/<site>");
SPSite site = new SPSite(siteUrl);
string webUrl = site.RootWeb.Url;
SharePoint API supports various authentication methods such as Windows authentication, Forms-based authentication, and OAuth. OAuth is the recommended method for accessing SharePoint data from external applications.
Example:
To authenticate using OAuth and get an access token, use the following code:
var authority = "https://login.microsoftonline.com/<tenant>";
var resource = "https://<sitecollection>.sharepoint.com";
var clientId = "<clientid>";
var clientSecret = "<clientsecret>";
string accessToken = null;
var authContext = new AuthenticationContext(authority);
var clientCred = new ClientCredential(clientId, clientSecret);
var result = authContext.AcquireTokenAsync(resource, clientCred);
if (result != null)
{
accessToken = result.Result.AccessToken;
}
SharePoint API provides a powerful and flexible way to interact with SharePoint data from external applications. It offers various authentication methods and APIs for different use cases. Developers can choose the appropriate API and authentication method based on their requirements.