Posts

How Get, Set, Delete Permission on SharePoint Online Site using Graph API

Image
How to get SharePoint Site ID [GET] https://graph.microsoft.com/v1.0/sites?search=contoso5 How to set SharePoint Site Permission only to selected App (AAD App reg) [POST] https://graph.microsoft.com/v1.0/sites/demotenant.sharepoint.com,4465d2n7-e7c4-40df-abfa-364106bbc502,57836gs4-b14f-4061-ba63-fdb0c2a923b6/permissions Request Body: ============ {     "roles": [         "write"     ],     "grantedToIdentities": [         {             "application": {                 "id": "3795d2n7-fa6d-4203-9076-5c8898799cb8",                 "displayName": "DemoTenantAADApp"             }         }     ] } How to get SharePoint Site Permission [GET] https://graph.microsoft.com/v1.0/sites/demotenant.sharepoint.com,4465d2n7-e7c4-40df-abfa-364106bbc502,57836gs4-b14f-4061-ba63-fdb0c2a923b6/permissions [Delete] How to Delete Selected SharePoint Site Permission https://graph.microsoft.com/v1.0/sites/demotenant.sharepoint.

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 SharePoint online default authentication method? And which credential flow it is using to authenticate?

Image
  Here I have explained SharePoint online default authentication method and which credential flow it is using to authenticate: The default authentication method for SharePoint Online is OAuth 2.0 and it uses the authorization code grant flow to authenticate users . This means that the user first logs in through an identity provider (such as Microsoft Account or Azure Active Directory) and then receives an authorization code which is then exchanged for an access token. This access token is then used to access the SharePoint Online resources on behalf of the user. Happy PC (Programming / Configuring)

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

What is the definition of token?

Image
In MSAL (Microsoft Authentication Library) and general OAuth 2.0/OpenID Connect, a token is a string that represents the authorization granted to a client to access a protected resource. Tokens are issued by an authorization server and are sent to the client in the form of a string. There are two main types of tokens: Access Token: is used to authenticate and authorize the client to access protected resources on the resource server. It contains information about the identity of the client and the authorization granted. Refresh Token: is used to obtain a new access token when the current one expires. It is typically a long-lived token that is stored securely on the client. MSAL uses these tokens to authenticate and authorize the client to access protected resources on the resource server. The client can use the access token to authenticate and authorize requests to the resource server. The client can also use the refresh token to obtain a new access token when the current one expires. W

In C# How to Find Duration of Web API Calling Another API and Complete Response Time

Image
In this article you will see how to find time duration of web api calling another web api and its complete response time. In C#, you can use the Stopwatch class to measure the duration of a web API call. Here is an example of how you can use it to measure the time it takes to call another API and receive a response: using System; using System.Diagnostics; using System.Net.Http; class Program {     static void Main(string[] args) {         var client = new HttpClient();         var stopwatch = new Stopwatch();         stopwatch.Start();         var response = client.GetAsync("https://example.com/api").Result;         response.EnsureSuccessStatusCode();         stopwatch.Stop();         var duration = stopwatch.Elapsed;         Console.WriteLine("API call and response time: {0}", duration);     } } This code uses the GetAsync method of the HttpClient class to asynchronously call the API, and the Result property to wait for the response. The EnsureSuccessStatusCode met

Article about Cloud Scale Distributed Design and Patterns

Image
  Cloud-scale distributed systems are designed to support very large numbers of users and handle high levels of traffic by distributing workloads across many machines. These systems typically have the following characteristics: Elasticity: The ability to scale up and down quickly in response to changing demand. Resilience: The ability to continue operating even in the face of hardware or software failures. High availability: The ability to provide uninterrupted service to users. Load balancing: The distribution of workloads across multiple machines to ensure that no single machine is overwhelmed. Auto-scaling: The ability to automatically add or remove resources based on workload. Some common patterns used in cloud-scale distributed systems include: Microservices: Breaking a large application into smaller, independent components that can be developed, deployed, and scaled independently. Containerization: Packaging a

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("Authorization", "Bearer " + GetAccessToken());             endpointReq.GetRequestStream().Write(bytefile, 0, bytefile.Length)

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");             var fileCreationInfo = new FileCreationInformation();             fileCreationInfo.Content = System.IO.File.ReadAllBytes(pathToFile);             fileCreationInfo.Overwrite = true;            

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 Recommended Ap

SharePoint Online List Item "Copy field to clipboard" option

Image
There is a new option available in SharePoint online "Copy field to clipboard". It is available on list items. On clicking copy field to clipboard from the menu, it copies the content of the field (or near left field of the cursor) will be copied to the clipboard and you can paste it anywhere on text docs. Happy PC (Programming / Configuring)

What is the difference between ADAL and MSAL ?

Image
MSAL (Microsoft Authentication Library) works with the Azure AD V2 endpoint. MSAL will connect client application users to the on-prem AD or cloud environment, will protect API by requesting token for access. ADAL (Active Directory Authentication Library) works with the Azure AD V1 endpoint. The V1 endpoint supports work accounts, but not personal accounts. The V2.0 endpoint is the combination of Microsoft personal account and work account into single authentication system. With MSAL we can also get authentications for Azure AD B2C. Happy PC (Programming / Configuring)

What is Cloud Computing?

Image
Cloud computing is the delivery of computing services such as servers, database, storage, software, infrastructure, networking, platform through Internet, instead of on your own physical setup at office or home.  Could computing is faster, dynamic resources, ability to quickly increase or decrease the size or power, auto scaling. Simply it is on-demand delivery of IT resources through the internet. Microsoft Cloud Computing refers to a range of services provided by Microsoft that allows users to store, manage, and process data using Microsoft's network of servers. These services are delivered over the internet and are typically accessed via a web browser or a mobile app. Some of the most popular Microsoft Cloud Computing services include: Azure : A cloud computing platform that provides a wide range of services including virtual machines, storage, databases, and networking. Office 365 : A suite of productivity and collaboration tools that includes email, calendar, and online versio

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)