Wednesday, May 25, 2016

When your MySite doesn't recognize you

So consider the following scenario:

Users log in to their MySites and are able to see their Profile and Newsfeed.  However, whenever they click on their Tasks link, they get redirected back to their Newsfeed.  It's almost as though SharePoint is treating them as a stranger to their own MySite.

We spun several hours on this problem, and I'm not sure our scenario fits for everyone.

The cause of our problem was we had migrated many thousands of MySites from one farm to a new farm.  We did this as part of a large scale farm upgrade.  When we did that, we did not backup and restore the User Profile databases.

Both farms were in the same domain, same usernames in Active Directory.  However, the rub came in the mismatch between the User Profile databases and the MySite content databases.

When a User Profile is imported from Active Directory, SharePoint assigns a GUID to that Profile and stores it in the User Profile database.  It then assigns that GUID to the MySite (in the MySites Property Bag) when the user first creates their MySite.

In our case, since the MySites were attached already, the creation action never fired.  And since the Profile GUID in the MySite Property Bag was different than the GUID in the Profile Database, when the user tried to click on their Tasks list, SharePoint didn't recognize them.

But there's a fix.  You can do this on a multiple site basis, or on an individual MySite basis.  The Powershell is:



$Web = Get-SPWeb http://[MySiteURL]/personal/[UserID]
$Web.SetProperty("urn:schemas-microsoft-com:sharepoint:portal:profile:userprofile_guid", "[NewGUID]")
$Web.Update()

 
You can get the NewGUID by querying UserProfileManager in PowerShell or by going to User Profile Service Application -> Manage User Profiles -> Find the user profile that is having issues -> Edit User Profile  

Then get the GUID out of the URL ProfAdminEdit.aspx?guid=[NewGUID] 

The credit for this one goes to Matt Coleman and Matt Royer - two of the guys on our team that dug pretty deep on this problem.  Hopefully it helps someone else out there.   

Thursday, May 12, 2016

Take ownership of SharePoint files with no checked in version, check them in, and publish with Powershell


This script will take ownership of all files in a library, then set whatever required metadata needs to be set, then check the items in.

Handy if a user has uploaded a ton of data using Explorer View, but never updated the metadata, and never actually checked in the files.  

if ((Get-PSSnapin | ? { $_.Name -eq "Microsoft.SharePoint.PowerShell" }) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

function BrowseItems($list, $Locationterm, $Departmentterm) {
    $files = $list.CheckedOutFiles
    foreach($file in $files)
    {
               $file.TakeOverCheckOut() #Take ownership of checked out document
    }


    foreach ($item in $list.Items) {
        $itemFile = $item.File

        if ($itemFile -ne $null) {
            if ($itemFile.CheckOutStatus -ne "None") {

                $LocationtaxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["GAC Location"]
                $DepartmenttaxField = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$item.Fields["GAC Department"]
                $fileName = $itemFile.Name
                $userName = $itemFile.CheckedOutByUser.Name;

                $item["Title"] = $fileName
                $LocationtaxField.SetFieldValue($item,$Locationterm)
                $DepartmenttaxField.SetFieldValue($item,$Departmentterm)
                $item.update()

               
                #Write-Host "Doing automatic CheckIn on the item" -f green -NoNewline
                $itemFile.CheckIn("Automatic CheckIn (Administrator)")
                #Write-Host " Done!" -f Yellow

                if( $list.EnableVersioning -and $list.EnableMinorVersions) {
                    #Write-Host "Doing automatic Publish on the item" -f green -NoNewline
                    $itemFile.Publish("Automatic Publish (Administrator)")
                    Write-Host "." -f Yellow
                }
            }
        }
    }
}

$url = "http://somesite.com/sites/teamsite/subweb"

$web = Get-SPWeb $url
$site = $web.Site

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Taxonomy")

$session = New-Object Microsoft.SharePoint.Taxonomy.TaxonomySession($site)
$termStore = $session.TermStores[0]

$Locationgroup = $termStore.Groups["Locations"]
$LocationtermSet = $Locationgroup.TermSets["Location"]
$Locationterms = $LocationtermSet.GetAllTerms()
$Locationterm = $Locationterms | ?{$_.Name -eq "SomeCity"}

$Departmentgroup = $termStore.Groups["Departments"]
$DepartmenttermSet = $Departmentgroup.TermSets["Department"]
$Departmentterms = $DepartmenttermSet.GetAllTerms()
$Departmentterm = $Departmentterms | ?{$_.Name -eq "SomeDepartment"}

$list = $web.lists["Some Document Library"]

BrowseItems $list $Locationterm $Departmentterm

Thursday, May 5, 2016

Rename a List or Library in SharePoint 2013 with Powershell

Don't save the List or Library as a template, then restore it.  Use Powershell instead!

add-pssnapin microsoft.sharepoint.powershell

$libOriginalUrl = "/Lists/OldListURL/";
$libNewUrl = "/Lists/NewListURL";
$web = Get-SPWeb -Identity http://somesite.com/sites/teamsite

$lib = $web.GetList($web.Url + $libOriginalUrl)
$rootFolder = $lib.RootFolder;
$rootFolder.MoveTo($web.Url + $libNewUrl)

Thursday, April 14, 2016

Powershell to set all pages in a subsite to a specific Page Layout

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue



#Variables for Web and Page URLs

$WebURL="http://somesite.company.com/sites/collection/subsite"

$OldPageLayoutName="OldPageLayout.aspx"

$NewPageLayoutName="NewPageLayout.aspx"



#Get the web and page

$Web = Get-SPWeb $WebURL



#Get Publishing Site and Web

$PublishingSite = New-Object Microsoft.SharePoint.Publishing.PublishingSite($Web.Site)

$PublishingWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($web)



#Get New Page Layout

$SitePageLayouts = $PublishingSite.GetPageLayouts($false)

$NewPageLayout = $SitePageLayouts | ? {$_.Name -eq $NewPageLayoutName}



#Get Pages Library

$PublishingPages = $PublishingWeb.GetPublishingPages()



#Iterate throgh each page

foreach ($Page in $PublishingPages)

{


        $Page.CheckOut()

        $Page.Layout = $NewPageLayout

        $Page.ListItem.Update();

        $Page.CheckIn("Page layout Updated via PowerShell")

    

        #$page.ListItem.File.Publish("")

        if ($Page.ListItem.ParentList.EnableModeration)

        {

            $Page.ListItem.File.Approve("Publishing Page Layout Updated!");

        }



     write-host "Updated Page layout on: "$Page.url

  
}

$Web.Dispose()

Monday, March 21, 2016

Powershell to remove all alerts for a user in a Web Application

This script will delete all alerts for a specific user on a Web App.  It's useful when a user is deleted, but the MySite cleanup job did not remove their collection level alerts.

Credit on this one to:  http://www.sharepointdiary.com/2011/11/managing-alerts-using-powershell.html - Salaudeen Rajack. 

##### Remove all alerts for specific user from a Web Application #####
$SPwebApp = Get-SPWebApplication "http://somesharepointsite.com"
$SpecificUser = "AD\someuser"
foreach ($SPsite in $SPwebApp.Sites)
{ # get the collection of webs
foreach($SPweb in $SPsite.AllWebs)
{ $alerts = $SPweb.Alerts
# if 1 or more alerts for a particular user, Make a note of them by copying their ID to an Array
if ($alerts.Count -gt 0)
{ $myalerts = @()
foreach ($alert in $alerts)
{ if ($alert.User -like $SpecificUser)
{ $myalerts += $alert
}
}
### now we have alerts for this site, we can delete them
foreach ($alertdel in $myalerts)
{ $alerts.Delete($alertdel.ID)
write-host $alertdel.ID
}
}
}
}

Friday, March 18, 2016

Powershell to approve all items in a SharePoint list


This script will set all items in the list to Approved.  Handy if you've bulk updated a bunch of items than need to get them published.

add-pssnapin microsoft.sharepoint.powershell


#$site = new-object Microsoft.SharePoint.SPSite("http://somesite.com/news")

$web = get-spweb "http://somesite.com/news/2016/"
$list = $web.Lists["Pages to be Changed"]
$items = $list.Items
foreach ($item in $items)

{

$item["_ModerationStatus"] = 0
$item.Update()

}

Remove WWW prefix from SharePoint URLs

It's not every day you are tasked with doing something, with Microsoft in the room, and the Microsoft rep goes "Wow, we should totally put that on TechNet."

This is one of those things.

If you want to remove the www prefix from a url for all SharePoint sites, here's how:

1.  Download and install the URL Rewrite Module on all web front ends.  Get it here:  http://www.iis.net/downloads/microsoft/url-rewrite

2.  Configure Alternate Access Mappings in SharePoint to allow the www url.  We added an Internal URL for our WWW entry, and mapped it to the public url.  Our AAM settings looked like this when we were done:



3.  Add a binding in IIS for the additional WWW url.  Our bindings looked like this:



4.  Configure the URL Rewrite Module for the new address.  Do that by:
- Adding a Blank Rule
- Enter the pattern http://www.somesite.com/*
- Choose Rewrite for the Action Type
- Enter the pattern http://somesite.com/*
- Save it.

Why this works:

SharePoint needs an Alternate Access Mapping for the www url.  Once that's in place, IIS needs a binding to recognize the request from SharePoint.  Once THAT is entered, the rewrite module will see the www request come in, pick it up, and reroute it. 

The users will see the site automatically replace the URL to http://somesite.com.