Extract Documents to Folder based on Managed Metadata Column - SharePoint

Image
  How to Extract documents from Multiple Document libraries in to Folders. Each has a Managed Metadata Column with Sub Terms. I have created three Functions to  Download Document Create Folder Main Method Addition to above, script will log the Document URL and destination Folder Path in .log file Create Folder   Function   Create-Folder {      param  ( $folderPath )      if  (!( Test-Path  -path  $folderPath ))     {          New-Item   $folderPath  -type directory     } } Download Document ( this is stolen from Stackoverflow 😁 ) Function   Download-Document {      param ( $web ,  $folderPath ,  $docItem )       #File Download Snippet Reffered From : https://stackoverflow.com/questions/43350575/how-to-use-powershell-to-download-files-from-sharepoint        $File  =  $web .GetFile ( $docItem .Url )          $Binary  =  $File .OpenBinary ()          $detinationPath  =  $folderPath  +  "\"  +  $File .Name ;          $Stream  =  New-Object  System.IO.FileStream( $detinationPath

How to Get SP Item filtered from Power Shell






We are going to look how we can retrieve a SharePoint List item by querying a string column.

I should admit I'm really impressed by new Visual Studio Code Power Shell Extension

in below snippet I have described how method will be called to get the output. in second Section you will have all details about the Power Shell Method

$spWeb =Get-SPWeb $spSiteURL;
$spList = $spWeb.Lists["SPListName"];
$spResultItem = Get-SPItem -ItemValue "SharePoint" -spList $spList

you can see implementation of the Get-SPItem Method in below section



function Get-SPItem
{
 #$ItemValue is the value of the column
 #RecordID is the Name of the string column
 param ($ItemValue,$spList) ;
$spqQuery="";
        try
        {
            if ($spList) 
            {       
                $spqQuery = New-Object Microsoft.SharePoint.SPQuery
                $spqQuery.Query = 
                    "   <Where>
                        <Eq>
                            <FieldRef Name='RecordID' />
                            <Value Type='Text'>"+$ItemValue+"</Value>
                        </Eq>
                    </Where>"
                $spqQuery.ViewFields = "<FieldRef Name='RecordID' /><FieldRef Name='Title' />"
                $spqQuery.ViewFieldsOnly = $true;
                $splListItems = $spList.GetItems($spqQuery) ;

                    if(![string]::IsNullOrEmpty($splListItems)) #Check the returned List Items are null
                    {
                        if($splListItems.Count -gt 0)
                        {                   
                            $tempObj = $splListItems[0];       #Get the First Item of the collection            
                            $rtnListItem = $spList.GetItemById($tempObj["ID"]);
                            return $rtnListItem;
                        }
                        else
                        {
                            return $null;
                        }
                    }                
                     else
                    {return $null;
                    }  
            }
            else
            {Write-Host "No Items";
            }
        }
            catch [System.Exception]
            {
                write-host -f red $_.Exception.ToString()
            }
           
}

Pre-Upgrade Check List

Download Our Free SharePoint Pre-Upgrade Check List

* indicates required

Vintage Clan

Popular posts from this blog

System.Data.SqlClient.SqlException (0x80131904): The EXECUTE permission - User Profile Issue

How to Create Host Header to SharePoint Web Application

SharePoint 2013 Access Denied in Site Collections in Web Application