none
Powershell Script to add site content link RRS feed

  • Question

  • Hi

    below is my code to add a 'Site contents' link in quick launch menu when it is not present in the site

    but i am not getting any value in "$web = $ctx.Web" and unable to move forward

    have tried many things but none works not even $ctx.Site

    Please suggest what is wrong in it?             

    $adminUPN="XXXXXX@Home500.onmicrosoft.com"
    $orgName="XXXXXX"
    $userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."
    
    Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential
    
    # Begin the process
    $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    $loadInfo3 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
    
    #Add SharePoint PowerShell SnapIn if not already added
    $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'} 
    if ($snapin -eq $null) 
    {    
    	Write-Host "Loading SharePoint Powershell Snapin"    
    	Add-PSSnapin "Microsoft.SharePoint.Powershell"  -EA SilentlyContinue
    }
    
    CLS
    
     $StartTime = $(get-date -f F)
     $timeStamp = Get-Date -format "MM_dd_yy_hh_mm"
    
     #Get Current folder file path
     $invocation = (Get-Variable MyInvocation).Value
     $currentPath = Split-Path $invocation.MyCommand.Path 
     $currentPath = $currentPath + "\"
     
     #Config File Path
     #$configPath = $currentPath + "“Config.xml”"
     $configPath = "C:\Users\kumar.akshat\Desktop\ScriptsSiteContentLink\Config.xml"
     
    #fetching details from config.xml
    [xml]$configXML = Get-Content $configPath
     $inputFileName = [string]$configXML.Config.Constants.InputFileName
     $errorFileName = [string]$configXML.Config.Constants.ErrorFileName
     $outFilePath = [string]$configXML.Config.Constants.OutputFileName
      
     #Source File path containing list of WebApplications in a farm. 
     $webApplFilePath = $currentPath + $inputFileName
     
     #Output File path of the exported AD Security Groups with Site collection and Group Name details. 
     $sitesFilePath = $currentPath + $outFilePath
      
     #File path of the file which will capture all the errors while running the script. 
     $errorPath = $currentPath + $errorFileName + $timeStamp + ".csv"
    
    # Creating object to write logging into the error and output file
     $sitesFile = New-Object System.IO.StreamWriter  $sitesFilePath
     $errorfile = New-Object System.IO.StreamWriter $errorPath
    
    # Fetching SharePoint WebApplications list from a CSV file
    $CSVData = Import-CSV -path $webApplFilePath          
    
    $sitesFile.WriteLine("SiteCollectionName"+","+"SiteURL")
    $errorfile.WriteLine("SiteURL"+"`t"+"ExceptionLevel"+"`t"+"ExceptionMsg");
    
    
    addSiteContentLink $CSVData
    
          $sitesFile.Close()
          $errorfile.Close()
    
    
    # Function to add Site Content link in thes where it does not exists
    
    function addSiteContentLink($CSVData)
    {
      try
         {
         New-Variable -Name "compareText"
         $compareText = "Site contents" 
         New-Variable -Name "username"
         $username = "XXXXXX@Home500.onmicrosoft.com"
         New-Variable -Name "password"
         $password = "XXXXXXXX"
    
         $securePass = ConvertTo-SecureString $password -AsPlainText -Force
    
         foreach ($row in $CSVData)  
         {
                    New-Variable -Name "web"
                    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($row.webUrl)
                    $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePass)
                    $web = $ctx.Web
                    $quickLaunch = $web.Navigation.QuickLaunch
                    
                    #Iterate through each iten in Quick launch menu
                    foreach($quickLaunch in $web)
                    {
                           if ($quickLaunch -contains $compareText)
    			{
    
    			Write-Host "Site Content link Exists!"
    
    			}	
    
    			else 
    			{
    				# Add a new navigation node
    				$navNode = New-Object Microsoft.SharePoint.Client.NavigationNodeCreationInformation
    				$navNode.AsLastNode = $true
    				$navNode.Title = "Site Contents"
    				$navNode.Url = $web.Url + "_layouts/15/viewlsts.aspx"
    				$navNode.IsExternal = $false
    
    				$ctx.Load($quickLaunchColl.Add($navNode))
    				$ctx.ExecuteQuery()
    			}
                    }
                   
                }
            }
                catch
                {
                    Write-Host("Exception at Site Collection Url :" + $currentSite.webUrl)                    
                    $errorfile.WriteLine($currentSite.webUrl+"`t"+"`t"+$_.Exception.Message)              
                }
            
             #Export Data to CSV 
             $sitesCollection | export-csv $sitesFile -notypeinformation
             $site.Dispose()
    
        }

    Thanks

    Paru


    Paru


    Sunday, April 16, 2017 3:45 PM

Answers

  • Load your $web and $quickLaunch objects before access the data...

    $ctx.Load($web)
    $ctx.Load($quickLaunch)
    $ctx.ExecuteQuery()


    • Edited by Dillibabub Sunday, April 16, 2017 5:48 PM
    • Marked as answer by Paru Upreti Sunday, April 16, 2017 10:51 PM
    Sunday, April 16, 2017 5:48 PM

All replies

  • Load your $web and $quickLaunch objects before access the data...

    $ctx.Load($web)
    $ctx.Load($quickLaunch)
    $ctx.ExecuteQuery()


    • Edited by Dillibabub Sunday, April 16, 2017 5:48 PM
    • Marked as answer by Paru Upreti Sunday, April 16, 2017 10:51 PM
    Sunday, April 16, 2017 5:48 PM
  • Thank you so much you solution worked, i used above and also i had to enable the publishing features of the site which i am accessing.


    Paru

    Sunday, April 16, 2017 10:52 PM