Category Archives: CSOM with PS

Client Side Object Model using the PowerShell scripting

PowerShell CSOM – Hide site columns from edit and new form

In this blog I will explain how to hide some of the columns from the edit and the new form for the list. CSOM has the API to hide column. There are three main api to hide / show the column

  1. SetShowInEditForm
  2. SetShowInNewForm
  3. SetShowInDisplayForm

Consider the below sample scenario where you are developing a Leave Application using custom list. When the user fills in the Leave application you don’t want to show status and comment field. So in the below scenario you want to hide status and comment field from edit form and the new form for the list. But the same fields need to be shown on the view form.

Run the below script to hide those fields from both the form.

https://github.com/msisgreat/ps/blob/master/HideColumn.ps1

Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"

$username = "username@domain.com"
$password = ""
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
Write-Host "Connecting to site ..."
$srcUrl = "" ## https://sitename/sites/sitename
$srcLibrary = "Leave Application"
$srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl)
$srcContext.Credentials = $credentials
$srcWeb = $srcContext.Web
$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)
$srcContext.Load($srcList)
$srcContext.ExecuteQuery()
Write-Host "Connected successfully"

$fields = @("Application Status","Approver Comment")
foreach($fieldname in $fields)
{
    Write-Host "Hiding from edit & new form: " $fieldname
    $fieldToEdit = $srcList.Fields.GetByTitle($fieldname);
    $srcContext.Load($fieldToEdit)
    $srcContext.ExecuteQuery()
    $fieldToEdit.SetShowInEditForm($false)
    #$fieldToEdit.Update()
    $srcContext.ExecuteQuery()
    $fieldToEdit.SetShowInNewForm($false)
    #$fieldToEdit.Update()
    $srcContext.ExecuteQuery()
    #$srcWeb.Update()

}

Sample PowerShell output

After the script is successfully executed the fields are hidden in the new and edit form. Below is the screenshot after the fields are hidden

Migration of documents in SharePoint Online between site collections

In this blog I am going to share the code to copy documents between site collections using CSOM on Office 365 SharePoint. The below code uses the recursive method to get all documents from source site collection and copy it to the target site collection. The script also preserves the modified and modified by details after copying.

In this blog I am using the latest CSOM targeted to Office 365, you can download the latest here

The sample code is in the PowerShell command. First download the CSOM dll and save it to the folder to be used. The PowerShell command will load the specific dll to use the API.

https://github.com/msisgreat/ps/blob/master/DocMigrationSitecollections.ps1


Add-Type -Path "C:\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Microsoft.SharePoint.Client.Runtime.dll"
$username = "name@domain.com"
$password = "”
$srcUrl = "" ## use the full URL of the site
$destUrl = "" ## use the full URL of the site
$srcLibrary = ""
$destLibrary = ""
$destinationFolder = "/sites//Shared Documents//" ## Make sure the folder name ends with /
$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)
function CreateFolders
{
param (
[Parameter(Mandatory=$true)] $srcfolder,
[Parameter(Mandatory=$true)] [string] $destFolderPath
)
Write-Host "Source Folder:" + $srcfolder.Name + " dest folder:" + $destFolderPath -ForegroundColor Yellow
$SPOFolder = $destWeb.GetFolderByServerRelativeUrl($destFolderPath)
$FolderName = $srcfolder.Name
$NewFolder = $SPOFolder.Folders.Add($FolderName)
$destWeb.Context.Load($NewFolder)
$destWeb.Context.ExecuteQuery()
Write-Host "Folder Created - " + $FolderName -ForegroundColor Yellow
## get source folder details
$SrcFolderListItem = $srcfolder.ListItemAllFields
$srcContext.Load($SrcFolderListItem)
$srcContext.ExecuteQuery()
####
$SPOFolderItem = $NewFolder.ListItemAllFields;
$replacedUser =$destWeb.EnsureUser($SrcFolderListItem["Editor"].Email)
$SPOFolderItem["Editor"] = $replacedUser
$replacedUser =$destWeb.EnsureUser($SrcFolderListItem["Author"].Email)
$SPOFolderItem["Author"] = $replacedUser
$SPOFolderItem["Created"] = $SrcFolderListItem["Created"]
$SPOFolderItem["Modified"] = $SrcFolderListItem["Modified"]
$SPOFolderItem.Update()
$destContext.Load($NewFolder)
$destContext.ExecuteQuery()
$fileCol = $srcfolder.Files
$srcContext.Load($fileCol)
$srcContext.ExecuteQuery()
### Load the file to hash table to check with Target library.
$hashFiles = @{}
$DestfileCol = $NewFolder.Files
$destContext.Load($DestfileCol)
$destContext.ExecuteQuery()
foreach ($destFile in $DestfileCol){
$hashFiles.add($destFile.Name,$destFile.Name)
}
foreach ($f in $fileCol)
{
if($hashFiles.ContainsKey($f.Name) -ne "true") ## check whether file name already exists
{
$srcContext.Load($f)
$srcContext.ExecuteQuery()
$id = $srcfolder.Name
$nLocation =$NewFolder.ServerRelativeUrl.TrimEnd("/") + "/" + $f.Name
Write-Host $nLocation
try
{
$fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($srcContext, $f.ServerRelativeUrl)
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($destContext, $nLocation, $fileInfo.Stream,$true)
$ListItem = $f.ListItemAllFields
$srcContext.Load($ListItem)
$srcContext.ExecuteQuery()
$fileCreated = $destWeb.GetFileByServerRelativeUrl($nLocation)
$destContext.Load($fileCreated)
$destContext.ExecuteQuery()
$DestListItem = $fileCreated.ListItemAllFields;
$replacedUser =$destWeb.EnsureUser($ListItem["Editor"].Email)
$DestListItem["Editor"] = $replacedUser
$replacedUser =$destWeb.EnsureUser($ListItem["Author"].Email)
$DestListItem["Author"] = $replacedUser
$DestListItem["Created"] = $ListItem["Created"];
$DestListItem["Modified"] = $ListItem["Modified"];
$DestListItem.Update()
$destContext.Load($fileCreated)
$destContext.ExecuteQuery()
}
catch
{
Write-Host $_ -ForegroundColor Red
}
}
}
$fL1FolderColl = $srcfolder.Folders
$srcContext.Load($fL1FolderColl);
$srcContext.ExecuteQuery();
foreach ($myFolder in $fL1FolderColl)
{
$srcContext.Load($myFolder)
$srcContext.ExecuteQuery()
CreateFolders $myFolder $NewFolder.ServerRelativeUrl
}
}
#### The main script starts here ######
$srcContext = New-Object Microsoft.SharePoint.Client.ClientContext($srcUrl)
$srcContext.Credentials = $credentials
$destContext = New-Object Microsoft.SharePoint.Client.ClientContext($destUrl)
$destContext.Credentials = $credentials
$srcWeb = $srcContext.Web
$srcList = $srcWeb.Lists.GetByTitle($srcLibrary)
$query = New-Object Microsoft.SharePoint.Client.CamlQuery
$listItems = $srcList.GetItems($query)
$srcContext.Load($srcList)
$srcContext.Load($listItems)
$srcContext.ExecuteQuery()
$destWeb = $destContext.Web
$destList = $destWeb.Lists.GetByTitle($destLibrary)
$destContext.Load($destWeb)
$destContext.Load($destList)
$destContext.ExecuteQuery()
########### this is to copy only certain folders
#$folder = $srcWeb.GetFolderByServerRelativeUrl($srcFolder)
#$srcContext.Load($folder)
#$srcContext.ExecuteQuery()
#Write-Host $destinationFolder
#CreateFolders $folder $destinationFolder
##############
foreach($item in $listItems)
{
if($item.FileSystemObjectType -eq "File")
{
#$srcContext.Load($item)
#$srcContext.ExecuteQuery()
$srcF = $item.File
$srcContext.Load($srcF)
$srcContext.ExecuteQuery()
$rootLocation = $destinationFolder + $srcF.Name
Write-Host $rootLocation
$fileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($srcContext, $srcF.ServerRelativeUrl)
[Microsoft.SharePoint.Client.File]::SaveBinaryDirect($destContext, $rootLocation, $fileContent.Stream,$true)
$fItem = $srcF.ListItemAllFields
$srcContext.Load($fItem)
$srcContext.ExecuteQuery()
$fCreated = $destWeb.GetFileByServerRelativeUrl($rootLocation)
$destContext.Load($fCreated)
$destContext.ExecuteQuery()
$DListItem = $fCreated.ListItemAllFields;
$replacedUser =$destWeb.EnsureUser($fItem["Editor"].Email)
$DListItem["Editor"] = $replacedUser
$replacedUser =$destWeb.EnsureUser($fItem["Author"].Email)
$DListItem["Author"] = $replacedUser
$DListItem["Created"] = $fItem["Created"];
$DListItem["Modified"] = $fItem["Modified"];
$DListItem.Update()
$destContext.Load($fCreated)
$destContext.ExecuteQuery()
}
elseif ($item.FileSystemObjectType -eq "Folder")
{
$srcContext.Load($item)
$srcContext.ExecuteQuery()
$folder = $srcWeb.GetFolderByServerRelativeUrl($item.FieldValues["FileRef"].ToString())
$srcContext.Load($folder)
$srcContext.ExecuteQuery()
##################
Write-Host $destinationFolder
CreateFolders $folder $destinationFolder
}
}
Write-Host “Script End"