5

I have a powershell script that I am trying to use to add a column (Created) to a specific view in a specific list in multiple webs. The list exists in every sub web. The view exists in every list. There are hundreds of sub webs that I need to traverse through.

So far I have the following but it is not finding any lists that match the $listname even though the lists exist:

$site = Get-SPSite "http://sharepoint/sitecollection"
$column = "Created"
$listname = "My List Name"
$viewname = "My View Name"

$site | Get-SPWeb -limit all | ForEach-Object {

# Get All Lists Where Title Is ...
            $lists = $_.Lists | where {
                            $_.Title -eq $listname
            }

            write-output (" Lists Count : " + $lists.Count)

# Loop libraries
for ($i = 0; $i -lt $lists.Count; $i++)
{
    try
    {
        # Get current view
                                            $view = $lists[$i].Views[$viewname]

        if($view)
        {
            # Delete if already exist
            while($view.ViewFields.ToStringCollection().Contains($column))
            {
                                                                            write-output (" Deleting Column From: " + $site.ToString())
                $view.ViewFields.delete($column)
                $view.Update()
            }

            # Add column
            if(!$view.ViewFields.ToStringCollection().Contains($column))
            {  
                                                                            write-output (" Adding Column To: " + $site.ToString())
                $view.ViewFields.add($column)
                $view.Update()
            }
        }
    }
    catch [Exception]
    {
        write-output ("  Error: " + $_.Exception.ToString())
    }
}
}

$site.Dispose()

How can I get all the lists with a specific name in multiple sites?

3 Answers 3

6

Here is the script I came up with to solve my own question below. Posting it in the hope that it helps somebody else trying to solve similar problem.

# ------------------------------------------------------------------------------
# SCRIPT: ADD-COLUMNTOVIEW.PS1
# DATE: 2013-09-30
# PURPOSE: ADD A COLUMN TO A SPECIFIED VIEW IN A SPECIFIED LIST IN MULTIPLE WEBS WITHIN A SITE COLLECTION.
# PARAMETERS: 1. SITE COLLECTION URL; 2. COLUMNNAME; 3. LISTNAME; 4. VIEWNAME
# MODIFICATIONS: 
# ------------------------------------------------------------------------------

function Add-ColumnToView
{
Param(
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
[string]$ColumnName,
[Parameter(Mandatory=$true)]
[string]$ListName,
[Parameter(Mandatory=$true)]
[string]$ViewName
) # END PARAMS

if ($Url)
{
    $site = Get-SPSite $Url

    if ($site)
    {
        # GET THE SUB WEBS IN SITE COLLECTION   
        $allWebs = $site.allwebs

        # LOOP THROUGH EACH SUB WEB
        foreach ($spweb in $allWebs) 
        {   
            try
            {
                # FIND THE LIST MATCHING THE $LISTNAME
                $list = $spweb.GetList($spweb.Url + "/Lists/" + $ListName)

                # CHECK IF THE LIST OBJECT EXISTS
                if ($list)
                {
                    # GET THE SPECIFIC VIEW
                    $view = $list.Views[$ViewName]

                    # CHECK IF THE VIEW OBJECT EXISTS
                    if($view) 
                    {
                        # DELETE IF ALREADY EXIST
                        while($view.ViewFields.ToStringCollection().Contains($ColumnName))
                        {
                            write-host (" Deleting Column From: " + $spweb.Url) -foregroundcolor yellow
                            $view.ViewFields.delete($ColumnName)
                            $view.Update()
                        }

                        # ADD COLUMN
                        if(!$view.ViewFields.ToStringCollection().Contains($ColumnName))
                        {   
                            write-host (" Adding Column To: " + $spweb.Url) -foregroundcolor green
                            $view.ViewFields.add($ColumnName)
                            $view.Update()
                        }
                    }
                }
            }   
            catch [Exception]
            {
                write-host ("  Error: " + $_.Exception.ToString()) -foregroundcolor red
            }
        }
    }
    else
    {
        write-host ("  Error: " + "Site Collection Not Found.") -foregroundcolor white
    }
    $site.Dispose()
}
else
{
    write-host ("  Error: " + "Site Collection URL Not Specified.") -foregroundcolor white
}
} #END FUNCTION ADDCOLUMNTOVIEW
4

Can you try $_.Lists[$listname] instead of $lists = $_.Lists | where { $_.Title -eq $listname }

Alternatively you can also try retrieving the List by URL, $_.GetList("/Lists/My Contacts")

1
  • This answer helped but I cannot up vote it as I don't have enough points yet. Commented Oct 1, 2013 at 14:44
3

Are you using the Internal name of the list or the display name of the list?.

You can have a list, say "MarketProduct", as internal name of the list but the display name could be "Market Product" so you should access the list as follows

$myweb = Get-SPWeb -Identity http://your_site.com
$list = $myweb.Lists["InternalNameOfTheList"]
2
  • This answer helped but I cannot upvote it as I don't have enough points yet. I am going to add the exact script I used in the hope it will help somebody else. Commented Oct 1, 2013 at 14:40
  • I upvoted your post. I think is interesting Commented Oct 1, 2013 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.