Skip to content

Instantly share code, notes, and snippets.

@rezanid
rezanid / Warmup-WebApp.ps1
Last active January 19, 2017 06:26
Warmup-WebApp is a PowerShell script that can invoke a list of pages in the front-end and even call operations of WCF services to make sure all required initialization and caching is ready and worker processes are started. Please note that this script will only work on PowerShell 3.0+
#Parameters
$logFolder = "C:\Temp\logs"
$logFile = "$logFolder\Warmup_$(Get-Date -Format "MM-dd-yyyy_hh-mm-ss").log"
$timeoutSec = 10
$headers = @{
SOAPAction= "";
"Accept-Encoding" = "gzip,deflate";
"User-Agent" = "PowerShell/Warmup-WebApp"
}
$webRequests = @(
@rezanid
rezanid / SP2010.CSOM.Publishing.cs
Created March 10, 2017 14:36
How to create a publishing page using CSOM in SharePoint 2010
public static void CreatePublishingPage(ClientContext ctx, string listTitle, string pageName, string pageContent)
{
const string publishingPageTemplate = "<%@ Page Inherits=\"Microsoft.SharePoint.Publishing.TemplateRedirectionPage,Microsoft.SharePoint.Publishing,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c\" %> <%@ Reference VirtualPath=\"~TemplatePageUrl\" %> <%@ Reference VirtualPath=\"~masterurl/custom.master\" %>";
var pagesList = ctx.Web.Lists.GetByTitle(listTitle);
var fileInfo = new FileCreationInformation
{
Url = pageName,
Content = Encoding.UTF8.GetBytes(publishingPageTemplate),
Overwrite = true
};
@rezanid
rezanid / RestFullWcfWithCustomTypeInGet.cs
Last active April 19, 2017 09:08
How to enable a WCF REST service to accept custom format parameters and convert them to custom types in GET operations.
public class StackOverflow_6783264
{
public class InputData
{
public string FirstName;
public string LastName;
}
[ServiceContract]
public interface ITest
{
@rezanid
rezanid / FlattenTree.cs
Created May 3, 2017 06:22
LINQ extension to flatten hierarchical structures.
public static class LinqExtensions
{
public static IEnumerable<T> Flatten<T>(this T source, Func<T, IEnumerable<T>> selector)
{
var stack = new Stack<T>();
stack.Push(source);
while (stack.Count > 0)
{
var current = stack.Pop();
yield return current;
@rezanid
rezanid / SessionReadonlyModule.cs
Created September 4, 2017 08:52
Enable concurrent calls to AspNetCompatible WCF REST services by making Session read-only in the service.
public class SessionReadonlyModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
}
public void Dispose()
{
// Nothing to dispose.
@rezanid
rezanid / QueryDraftPages.caml.xml
Created September 13, 2017 12:55
CAML query to retrieve draft pages from SharePoint
<View>
<Query>
<OrderBy>
<FieldRef Name="_UIVersionString"/>
<FieldRef Name="LinkFileName" />
</OrderBy>
<Where>
<Or>
<Eq>
<FieldRef Name="_ModerationStatus"/>
@rezanid
rezanid / AspNetCaching.cs
Last active September 28, 2017 07:27
Caching data in ASP.NET using Lazy<T>
class CachedDataSource
{
protected T RetrieveCachedData<T>(
string cacheKey, Func<T> fallbackFunction, CacheItemPolicy cachePolicy) where T : class
{
var originalData = new CacheItem<Lazy<T>>(new Lazy<T>(fallbackFunction));
var cachedData = (CacheItem<Lazy<T>>)_cacheProvider.AddOrGetExisting(cacheKey, originalData, cachePolicy);
if (cachedData != null)
{
Logger.LogMessage(SeverityLevels.Verbose,
@rezanid
rezanid / Invoke-WebRequestV2.ps1
Last active December 6, 2017 09:37
Send any kind of HTTP request or call SOAP / Web Services in PowerShell V2
function Invoke-WebRequestV2 {
param (
[Parameter(Mandatory=$true)]
[uri]
$Uri,
[Parameter(Mandatory=$false)]
[string]
$Method,
[Parameter(Mandatory=$false)]
[string]
@rezanid
rezanid / RegisterAssembly.ps1
Last active January 15, 2018 10:33
Register an assembly in GAC using PowerShell
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
[System.EnterpriseServices.Internal.Publish] $publish = new-object System.EnterpriseServices.Internal.Publish
$publish.GacInstall(<<FullFilePathToTheDll>>)
@rezanid
rezanid / StringCompressor.cs
Created January 29, 2018 13:49
Compress String into MemoryStream
public class StringCompressor {
readonly string _value;
public void StringCompressor(string value)
{
_value = value;
}
public MemoryStream ToCompressedStream1(CompressionLevel level)
{