Introduction
SharePoint has an object model known as the Client-side object model (CSOM), which is available for the .net framework. It wasn't available for the .NET standard, but now Microsoft has provided a much-awaited CSOM for the .NET standard.
With the release of this, we can easily connect to SharePoint using an Azure AD OAuth-based approach from .net core applications.
So to understand how CSOM for .NET standard works, let's create a .NET Core console application connect to SharePoint, and fetch all the items from the list.
Create Azure AD application
Step 1. Navigate to here.
Step 2. Click on Azure Active Directory and click on App registration.
Step 3. Click on + New registration to register the Azure AD application
Step 4. Provide an appropriate name for the application -- we will use NETStandardCSOM.
![Register an application]()
Step 5. Navigate to API Permission and select SharePoint to provide appropriate permission. Select delegated permission and check all the required permissions. For our demo, we will select AllSites.FullControl.
![Request API permission]()
Step 6. Navigate to the authentication section and Under Default Client type select "Yes".
![Authentication]()
Step 7. Copy the client ID which got generated after creating the Azure AD application
Create a .Net Core console application
Step 1. Navigate to Visual Studio and create a new project with the template as .net core console application and name the project NetStandardCSOM.
Step 2. Add all the below NuGet packages.
- Microsoft.SharePoint online.CSOM - This library is CSOM for .NET Standard.
- Newtonsoft.Json
- System.Text.Json
- System.IdentityModel.Token.Jwt
Step 3. Add a class file named AuthenticationManager and add the below code.
This is the actual file where all the magic happens to fetch the token stored in the cache, so we are using OAuth 2.0 Resource Owner Password Credentials to fetch the token.
Please replace the client ID that we created in Step 1 in the string name defaultAADAppId.
Step 4. Now let us use this context and fetch the title of the site collection.
Replace the below code in the Program.cs file.
Update the URI with the site collection URL for which we require a title.
Update the Username and Password of the User
Outcome
![SharePoint CSOM For .NET Standard]()
Conclusion
Now with the release of CSOM for .NET Standard, we can use this in Azure Function v2 which is based on .NET core, and connect to SharePoint. We can even use it in all other applications which are based on .NET Standard so we can connect to SharePoint from any platform.