Friday, January 30, 2015

Powershell to move files around in same web for O365 SharePoint online

Sometime we need to help users to move files around the same web for O365 SharePoint online.  We have implemented this using CSOM as below. Please note this will not work to move files across the web or site.

$listTitle = "SourceDocuments"
$sourceFolder = "/SourceDocuments"
$destFolder = "/DestinationDocuments"
$siteUrl = ""

# Login as the user account to window server and run the following script
# read-host -AsSecureString | ConvertFrom-SecureString | out-file C:\cred.txt
# The password will be encripted to file
$o365admin = ""
$password = get-content C:\cred.txt | convertto-securestring
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($o365admin,$password)

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$ctx.Credentials = $credentials

#Load items
$list = $ctx.Web.Lists.GetByTitle($listTitle)
$query = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()
$items = $list.GetItems($query)

foreach ($item in $items){

  if($item.FileSystemObjectType -eq [Microsoft.SharePoint.Client.FileSystemObjectType ]::File) {

     $destFileUrl = $item["FileRef"].ToString().Replace($sourceFolder,$destFolder)
     $item.File.MoveTo($destFileUrl, [Microsoft.SharePoint.Client.MoveOperations]::Overwrite)

I'm still working to find good solution to move files across site collection. I'm also working on to move the web from one site collection to another. Currently Microsoft API does not support this. Please share your thoughts if you have done this already.

Wednesday, January 28, 2015

Automation to monitor NextLabs policies deployed to all SharePoint servers

After we have deployed NextLabs SharePoint entitlement management solution, we have come up a automation process to update the SharePoint entitlement policy daily. However, we found sometimes the updated policies are not deployed to ALL SharePoint WFEs. As a result, some servers might have the old policies. We have worked with NextLabs to provide a API so we could very the deployed policies are the updated ones. However, there is no such API at this time. 

In this article, I will provide a workaround to check the policy file on ALL SharePoint WFEs. If any file not updated during last 12 hours, the process will send out email to notify administrator.

This is  just a quick workaround and you may modify for your own purpose.

# References and Snapins
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null) {
  Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#  Variables that you need to change

$policyPath = “C:\Program Files\NextLabs\Policy Controller\bundle.bin”
$timespan = new-timespan -days 0 -hours 12 -minutes 0

$emailFrom = ""   
$emailTo = ""
$subject="NextLabs policy check "  + [System.DateTime]::Now.ToLocalTime() 
$message = "NextLabs policy checking results "  + [System.DateTime]::Now.ToLocalTime() 
$policyoutofdate = $false

# Get all WFE servers
$appServers = get-spserver | ? { $_.Role -eq "Application" }
foreach($server in $appServers)
    if($server.Status -eq "Online")
        $ServiceName = “Microsoft SharePoint Foundation Web Application”
        $ServiceRef = Get-SPServiceInstance -server $server | where-object {$_.TypeName -eq $ServiceName}
        if($ServiceRef.Status -eq "Online") # This is WFE server that has NextLabs policies
            $currentHostName = gc env:computername;
            $serverName = $server.DisplayName

            if($server.DisplayName.ToLower() -ne $currentHostName)
                 $policyPath = "\\" + $server.DisplayName + "\" +  $policyPath.Replace(':','$');

             if(Test-Path  $policyPath)
                    # Check time
                    # Get-Item “C:\Program Files\NextLabs\Policy Controller\bundle.bin” | Format-List 

                    $lastWrite = (get-item $policyPath).LastWriteTime
                    if (((get-date) - $lastWrite) -gt $timespan) {
                        $policyoutofdate = $true
                        Write-Host "Policy might be out of date on server $serverName." -ForegroundColor Red
                        $message = $message + "`n" + "`n" + "`n" + "Policy might be out of date on server $serverName." + "`n"
                    } else {
                        # newer
                        Write-Host "Policy is fine on server $serverName." -ForegroundColor Green

                    #Write-Host "Checked the policy file"
                    $message = $message + "WARNING: The NextLabs policy file does not exists on server $server" + "`n"
                    Write-Host "WARNING: The NextLabs policy file does not exists on server $server" -foreground yellow;
            Write-Host -foregroundcolor Yellow "WARNING: Server $server is Offline. Skipping checking the NextLabs policy";

$message = $message + "`n"
$smtp=new-object Net.Mail.SmtpClient($smtpServer)

    $smtp.Send($emailFrom, $emailTo, $subject, $message)

Thursday, January 15, 2015

Ultimate procedure to display SharePoint online hybrid search results in SharePoint Server 2013

After you have configured the SharePoint online and DirSync to allow users to leverage the Office 365 features, you might want to implement the following two functions to streamline the integration from SharePoint on-premises to SharePoint online.

  1. Redirect on-premises OneDrive to SharePoint online OneDrive for business
  2. Configure one-way outbound hybrid search to display SharePoint online results in SharePoint Server 2013

TechNet has good procedure but is missing some key procedure. Manas from Microsoft escalation engineer team summarized the process in his blog that is helpful. Bill from Microsoft also provided step-by-step procedure. However, when we configure our one-way outbound hybrid search, we run into many issues and brought up some concerns on these instructions. Here are the summary of the concerns.

  • Uncompleted scripts from TechNet
  • Several discrepancies among all these instructions
  • Lacking verification procedure
  • Lacking automation scripts 

In this article, I’ll list the major articles with tips, verifications, automation scripts, and some debugging tips. You could follow each individual blog on the details.

  1. Redirecttargeted users to Office 365 with OneDrive for Business and MySite fromSharePoint On Premises
  2. Procedureand tips to set up audience targeting of OneDrive for Business in Office 365from SharePoint Server 2013 on-premises
  3. Procedureand tips to replace the STS certificate in on-premises environment forSharePoint Server 2013 hybrid solution
  4. Procedure,tips, and verification scripts to build a server-to server trust betweenSharePoint Server 2013 and SharePoint Online for one-way outbound hybrid search
  5. Displayhybrid search results and SharePoint online search only in SharePoint Server2013
  6. Automatehybrid search center configuration in SharePoint Server 2013
  7. Workaroundto make SharePoint online one-way outbound hybrid search to display onlineresult even SharePoint online is not using UPN as login
If you do not want to use Office 365 production tenant for your testing, you could use my article ActiveOffice 365 Developer Subscription using MSDN subscription benefit to set up a develop environment.

Now, you should have all the necessary details to configure and verify the one-way outbound hybrid search. The users could seamlessly navigate from SharePoint on-premises to SharePoint online and see online search result from on-premises.