Thursday, October 7, 2021

Powershell: save list and library as template in sharepoint 2013 / 2016 / 2019

Leave a Comment

  

Business Scenario

This is quite usual scenario where developers or site admin have to move newly create list or libraries from Dev environment to Staging / Production. This blog will show options for doing it manually as well as through Powershell. This is series of blog where:

  1. Save list / library with or without data as list template through powershell.
  2. Create new list / library using template through powershell

 

 Manual way of saving list as template

After creating and verifying business usability of custom list or library with all custom column etc, if we want to save this list as list template to be used on same farm / environment or other other farms to recreate same list with similar schema or data, we need to follow these steps:

  • Open your custom sharepoint list and go to list settings.
  • From settings under permission and management section, click save list as template
  • Provide template name and file name and choose option if need to save list data along with schema.
  • Once list template save it prompts for operation successful page message along with link to list template gallery

 

 

 Powershell to the rescue

Lets say we need to save multiple list as list templates, so instead of doing it manually we can customize following script:

#Import SharePoint Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration parameters
$WebURL="http://sharepoint site"
$ListName="List name"
$TemplateName="Template Name"
$TemplateFileName="Template File Name"
$TemplateDescription="Some description"
$SaveData = $false
 
#Get the Web and List objects
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
 
#Save List as Template
$List.SaveAsTemplate($TemplateFileName, $TemplateName, $TemplateDescription, $SaveData)
Write-Host "List Saved as Template!




and saved list is available in list template gallery

Read More

Tuesday, July 28, 2020

Orphaned features in SharePoint Farm

Leave a Comment
While working for a client project and deploying existing SharePoint solution face issue about orphaned features which gives error a feature with feature id XXXXX already exist in farm.

There are two solution either in feature set force install option or delete orphaned features using following powershell commands:

Get-SPFeature | ? {$_.Scope -eq $null}


$feature = Get-SPFeature | ? {$_.DisplayName -eq "FeatureToBeDeleted"}

$feature.Delete()


Read More

Saturday, July 18, 2020

Solution: Datepicker not working within updatepanel

Leave a Comment
Problem Statement:

Got chance to work on an existing project where in webform within update panel bootstrap datepicker control isn't working after callback/postback.

Solution:

Bind & Update datepicker controls at endRequest as follows:
$(function () {
   bindDatePickers(); // bind date picker on first page load
   Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindDatePickers); // bind date picker on every UpdatePanel refresh
});

function bindDatePickers() {
   $('#<%= txtStartDateFrom.ClientID%>').datepicker({
      uiLibrary: 'bootstrap4'
   });
   $('#<%= txtEndDateTo.ClientID%>').datepicker({
      uiLibrary: 'bootstrap4'
   });
 }
Read More

Wednesday, June 3, 2020

PowerShell: Migrate SharePoint list content from one farm to other farm using CSOM

Leave a Comment
Scenario:
I met a scenario, where I need to migrate some list items (around 500+) from one farm (development) to another farm (Staging) and then to Production farm.

Ideal scenario probably would be to create list schema with content. However, in my current scenario I used CSOM for copying list items. Also in my scenario I just can't replace current content of destinate list item. I must have to just copy/move list items which are required for newly developed components.

Solution:

First I save list as list template along with content from source farm and create new list in destination farm using saved & imported list template definition. Then I delete list items which is not supposed to be copied. Although this can be skipped or cater through power shell script. However, due to quick delivery I wrote following power shell script which serve my purpose.

#Since I am doing for On-Premises, so I must have to import Client runtime for On-Premises
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"

#Here I am getting user credentials
$siteURL="http://SomeSiteUrl/"
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$pwd=Read-Host -Prompt "Enter password" -AsSecureString
$creds=New-Object System.Net.NetworkCredential("domain\UserName",$pwd)
$ctx.Credentials=$creds

$sourceList=$ctx.Web.Lists.GetByTitle("[{ImportedListFromDestinationFarm}]")
$destList=$ctx.Web.Lists.GetByTitle("[{SourceListWhereDataNeedToCopied}]")
$sourceListItems=$sourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$fields=$sourceList.Fields
$ctx.Load($sourceListItems)
$ctx.Load($sourceList)
$ctx.Load($destList)
$ctx.Load($fields)
$ctx.ExecuteQuery() 

#Here I am making sure, fields of list item isn't hiddent, or not readonly & don't have attachment
#and then updating in destination list
foreach($item in $sourceListItems)
 {
 #Write-Host $item.ID
$listItemInfo=New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
$destItem=$destList.AddItem($listItemInfo)
foreach($field in $fields)
{
#Write-Host $field.InternalName "-" $field.ReadOnlyField
if((-Not($field.ReadOnlyField)) -and (-Not($field.Hidden)) -and ($field.InternalName -ne "Attachments") -and ($field.InternalName -ne "ContentType"))
{
#Write-Host $field.InternalName "-" $item[$field.InternalName]
$destItem[$field.InternalName]=$item[$field.InternalName]
$destItem.update()
} }}
$ctx.ExecuteQuery()
Read More