📜  SharePoint-API(1)

📅  最后修改于: 2023-12-03 14:47:26.091000             🧑  作者: Mango

SharePoint API

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 Overview

SharePoint API is grouped into the following categories:

  • SharePoint REST API: The REST API exposes various SharePoint entities such as lists, libraries, and sites as resources represented in JSON format. Developers can use standard HTTP requests to interact with SharePoint data.

Example:

To get items from a SharePoint list using REST API, use the following URL:
 
https://<sitecollection>/<site>/_api/web/lists/getbytitle('ListTitle')/items
  • SharePoint CSOM API: The CSOM (Client-Side Object Model) API is a set of .NET libraries that allow developers to interact with SharePoint from client-side applications such as .NET, Xamarin, and UWP. This API offers more functionality compared to the REST API, such as the ability to manage users and permissions.

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();
  • SharePoint Server-Side API: The Server-Side API is a set of .NET libraries that allows developers to interact with SharePoint from server-side applications such as ASP.NET and SharePoint-hosted apps. This API offers more functionality than the CSOM API, such as the ability to manage web parts and timer jobs.

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 Authentication

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;
}
Conclusion

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.

References