PowerShell Get Value of a Hyperlink Field in SharePoint

I needed to get the hyperlink value from a field in a SharePoint List.

Here is how you do it.

$web = get-spweb http://sharepointed.com/sites/YourSite
$list = $web.Lists["Your List Name"]

foreach($i in $list.items)
	{
		$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($i["MyHyperlinkField"])
		write-host $ofldurl.URL 
                write-host $ofldurl.Description
	}
$web.Dispose()

PowerShell Update Current Navigation in SharePoint

Using PowerShell, you can update SharePoint navigation items.
Example for: Display only the navigation items below the current site

$SiteCollection = Get-SPSite "http://taco/SiteCollection/"

foreach ($subSite in $SiteCollection.AllWebs)
	{
		$updateSite = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($subSite)
		$updateSite.Navigation.InheritCurrent = $false
		$updateSite.Navigation.ShowSiblings = $false
		$subSite.Dispose()
	}
	
$SiteCollection.Dispose()

PowerShell to Update Alerts in SharePoint

Was reading Mikes post about Alerts not working in SharePoint 2010 and decided to convert his code to PowerShell.  Read Mikes post before running this script….

$SiteCollection = Get-SPSite "http://YourSharePointSite/"

foreach ($subSite in $SiteCollection.AllWebs)
	{
		foreach($alrt in $subSite.Alerts)
			{
				$origTitle = $alrt.Title
                                $alrt.Title = "UPDATE: $origTitle"
                                $alrt.Update()

                                $alrt.Title = $origTitle
                                $alrt.Update()
				Write-Host $alrt.Title
			}
		$subSite.Dispose()
	}

$SiteCollection.Dispose()

PowerShell Update Top Link Bar or Global Navigation

I needed to update hundreds of sub sites to inherit the same top link bar as the parent site.   Using PowerShell with SharePoint, this only took a minute.

Global Navigation

Display the same navigation items as the parent site (wanted this to be checked)

 
$SiteCollection = Get-SPSite "http://sharepoint/sitecollection/" 
foreach ($subSite in $SiteCollection.AllWebs) 
{ $updateSite = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($subSite) $updateSite.Navigation.InheritGlobal = $true $updateSite.Navigation.ShowSiblings = $false $subSite.Update() 
$subSite.Dispose() } 

$SiteCollection.Dispose() 

PowerShell Exception calling GetLimitedWebPartManager

Using PowerShell against SharePoint, I was trying to remove a web part from a lot of pages.

Time after time I was receiving this error:

Exception calling “GetLimitedWebPartManager” with “2” argument(s): “Unable to cast COM object of type ‘Microsoft.SharePoint.Li
brary.SPRequestInternalClass’ to interface type ‘Microsoft.SharePoint.Library.ISPRequest’. This operation failed because the Q
ueryInterface call on the COM component for the interface with IID

Turned out that I wasn’t trying to work with the correct page. I was trying to work with a web part on a page in a library, but by script was working with a site page.

PowerShell SharePoint Delete Web Part

Migrated a company from SharePoint 2007 to SharePoint 2010 and hit another small bump in the process.

The core issue was related to the AllItems.aspx page in a given library had a Content Editor web part on the page. Why?  No idea, but when users tried to access the library, the Documents and Library tab were missing from the Ribbon.

This wouldn’t be an issue if I was dealing one library.

In my case, I had a Site Collection with a dozen sites, then under each of those sites were 100+ sites.  So, I only needed to update ~1,000 sites.

Options:
A. Hire someone to edit ALL of those sites / pages.
B. Our great friend PowerShell!

What I’m doing in the script:
Starting at the Site Collection.
Loop on each site.
Looping on each List in the site.
When site = Random Documents dig in a little deeper.
Get the web parts on the AllItems.aspx page.
Loop on the web parts.
If web part title = “Content Editor Web Part”
Delete it.

 $site = Get-SPSite "http://sharepointed.com/SiteCollection"

foreach ($web in $site.AllWebs)
{
foreach($List in $web.Lists)
{
If($list.Title -eq "Random Documents")
{
$webpartmanager = $web.GetLimitedWebPartManager(($web.Url + "/Random%20Documents/Forms/AllItems.aspx"), [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

for($i=0; $i -lt $webpartmanager.WebParts.Count; $i++)
{
if($webpartmanager.WebParts[$i].Title -eq "Content Editor Web Part")
{
$webpartmanager.DeleteWebPart($webpartmanager.Webparts[$webpartmanager.WebParts[$i].ID])
}
}
}
}
$web.Update()
$web.Dispose()
}
$site.Dispose()

Update Default Value of a Date and Time field

Ohhh how handy can PowerShell be!

I needed to change the default value of a Date and Time field for several hundred sites. PowerShell allowed me to update the field on all of my SharePoint sites in a matter of seconds.

#add-pssnapin microsoft.sharepoint.powershell

$spsite=[Microsoft.SharePoint.SPSite]("http://sharepointed.com/customers/")

foreach ($web in $spsite.AllWebs) 

{
    Write-Host $web.name
    
    $List = "List or Library"
    $OpenList = $web.Lists[$List]

    $Field = $OpenList.Fields["My Date Field"]
	$Field.DefaultValue = "[today]"
    $Field.Update($True)
    
    $web.Dispose()

}
$spsite.Dispose()

Update Hyperlink Field in SharePoint with PowerShell

Recently was asked to update the hyperlink field in a SharePoint list. The list had thousands of links pointing to a server we were moving, so the URL in the list needed to updated to reflect the new location. The only options I could think of, were to open SQL Server and do find/update against the content database(s). Well, Microsoft doesn’t like up playing with the SharePoint databases. So I cracked open PowerShell and went to town trying to figure this out.

#Add-PSSnapin Microsoft.SharePoint.PowerShell

$siteUrl = "http://sharepoint/SiteCollection/SiteName"
$webName = “SiteName”
$listName = "Name of your list"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spWeb = $spSite.OpenWeb($webName)
$spList = $spWeb.Lists[$listName]

			foreach($Item in $spList.Items )
			{
				$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])

				$ofldurl.URL = $ofldurl.URL.Replace("Nacho", "Taco")
				$ofldurl.Description = $ofldurl.Description.Replace("Nacho", "Taco")

				$item["URL"] = $ofldurl
				$item.update()
			}

$spWeb.Dispose()

What I’m doing here is replacing part of the URL string with a new word. The script is looking for the string Nacho and replacing it with Taco.

**** update ****
After migrating from SharePoint / WSS 2007 to SharePoint 2010, we started to notice some of our URLs were messed up. Web parts ( Page Viewer) and internal hyperlinks were pointing to an incorrect location.

example:
http://sharepoint/crmsp/customers/sitename/_layouts/1033/mngsubwebs.aspx?view=sites

Notice the 1033, no bueno!

Using the script above, i was able to update the links.

#Add-PSSnapin Microsoft.SharePoint.PowerShell

$siteUrl = "http://sharepoint/SiteCollection/SiteName"
$webName = “SiteName”
$listName = "Name of your list"
$spSite = new-object Microsoft.SharePoint.SPSite($siteurl)
$spWeb = $spSite.OpenWeb($webName)
$spList = $spWeb.Lists[$listName]

			foreach($Item in $spList.Items )
			{
				$ofldurl= new-object Microsoft.SharePoint.SPFieldUrlValue($Item["URL"])

				$ofldurl.URL = $ofldurl.URL.Replace("1033/", "")
				$ofldurl.Description = $ofldurl.Description.Replace($ofldurl.Description, $ofldurl.Description)

				$item["URL"] = $ofldurl
				$item.update()
			}

$spWeb.Dispose()

In SharePoint 2016, the Description property appears broken?
This is the only way I could manage to set the URL and display text:

$newItem[“LinkField”] = "http://taco.com, taco2"

The page you selected contains a list that does not exist

Error_pagetitle
An error occurred while getting the items.
List does not exist.
The page you selected contains a list that does not exist. It may have been deleted by another user.
ErrorPageRequestGuid

This was one crazy mess!

First I tried digging around in SharePoint Designer with no luck. In Designer, when I clicked on Lists and Libraries, nothing would appear in the right window. Odd. When I clicked on All Files, all my items would appear. But, if I tried to right click on the masterpage folder I would receive and error. Then… I noticed when I tried to open the Documents library in my site it would say something like the webpage does not exist.

Onto the ULS Log Viewer.
Started up the Viewer, then made SharePoint toss that error again.
Looked in the Viewer and found that SharePoint error-ed out because of the Documents library on the site. No idea why this single library was the issue. I couldn’t open the library, so I tried to attack it with PowerShell.

 $site = new-object Microsoft.SharePoint.SPSite("http://sharepointed.com/")
 $web = $site.rootweb
 $list = $web.Lists["Documents"]
 $list.Delete
 #do a .Dispose()

Ran this code, then went back into SharePoint, and tried to create a new Document Library. Shaaazam, it worked!

It worked but my Documents library was still showing up in the _layouts/viewlsts.aspx view.

Still working on this issue.

*one note.
For some reason, if I tried to access the site from a PC without SilverLight, I was able to create new Libraries.