Publish files which are not published by users

#Set-ExecutionPolicy Unrestricted
#Set-ExecutionPolicy RemoteSigned
cls

$Host.UI.RawUI.WindowTitle = "-- Publish Files --"

$scriptdir = $PSScriptRoot


#$DatePrefix ="XML\"+ (Get-Date).ToString("MMddyyyyHHmm")
$DatePrefix ="XML\"

Write-Host "Loading the CSOM library" -foregroundcolor yellow

Add-Type -AssemblyName System.Web
# Paths to SDK. Please verify location on your c omputer.
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Publishing.dll"
Add-Type -Path "$scriptdir\Microsoft.SharePoint.Client.Taxonomy.dll"
Write-Host "Succesfully loaded the CSOM library" -foregroundcolor green

# Insert the credentials and the name of the admin site
$Username="gaurav@goyal.com";
$AdminPassword=Read-Host -Prompt "Password" -AsSecureString;

$AdminUrl="<<Your Site Collection URL"


$StyleLibrary="Style Library"
$MasterPage="Master Page Gallery"

Write-Host "Connecting to site" -foregroundcolor yellow
   
$ctx=New-Object Microsoft.SharePoint.Client.ClientContext($AdminUrl)
$ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $AdminPassword)



Function getUnPublishedFiles($listName){

    Write-Host ""
    Write-Host ""
    Write-Host ""
    Write-Host "Working on $listName....." -ForegroundColor Gray


    Write-Host "Creating file $listName.txt" -foregroundcolor yellow
    $xmlFilePath = "$scriptdir\XML\$listName.txt"

    #Create Export Files
    New-Item $xmlFilePath -type file -force

    #Managed Client Object Model CAML Query Syntax
    $spList = $ctx.Web.Lists.GetByTitle($listName);
    $localWeb=$ctx.Web;
    $ctx.Load($spList);
    $ctx.Load($localWeb);
    $ctx.ExecuteQuery();

    if ($spList -ne $null -and $spList.ItemCount -gt 0)
    {
       #$camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery()
       $camlQuery =[Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery(1000);

       $camlQuery.ViewXml = "<View Scope='RecursiveAll'>"+
                                  "<Query>"+
                                     "<OrderBy>"+
                                        "<FieldRef Name='FileRef' />"+
                                        "</OrderBy>"+
                                    "<Where>"+
                                      "<Neq>"+
                                        "<FieldRef Name='_ModerationStatus' />"+
                                        "<Value Type='ModStat'>0</Value>"+
                                      "</Neq>"+
                                    "</Where>"+
                                  "</Query>"+
                                  "<ViewFields>"+
                                    "<FieldRef Name='FileLeafRef' />"+
                                    "<FieldRef Name='FileRef' />"+
                                    "<FieldRef Name='_ModerationStatus' />"+
                                  "</ViewFields>"+
                                "</View>";

       $listItems = $spList.GetItems($camlQuery);
       $ctx.Load($listItems);
       $ctx.ExecuteQuery()
       foreach($item in $listItems)
       {  
            $fileURL=$item["FileRef"].ToString()
            Write-Host $fileURL
            Add-Content $xmlFilePath $fileURL
           
            $newFile = $localWeb.GetFileByServerRelativeUrl($fileURL);
            $newFileItem = $newFile.ListItemAllFields;
            $ctx.Load($newFile);
            $ctx.Load($newFileItem);
            $ctx.ExecuteQuery();

            $newFile.Publish('Auto publsieh');
       }

       Invoke-Item "$scriptdir\XML\$listName.txt"
    }
}


getUnPublishedFiles($StyleLibrary)
getUnPublishedFiles($MasterPage)

Invoke-Item "$scriptdir\XML\"

Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

Change Language for current user with JSOM in SharePoint Online

SharePoint 2010 CSS Chart