NPM unable to authenticate with proxy

Scenario: Trying to install package with NPM, but it is unable to authenticate with the proxy


Solution:

Connecting npm proxy through Telerik Fiddler

  1. Tools > Options
  2. Act as system proxy on startup
  3. Remember the listen port
  4. Under Rules > Automatically Authenticate  (Check this)
  5. Set proxy to point to use fiddler as proxy, proxy address is http://localhost:<port from previous setting>
  6. npm config set proxy http://localhost:8888
  7. npm config set https-proxy http://localhost:8888
  8. Run your install command, it should be working as expected.

 

PowerShell: Simple Logging helper

Scenario: How to log all my message with different color on the console and with category into CSV files


Code Snippet

$global:errorCount = 0
$logFolder = "$root\Logs"

function EnsureLogFolder() {
	$command = $MyInvocation.MyCommand.Name
	if(!(Test-Path $logFolder)) {		
		$newFolder = New-Item -ItemType Directory -Path $logFolder -Force
		LogInfo "[$command]: Log folder does not exist, provisioning folder $logFolder..."
	}
}

function LogMessage($msg, $category) {	
	$logFile = "$logFolder\log.csv"
	$mailFile = "$logFolder\mail.csv"

	$Properties = @{		
		"Category" = $category
		Date = (Get-Date)		
		Message = $msg
	}
	
	switch($category) {
		"INFO" { Write-Host $msg }
		"DEBUG" { Write-Host "================ $msg ================" -ForegroundColor Magenta  }
		"SUCCESS" { Write-Host $msg -ForegroundColor Green }
		"WARNING" { Write-Host $msg -ForegroundColor Yellow }
		"ERROR" { Write-Host $msg -ForegroundColor Red }
		"MAIL" { }
	}

	$obj = New-Object psobject -Property $Properties
	if($category -ne "MAIL") {		
		$obj | Select Date, Category, Message | Export-Csv $logFile -NoTypeInformation -Append
	}
	else {
		$obj | Select Date, Message | Export-Csv $mailFile -NoTypeInformation -Append
	}
}

function LogEmail($msg) {
	LogMessage $msg "MAIL"
}

function LogDebug($msg) {
	LogMessage $msg "DEBUG"
}

function LogInfo($msg) {	
	LogMessage $msg "INFO"
}

function LogSuccess($msg) {
	LogMessage $msg "SUCCESS"
}

function LogWarning($msg) {
	LogMessage $msg "WARNING"
}

function LogError($msg) {
	LogMessage $msg "ERROR"
	$global:errorCount++
}

SharePoint Online (Office PnP): Populate default fields in View dynamically

Scenarios: How to add all fields into view dynamically


Code Snippet

Function AddDefaultFieldsToView {
	Param (
		[Parameter(Mandatory=$True)] 
        [string]$ContentTypeName,
		[Parameter(Mandatory=$True)] 
        [string]$ViewName,
		[Parameter(Mandatory=$True)] 
        [string]$ListName
	)

	$view = Get-PnPView -Identity $ViewName -List $ListName
	if($view -eq $null) {
		Write-Host "View $ViewName does not exist in $ListName"
		return
	}

	$ct = Get-PnPContentType -InSiteHierarchy -Identity $ContentTypeName	
	$columns = Get-PnPProperty -ClientObject $ct -Property "Fields"

	Write-Host "Applying default columns to View: '$ViewName' from ContentType: '$ContentTypeName'"
    foreach ($column in $columns | Where-Object { !$_.Hidden } ) {        
		if($view.ViewFields -contains $column.InternalName){
			Write-Host ("'" + $column.Title + "' already exist in View")
			continue
		}

        if ($column.Title -match "Modified By" -or
			$column.Title -match "Created By" -or
			$column.Title -eq "Name" -or
			$column.Title -eq "Content Type" -or
			$column.Title -eq "Title"
		) {
			#system fields				
            continue
        }
		
        $view.ViewFields.Add($column.Title)        
		Write-Host ("Added field '" + $column.Title + "' to '" + $ViewName + "'")
    }

	$view.Update()
	Execute-PnPQuery
	Write-Host "Applied default columns"
}

SharePoint Online (Office PnP) PowerShell: How to set list experience

Scenario: How to manage List experience with Office PnP PowerShell


List experience settings: List Settings > Advance Settings > List Experience


Code snippet

Function SetListExperienceToClassic() {
	Param (
		[Parameter(Mandatory=$True)]
        [string]$ListName
	)
	$list = Get-PnPList $ListName -Includes ListExperienceOptions -ErrorAction SilentlyContinue
	if($list -ne $null) {
		#Auto
		#NewExperience
		#ClassicExperience
		$list.ListExperienceOptions = "ClassicExperience"
		$list.Update()
		Execute-PnPQuery
	}
}

SharePoint Online: Team site is not responsive

Scenario: My team site is not responsive on mobile devices

  • If you have an existing custom master page, by using Custom Master Page and provision script to apply it
    PnP Example:

    #enable publishing feature on all sites
    
    $subWebs = Get-SPOSubWebs
    foreach ($web in $subWebs) {
    	Enable-SPOFeature -Identity "94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb" `
                              -Force -Web $web.ServerRelativeUrl
    }
    #Add/update the master page
    Add-SPOMasterPage -SourceFilePath "C:/temp" -Title "master.aspx" `
                      -Description "customer master page" `
                      -DestinationFolderHierarchy "test" -UiVersion 15
    
    #Apply it on the root site
    Set-SPOMasterPage -MasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
    		  -CustomMasterPageServerRelativeUrl $MasterPageServerRelativeUrl
    
    #Apply it to all subsites
    $MasterPageServerRelativeUrl = "/_catalogs/masterpage/test/master.aspx"
    $subWebs = Get-SPOSubWebs
    foreach ($web in $subWebs) {
    Set-SPOMasterPage -MasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
                -CustomMasterPageServerRelativeUrl $MasterPageServerRelativeUrl `
      	    -Web $web.ServerRelativeUrl
    }
    
    
  • Register Site Action and use jQuery to apply responsive elements: e.g: https://github.com/SharePoint/PnP-Tools/tree/master/Solutions/SharePoint.UI.Responsive, but if you already have customized publishing site, you might have to modify the scripts to apply these responsive actions only to certain site templates.
  •     .\Enable-SPResponsiveUI.ps1 -TargetSiteurl "https://intranet.mydomain.com/sites/targetSite"
    
  • Using SPMeta2, example code snippet
    
     public ModelNode GetModel()
     {            
           var model = SPMeta2Model.NewWebModel(rootWeb =>
           {
               rootWeb.AddWeb(intranetWebs.TeamSites, web =>
               {
                    web.AddMasterPageSettings(intranetMasterPages.masterPageSettings);
                      
               });
           });
           return model;
    }
    
    public static class intranetMasterPages
    {
        public static MasterPageSettingsDefinition masterPageSettings = new MasterPageSettingsDefinition
        {
            // both should be site relative URLs
            SiteMasterPageUrl = "/_catalogs/masterpage/Intranet.master",
            SystemMasterPageUrl = "/_catalogs/masterpage/Intranet.master"
            //"/_catalogs/masterpage/seattle.master"
        };
    }
    
  • More(TBA)…

SharePoint Online: List and List Items operation with PnP PowerShell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

Retrieving List


#Get all lists
$lists = Get-SPOList  
foreach($list in $lists){              
       #Properties
       $list.Title  
       $list.Description  
       $list.ItemCount  
       #etc...
}  

#Get Single list
Get-SPOList "Test"

Create List
This will create the list if the list does not exist

Function CreateList {
	Param (
		[Parameter(Mandatory=$True)]
		[string]$title,
		[Parameter(Mandatory=$True)]
		[string]$removeContentType,
		[Parameter(Mandatory=$True)]
		[string]$contentType,
		[Parameter(Mandatory=$True)]
		[string]$template,
		[Parameter(Mandatory=$False)]
		[string]$url
	)

	$list = Get-SPOList $title -ErrorAction SilentlyContinue
	if($list -eq $null) {
		Write-Host "Creating List title:" $title "template:" $template "url:" $url -ForegroundColor Yellow
		if($url -eq $null) {
			New-SPOList -Title $title -Template $template -Url $title
		}
		else {
			New-SPOList -Title $title -Template $template -Url $url
		}

		if($removeContentType -ne $null){
			Remove-SPOContentTypeFromList -List $title -ContentType $removeContentType
		}
		
		if($contentType -ne $null) {
			Add-SPOContentTypeToList -List $title -ContentType $contentType -DefaultContentType
		}
				
		Write-Host "Created List" $title -ForegroundColor Green
	}
	else {
		Write-Host "List" $title "already exist" -ForegroundColor Yellow
	}
}

Execute the function
CreateList -title "Custom Link List" `
           -removeContentType Link `
           -contentType "SpecialLink" `
           -template Links

Update List

#Update List
Set-SPOList -Identity "Test" -Title "Test List 2" 

#Unique Permission
Set-SPOList -Identity "Test" -BreakRoleInheritance

Delete List

Remove-SPOList -Identity "Test" -Force

How to add an item

#Add an empty item
Add-SPOListItem -List "Test"

#Add an item with value(s)
Add-SPOListItem -List "PnPList" -Values @{"Title"="1";"TestColumn"="1"}

Example: Retrieving view

Get-SPOView -List "Test"

Example: Add view to list

Add-SPOView -List "Target List" -SetAsDefault `
	-Title "View Title" `
	-ViewType None `
	-Fields @("Field you wanted to display: e.g: ID", "URLwMenu", "etc..") `
	-Query "CAML Query: <Where>...</Where>"

Example: Remove view from list

Remove-SPOView -List "Target List" -Identity "View Title" -Force

SharePoint Online: Content Type and Site Column operation with PnP PowerShell

First, you will have to connect to your SharePoint Online Site

Example: Connect to SPO

$siteurl = "https://yourspsite.sharepoint.com"  
Connect-SPOnline -Url $siteurl  
$ctx = Get-SPOContext  

========== Site Columns ==========

Example: Adding a site column

#Adding Text Column
Add-SPOField -DisplayName "Text Column" -InternalName "TextColumn" -Group "Test" -Type Text -Required
#Adding Note Column
Add-SPOField -DisplayName "Note Column" -InternalName "NoteColumn" -Group "Test" -Type Note -Required
#Adding Choice Column
Add-SPOField -DisplayName "Choice Column" -InternalName "ChoiceColumn" -Group "Test" -Type Choice -Choices @("Test Value 1","Test Value 2","etc..")
#Other field types such as: Boolean, Integer, DateTime, Lookup, Number, etc...

Example: Retrieving site column(s)

#Get all site column
Get-SPOField

#Get single site column
Get-SPOField -identity "TextColumn"

Example: Retrieving site column properties

#Get all site column
$fields = Get-SPOField
foreach(var $field in $fields) {
    #Properties
    $field.Title
    $field.InternalName
    $field.DefaultValue
    #etc...
}

#Get single site column
$field = Get-SPOField -identity "TextColumn"
#Properties
$field.Title
$field.InternalName
$field.DefaultValue
#etc...

========== Content Type ==========

Example: Create Content Type

$parentCT = Get-SPOContentType -Identity Link
Add-SPOContentType -Name "SpecialLink" -Group "Custom" -ParentContentType $parentCT

Example: Associate site column with content type

#By Name
Add-SPOFieldToContentType -Field "TextColumn" -ContentType "SpecialLink"

#By Reference
$field = Get-SPOField -identity "TextColumn"
$parentCT = Get-SPOContentType -Identity "SpecialLink"
Add-SPOFieldToContentType -Field $field -ContentType $parentCT

Example: Deleting Site Column

Remove-SPOField -Identity "TextColumn" -Force