Skip to content

Instantly share code, notes, and snippets.

View asadrefai's full-sized avatar

Asad Refai asadrefai

View GitHub Profile
@asadrefai
asadrefai / GetSecretAsyncInAzureFunction.cs
Last active July 7, 2021 14:50
Gets the secret value from Azure Key Vault and establish SharePoint connection
[FunctionName("Function1")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
const string secretName = "Test";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
@asadrefai
asadrefai / GetSecretAsyncInConsoleApp.cs
Last active July 7, 2021 14:51
Gets the key vault secret value and establish connection with SharePoint in net core console app
static async Task Main(string[] args)
{
const string secretName = "Test";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions();
options.ExcludeEnvironmentCredential = true;
options.ExcludeInteractiveBrowserCredential = true;
options.ExcludeManagedIdentityCredential = true;
const string secretName = "Test";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
// Creating an object and excluding methods which I do not want to be executed. Although these are executed in series, i.e. in my code, due to Enviornment Variables or Visual Studio exception was thrown.
// Ideally I would expect, if one method does not work, without breaking code it should move to another one.
DefaultAzureCredentialOptions options = new DefaultAzureCredentialOptions();
options.ExcludeEnvironmentCredential = true;
options.ExcludeInteractiveBrowserCredential = true;
options.ExcludeManagedIdentityCredential = true;
@asadrefai
asadrefai / SampleGetSecretAsync.cs
Last active July 7, 2021 13:49
Snippet for getting secret value from Azure Key Vault
const string secretName = "TestName";
var keyVaultName = "test-blog-kv";
var kvUri = $"https://{keyVaultName}.vault.azure.net";
var client = new SecretClient(new Uri(kvUri), new DefaultAzureCredential());
var secret = await client.GetSecretAsync(secretName);
@asadrefai
asadrefai / Create-SelfSignedCertificate.ps1
Created July 7, 2021 10:12
Microsoft's recommended script to create a self signed certificate
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Creates a Self Signed Certificate for use in server to server authentication
.DESCRIPTION
.EXAMPLE
.\Create-SelfSignedCertificate.ps1 -CommonName "MyCert" -StartDate 2015-11-21 -EndDate 2017-11-21
This will create a new self signed certificate with the common name "CN=MyCert". During creation you will be asked to provide a password to protect the private key.
.EXAMPLE
.\Create-SelfSignedCertificate.ps1 -CommonName "MyCert" -StartDate 2015-11-21 -EndDate 2017-11-21 -Password (ConvertTo-SecureString -String "MyPassword" -AsPlainText -Force)
@asadrefai
asadrefai / SetDefaultPageLayout.ps1
Created May 16, 2018 13:04
set default page layout in SharePoint using CSOM PowerShell
Try{
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.Client.Publishing.dll'
}
catch {
Throw "Unable to load SharePoint Client runtime"
}
@asadrefai
asadrefai / MaskPassword.cs
Created January 2, 2017 07:21
c# function to mask and display password string as *
/// <summary>
/// Function to display password string as *, return appropriate one to the code.
/// </summary>
public static string ReadLineMasked(char mask = '*')
{
var sb = new StringBuilder();
ConsoleKeyInfo keyInfo;
while ((keyInfo = Console.ReadKey(true)).Key != ConsoleKey.Enter)
{
if (!char.IsControl(keyInfo.KeyChar))
@asadrefai
asadrefai / AddDllInGAC.ps1
Created December 22, 2016 05:17
Add dll in GAC using PowerShell
#Note that you should be running PowerShell as an Administrator
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Path\To\DLL.dll")
@asadrefai
asadrefai / LargeFileUpload.ps1
Last active December 19, 2022 17:53
Large file upload in SharePoint Online with CSOM PowerShell
Try{
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
Write-Host $_.Exception.Message
Write-Host "No further parts of the migration will be completed"
}
Function UploadFileInSlice ($ctx, $libraryName, $fileName, $fileChunkSizeInMB) {
@asadrefai
asadrefai / ChangePageLayout.ps1
Created August 11, 2015 09:40
Change page layout of a SharePoint publishing page using CSOM PowerShell
function ChangePageLayout()
{
param(
[Parameter(Mandatory=$true)][string]$siteurl,
[Parameter(Mandatory=$false)][System.Net.NetworkCredential]$credentials,
[Parameter(Mandatory=$false)][string]$PageName,
[Parameter(Mandatory=$false)][string]$PageLayoutName,
[Parameter(Mandatory=$false)][string]$PageLayoutDisplayName,
[Parameter(Mandatory=$false)][string]$Title,
[Parameter(Mandatory=$false)][bool]$isCustomPageLayout