Skip to content

Instantly share code, notes, and snippets.

@rezanid
rezanid / GetNamespaceStyle.cs
Created July 9, 2025 18:00
How to get .editorconfig options using Roslyn / Visual Studio SDK
public string GetNamespaceStyle(Document document)
{
var options = await document.GetOptionsAsync(cancellationToken);
var optionKey = new OptionKey(new Option<string>("code_style", "csharp_style_namespace_declarations", "block_scoped"));
return options.GetOption<string>(optionKey);
}
@rezanid
rezanid / ACustomCodeGenerator_GetProjectProperty.cs
Created March 13, 2025 16:44
How to get a project property in a Visual Studio Custom Tool aka Single File Generator
public class ACustomCodeGenerator : BaseCodeGeneratorWithSite
{
private string? GetProjectProperty(string propertyName)
{
ThreadHelper.ThrowIfNotOnUIThread();
return GetService(typeof(IVsHierarchy)) is IVsHierarchy hierarchy ? hierarchy.GetProjectProperty(propertyName) : null;
}
}
@rezanid
rezanid / Delete-Dataverse.ps1
Last active February 15, 2025 17:34
Power Platform Bulk Delete using PSDataverse and Web API filter.
Connect-Dataverse -Url https://something.crm.dynamics.com
do { $result = Send-DataverseOperation "ordercloses?`$select=activityid" |
Select-Object -ExpandProperty Content |
ConvertFrom-Json |
Select-Object -ExpandProperty value |
ForEach-Object { [pscustomobject]@{ "Method"="DELETE"; "Uri"="ordercloses($($_.activityid))" } | ConvertTo-Json } |
Send-DataverseOperation -BatchSize 200 -MaxDop 50 -ErrorVariable ErrOperation -OutputTable
} while ( $result )
@rezanid
rezanid / GetVisualStudioDocument.cs
Last active January 21, 2025 22:03
How to get the Document object in Visual Studio extensions
// The wrong way:
private GetVisualStudioDocument(SnapshotPoint triggerLocation)
{
// Get the VisualStudioWorkspace service
var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var workspace = componentModel.GetService<VisualStudioWorkspace>();
// Retrieve the document associated with the trigger location
var documentId = workspace.CurrentSolution.GetDocumentId(triggerLocation.Snapshot.TextBuffer.AsTextContainer());
if (documentId == null) return CompletionContext.Empty;
@rezanid
rezanid / postman-visualize-plugintracelogs.js
Created April 7, 2024 12:12
Visualize Postman results for Power Platform's Plugin Trace Logs
const moment = require('moment');
var template = `
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-cuYeSxntonz0PPNlHhBs68uyIAVpIIOZZ5JqeqvYYIcEL727kskC66kF92t6Xl2V" crossorigin="anonymous"></script>
<style>
.table-responsive-custom {
display: block;
width: 100%;
overflow-x: auto;
@rezanid
rezanid / Postman-PreRequest-Authentication.js
Last active March 10, 2025 14:56
Postman Pre-Request script to authenticate and refresh authentication token using OAuth2 device flow
// Environment Variables:
// url: <your-resource> example: https://myfancyapp.crm4.dynamics.com
// clientid: <user-clientid-from-appreg> example: 1950a258-227b-4e31-a9cf-717495945fc2
// To know how to use this script, please read the following blog post:
// https://bycode.dev/2024/04/04/automatically-authenticate-in-postman-with-pre-request-scripts/
const utils = {
auth: {
message: "",
async refreshAuth() {
@rezanid
rezanid / Directory.Build.props
Created September 18, 2023 16:56
Upgrade C# language version for .NET Framework 4.x
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<LangVersion>11</LangVersion>
<Nullable>disable</Nullable>
</PropertyGroup>
<PropertyGroup>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>
@rezanid
rezanid / Add-Path.ps1
Last active August 28, 2023 14:38
Adding PATH to Windows permanently (and safely) the right way using PowerShell
function Add-Path {
param(
[Parameter(Mandatory, Position=0)]
[string] $LiteralPath,
[ValidateSet('User', 'CurrentUser', 'Machine', 'LocalMachine')]
[string] $Scope
)
Set-StrictMode -Version 1; $ErrorActionPreference = 'Stop'
@rezanid
rezanid / JsonConverters.cs
Created May 7, 2023 19:05
Json Converters to facilitate unit testing for Power Platform's Custom APIs
using Microsoft.Xrm.Sdk;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace MyFancyPlugins.Tests.JsonConverters;
/// <summary>
/// Allows serialization / deserialization of <see cref="Microsoft.Xrm.Sdk.EntityCollection". This type is
/// intended for unit-testing only./>