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 Create SharePoint Sub Site using PowerShell



In this Post we are going to look how to create a SharePoint Sub site using PowerShell. I had to do some research but it was not hard to find a solution.

In below Example we will be creating sub site and assigning a Template once it is created. I had a requirement to set a custom Template with basic data, which I have created. It had Many lists and libraries, Nintex Forms, Nintex Workflows Managed Meta Data columns, Content Types etc.

Publishing Nintex Forms through PowerShell I will be discussing in my next post

Once I created the Sub site, I faced a challenge to set the master page because, newly created site is not getting inherited the master page from the parent.
Finally I found the solution from SharePoint Exchange Gentleman called "Evgenii Vilkov" had provided a simple solution to my problem. I have provided a link to same post within comments section in the code snippet.

In below Example I have included logging to a CSV file with the use of Add-Content function in PowerShell

<#
References
=================
for setting the Master Page, I have refered the code snippet from below post in SharePiont Stack Exchange replyied by "Evgenii Vilkov" in his reply to a Question Reply


#>
$logFilePath ="C:\scripts\logs" # Path to save the log file

function Create-SubSite
{
    param($spSite, $subSiteURL, $siteTemplate, $siteName )

    #$spSite => Site collection
    #subSiteURL => URL of the site which we are trying to assign the Master Page
    #$steTemplate => Template of the site I have used a custom template.
    #$siteName => Name of the site
   

    try{

        #Initiating the New Site
        $newweb = New-SPWeb -Url $subSiteURL -Name $siteName;
       
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | new site creation Succussfull | success";
        #assign Custom Template
        $newweb.ApplyWebTemplate($siteTemplate);
       
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Template Assignment Succussfull | success";

        #Set Master Page for the site

        Write-Host "Master Page Assignment in progress..."

        $newweb.MasterUrl = $spSite.RootWeb.ServerRelativeUrl +  $systemMasterPage
        $newweb.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + $systemMasterPage;
        $newweb.Update();

        Write-Host $newweb.MasterUrl;               
        Write-Host "Master Page Assignment Completed."

        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Master Page Assignment Succussfull | success";

    $newweb.Dispose();
    }
    catch{
        $ErrorMessage ='';
        $ErrorMessage = $_.Exception.Message
        Write-Host $ErrorMessage;
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Create Sub Site Failed  | $($ErrorMessage)";       
       
    } 

}

Once I implement this, my next challenge was to publish Nintex Forms on newly created site. for some reason all my Nintex forms were successfully included in the new site. But I had to republish each and every form manually.

I will discuss about how I was able to solve the problem in a future post.

I hope this might help any one like me who is looking for a code snippets to automate.

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