Posts

Showing posts with the label C#

In C# CSOM How to Delete Folders Recursively, Sub-Folders, Files in SharePoint Online Document Library

Image
In this article we will see how to delete Root folder, sub-folders, files in SharePoint Document Library.  Here I have given snippet by hardcoding (not recommended) one folder name "TestFolder1".  However you can use loop statement and iterate list of root folder names.  var clientContext = GetClientContextWithAccessToken(siteUrl, accessToken); Web web = clientContext.Web; clientContext.Load(web); clientContext.ExecuteQuery(); //Here you can add for loop with list of root folder names and iterate //Start of for loop string folderPath = "/sites/contoso5/LibraryName/ TestFolder1 "; //Pass the folder name dynamically Folder folder = clientContext.Web.GetFolderByServerRelativeUrl(folderPath); clientContext.Load(folder, folderitem => folderitem.Properties, folderitem => folderitem.Name, folderitem => folderitem.Files); clientContext.ExecuteQuery(); DeleteFolderRecursive(folder); //Call method to delete subfolders and files. folder.DeleteObject(); clientContext.E...

What is SOLID Principles?

Image
SOLID is an acronym for five principles of object-oriented programming and design. The SOLID principles are: Single Responsibility Principle (SRP) - a class should have only one reason to change Open-Closed Principle (OCP) - a class should be open for extension but closed for modification Liskov Substitution Principle (LSP) - objects of a superclass should be able to be replaced with objects of a subclass without affecting the correctness of the program Interface Segregation Principle (ISP) - a class should not be forced to implement interfaces it does not use. Dependency Inversion Principle (DIP) - high-level modules should not depend on low-level modules, but both should depend on abstractions. These principles were identified by Robert C. Martin in the early 2000s and are considered to be best practices for designing maintainable and extensible software systems.

What is the difference between Principles and Patterns?

Image
The difference between principles and patterns are as follows. Principles and patterns are both important concepts in software development, but they are different in nature. Principles are general guidelines or rules that provide a broad understanding of how to design and build software systems. They are often abstract and can be applied to a wide range of situations. Examples of principles include SOLID principles, DRY (Don't Repeat Yourself) principle and KISS (Keep It Simple, Stupid) principle. Patterns, on the other hand, are specific solutions to common problems that occur in software development. They provide a detailed description of how to solve a particular problem, and they are usually written in the context of a specific programming language or platform. Examples of patterns include the Singleton pattern, the Factory pattern, and the Observer pattern. In summary, principles give you a general idea of how to design systems, while patterns give you specific solutions to co...

C# How to Get SharePoint Online Documents Analytics Using Graph API

Image
In this article we will see How to get SharePoint Online documents analytics using the Microsoft Graph API. To get, you will need to: Register an app in Azure AD to authenticate and authorize API requests. Grant the app the necessary permissions to read the documents and their analytics data. Use the API to get the analytics data for the documents. Below is the example of how you can do this in C#: using Microsoft.Identity.Client; using Microsoft.Graph; using System.Net.Http.Headers; // Replace these with your own values. const string clientId = "your-client-id"; const string clientSecret = "your-client-secret"; const string tenantId = "your-tenant-id"; const string resourceId = "https://graph.microsoft.com"; const string siteId = "your-site-id"; // Acquire an access token. var authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenantId); var clientCredential = new ClientCredential(clientId, clientS...

C# How to Access SharePoint Online Lists using Graph API using MSAL

Image
In this article, we will see how to invoke SharePoint Online Lists using Graph API using MSAL authentication in C#. To access a SharePoint Online list using the Microsoft Graph API with MSAL (Microsoft Authentication Library) authentication in C#, you will need to follow these steps: Register your application with Azure Active Directory (AAD) and grant it the necessary permissions to access SharePoint Online. This will enable your application to authenticate with AAD and obtain access tokens. Install the Microsoft.Identity.Client NuGet package in your C# application. This will allow you to authenticate users and obtain access tokens using the MSAL library. Use the MSAL library to authenticate the user and obtain an access token. This token will be used to authorize your application to access SharePoint Online. Use the access token to make a request to the Microsoft Graph API to retrieve the data from the SharePoint Online list. Here is an example of how you can use the MSAL library to ...

C# Http Method How to Upload Document into SharePoint Online Document Library

Image
In this article we will see how to upload file into SharePoint Online Document Library using REST API and HTTP method. Also you will drr how to get the document ID of the same upload document. Here is the code:             string filePath = @"C:\MyFile.pdf";             string siteUrl = "https://domainname.sharepoint.com/sites/contoso5/"             byte[] bytefile = System.IO.File.ReadAllBytes(filePath);             HttpWebRequest endpointReq = (HttpWebRequest)HttpWebRequest.Create(siteUrl + "/_api/web/GetFolderByServerRelativeUrl('DocLib/MyFolder')/Files/add(url='Myfile.pdf',overwrite=true)?$expand=ListItemAllFields");             endpointReq.Method = "POST";             endpointReq.Headers.Add("binaryStringRequestBody", "true");             endpointReq.Headers.Add("Authorizatio...

C# CSOM How to Upload Document into SharePoint Online Document Library

Image
In this article we will see how to upload document into SharePoint Online Document Library using SharePoint Client Object Model CSOM. Also I have given piece of code to get document ID of the  uploaded document. Here is the code:             string siteUrl = "https://domainname.sharepoint.com/sites/contoso5/";             string docUrl = "https://domainname.sharepoint.com/sites/contoso5/DocLib/MyFolder/NewPDF.pdf";             string pathToFile = @"C:\NewPDF.pdf";             var clientContext = GetClientContext(siteUrl);             Web web = clientContext.Web;             clientContext.Load(web, a => a.ServerRelativeUrl);             clientContext.ExecuteQuery();             List documentsList = clientContext.Web.Lists.GetByTitle("DocLib"); ...

SharePoint Online Access List Item by ID using Graph API

Image
In this page, we will see How to access SharePoint Online List Item by ID using Graph API. Below screenshot is from Graph Explorer ( Graph Explorer | Try Microsoft Graph APIs - Microsoft Graph ).  I have formatted the Graph API url and executed from Graph Explorer and below is the output. Graph API url: https://graph.microsoft.com/v1.0/sites/<site-id>/lists/<list-name>/Items?expand=fields(select=Id,Column1,Column2,Column3)&filter=fields/ID eq <item-id> we have to use header key value (highlighted in green arrow on above screenshot) as because filter works only on indexed columns Header: Prefer   Value:  HonorNonIndexedQueriesWarningMayFailRandomly (Replace the site-id, list-name, column name, item-id with your actual values.) On executing above API with actual values, response will be returned with respective list item However, instead of filter by ID, we can directly access the particular list item by passing ID like below And this is the Microsoft ...

C# How to Call Graph API from Web API using MSAL Authentication

Image
In this article you will see, in C# how to call Graph API from Web API using MSAL Auth using .NET Framework. MSAL auth with Graph API works good on Console app, Azure Func App, etc. But not for Web API, need to handle with Azure Identity.  To make it work on Web API, instead of using Microsoft.Client.Identity we will need to Azure.Identity. Internally Azure.Identity uses the MSAL authentication. So you can use like below. var tenantId = "xxxxx-xxxx-4392-xxxx-xxxxxxxxxx"; var clientId = "xxxxx-xxxx-4203-xxxx-xxxxxxxx"; var clientSecret = "xxxxxzzxxzzzxxzzzzzxxxxxxxx"; var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret); GraphServiceClient graphServiceClient = new GraphServiceClient(clientSecretCredential); var users = graphServiceClient.Users.Request()                   .Select(x => x.DisplayName).GetAsync().Result; Output: You will get result like below. Happy PC (Programming / Configuring)