Thursday, July 5, 2012

Using Powershell to mass update a SharePoint Hyperlink field


Scenario:
You have a SharePoint List that contains all of your site's subsites, one of the columns in the List is used to point to the URL of the subsite. Using a foreach loop and comparing values in the List to the name of the sub site you can update the hyperlink column like so:

#add-pssnapin microsoft.sharepoint.powershell
$web = get-spweb -identity "https://dashboard.company/.com"
# We have to use the @ sign specify that this will be an array
$subsites = @($web.webs)
foreach($subsite in $subsites)
       {
        # The hyperlink field has 2 columns the URL and the Description
        $itemUrl = $subsite.Url
        $itemDesc = $subsite.Title
        #We filter the list to make sure we do not get
        # any items named Temp
        if($itemDesc -ne "temp")
        {
        # Get the List we want to update
        $listWeb = Get-SPWeb -identity "https://dashboard.company.com/orders"
        $list = $listWeb.Lists["Your List Name"]
        # We have to use the @ sign specify that this will be an array
        $items = @($list.Items)
        foreach($item in $items)
        {
            $siteUrl = $item["Your Column Name"]
            $to = $item["Task Order"]
            # We want to match up each List item with the right Title of each site
            if($itemDesc -eq $to)
            {
            # Set the name of the hyperlink column in a variable
            $siteUrl = $item["Your Column Name"]
            # You have to enclose the variables in quotes for the vaules
            # to be passed
            $siteUrl = "$itemUrl, $itemDesc"
            $item["Your Column Name"] = $siteUrl
            $item.update()
            # Print out the sub site name to the console window
            # goes faster if you do not print it out           
            $siteUrl
            $listWeb.Dispose()
            }
          }
        }
    }
$web.Dispose()

Wednesday, May 30, 2012

I was recently setting up a new virtual test box for SharePoint to work on developing a new project and was trying to mimic my production network with sever and site collection names, however I was getting a prompt to enter my username and password but even entering my credentials I was still not able to access my site. My dev server name was SP2010-SP which is where my central admin site was as well as my first site collection, even this site was giving me issues with login, to make it more interesting I created 2 additional sites at dashboard.company.com and portal.company.com using host headers so I could use port 80. The solution that worked was to do the following:

Specify host names (Preferred method if NTLM authentication is desired)
To specify the host names that are mapped to the loopback address and can connect to Web sites on your computer, follow these steps:
  1. Set the DisableStrictNameCheckingregistry entry to 1. For more information about how to do this, click the following article number to view the article in the Microsoft Knowledge Base:
    281308  (http://support.microsoft.com/kb/281308/ ) Connecting to SMB share on a Windows 2000-based computer or a Windows Server 2003-based computer may not work with an alias name
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry key:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0
  4. Right-click MSV1_0, point to New, and then click Multi-String Value.
  5. Type BackConnectionHostNames, and then press ENTER.
  6. Right-click BackConnectionHostNames, and then click Modify.
  7. In the Value data box, type the host name or the host names for the sites that are on the local computer, and then click OK.
  8. Quit Registry Editor, and then restart the IISAdmin service.

Thursday, May 24, 2012

The holy grail for SharePoint developers just getting started

While recently looking for some code samples for creating a custom web part I came across this little gem from Microsoft, the SharePoint 2010 Code Samples 101 download located here: http://code.msdn.microsoft.com/SharePoint-2010-101-Code-da251182

This is a great collection of Visual Studio Solutions written in C# to get you started on your next custom project for SharePoint 2010.

Tuesday, May 22, 2012

Create a SharePoint sub site from a site export

Our issue was some 3rd party web parts were not allowing us to save a site as a template through the normal way of going into the Site Actions > Site Settings > Save Site as a template.

Our solution involved using Powershell to do an export of the site we wanted to use as a template. The main challenge with this is that you need to first create a SharePoint site, then do an Import-SPWeb to restore the site that was exported, then set the title and description. (This works with both http and https sites.)

# Define root site where the shell of the sub site will be created under
$site = "https://portal/"
   
Write-Host "Please enter the Title of the site to create"       
$webTitle = Read-Host "Please enter the title of the site you wish to create"    
$newSiteUrl = "https://portal/$webTitle"     
# Create empty subsite
$web = New-SPWeb $newSiteUrl -name $webTitle -UseParentTopNav
# Use the base SharePoint template of STS#1
$web.ApplyWebTemplate("STS#1")
# Give the site a name
$web.Name = $webTitle
# Apply changes
$web.Update()
# Since we are using a custom template for the sites we will want to set some variables
$templateName = "https://portal/$webTitle"
$templateLocation = "C:\Temp\Template\site.bak"
# Restore the site from the back up
Import-SPWeb $templateName -Path $templateLocation -IncludeUserSecurity
# We need to update some properties of the restored site, so we set the site as a variable
$web = (Get-SPWeb -Identity "https://portal/$webTitle")
# Once the site is restored this will overwrite the title and description which we set here
$web.title =$webTitle
$web.description = "Template Site"
# Update the site
$web.update()
# Clean it up
$web.dispose()
  
Write-Host "The Installation is complete please visit the site at" $web.URL

Monday, April 16, 2012

SharePoint 2010 Table of Contents web part using javascript

One of the things I love about SharePoint is the ability to quickly create solutions utilizing the html web part or the content editor web part, lately I have been using the html web part as it is just a bit friendlier to use when adding client side code to handle tasks.

Today I have been trying to find an easy way to list all the sub sites in order to create a Table of Contents for our users, and while there are several ways you can do this without having to use code the Tree view and the actual Table of Contents web parts just were not meeting my needs, so a little javascript magic to the rescue!

Add a html web part to your page and then click the edit web part
Click the source editor button in the properties dialog box on the right
and paste in this script:


This script will loop through your sub sites and display a list of sites, I took it one step further and made each item a link that will take you to the site listed.

Tuesday, April 3, 2012

Powershell error when creating a new SharePoint Site

I ran into another odd problem with my SharePoint 2010 deployment yesterday.  I was trying to create a new site using the New-SPWeb PowerShell command and I kept getting the following error message:

PS C:\Users\rsmw> new-spweb http://sharepoint.local/newspweb
New-SPWeb : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:10 + new-spweb <<<<  http://sp2010.mps.k12.mi.us/warrensm2
 + CategoryInfo          : InvalidData:
(Microsoft.Share....SPCmdletNewWeb:SPCmdletNewWeb) [New-SPWeb],
UnauthorizedAccessException
 + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletNewWeb
 
The account I was logged in with was a farm administrator and I could create sites through the web interface.   Apparently, I needed to add the farm administrator account to the Owners group of the parent site in order to create a new site. After that, all was good to go.

Sunday, April 1, 2012

Using PowerShell to set SharePoint 2010 MasterPage Recursively

During a recent development project to create a custom branded MasterPage for our 2010 portal, the page I had created had an EventReceiver that set the MasterPage to my custom MasterPage, however I forgot to enclose one of my vars with a semi colon and the page deployed but would not render, I kept getting a SharePoint error saying File not found!!! Even after retracting the solution with Visual Studio I kept getting the same error. I needed some way to reset the MasterPage to v4.master, enter PowerShell, my favorite way to work with admin task. Thanks to Todd Lindt's quick post found here:
http://www.toddklindt.com/blog/Lists/Posts/Post.aspx?ID=226

The master page setting is scoped at the web level, so the first thing I did was use Get-SPWeb to get a variable for the web I want to change. There are two properties that control the master page settings; MasterUrl and CustomMasterUrl. The former controls the master page used to render the System pages (the ones that start with /_layouts) and the latter controls the master page used to render the content pages.


To alter those settings for a publishing site at http://sharepoint/, use the following PowerShell script:

$web = Get-SPWeb http://sharepointurl/
$web.CustomMasterUrl = "/_catalogs/masterpage/customMasterPage.master"
$web.MasterUrl = "/_catalogs/masterpage/customMasterPage.master"
$web.Update()
This changes the master page for both settings and content to a customMasterPage.master. You can also set it to the default v4.master if you just need to reset it to the default masterpage. Since we're doing this with PowerShell it's easy to loop through a group of webs and set their master pages to whatever you like