Posts

Showing posts with the label SharePoint Online

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-b1...

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)

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 ...

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)