Friday, January 22, 2016

PowerShell: Activate Nintex Workflow For a Site Collection (Includes all Sub Sites)

Recently I was on a project which was using Nintex Workflow 2013 and we needed to have the site collection feature activated, as well as the Nintex Workflow 2013 site feature on all of the sub sites within this particular site collection.  Well this was an intranet site which was comprised of 50 sub sites which would all need to be enabled.

In order to use Nintex Workflow the site collection feature must be activated as well as the site feature for any sub site which wishes to use the Workflow Product.  As much fun as manually clicking through 50+ sites and activating the feature would be, I found a script online and tweaked it a bit to be a bit more robust.  Original script author Vadim Tabakman wrote the bulk of the script, my additions are noted in the script comments.



Best Regards,
Dan


#Orignal Script Author: Vadim Tabakman
#http://www.vadimtabakman.com/nintex-workflow-enable-the-nintex-workflow-feature-on-all-subsites.aspx

#Updates By Dan Adams
    #Added a param for re-usablilty rather than have a set path
    #Added the logic to enable the Site Collection Nintex Workflow Feature before enabling the sites
    #Wrapped the Enable-SPFeature calls to handle errors if the Feature was already activated on the site

param($url = $(Read-Host -prompt "Root Site Collection Path"))

#Get the PowerShell Snapin
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

#Get Web
$web = get-spweb $url
#Get Site
$rootSite = Get-SPSite $url

#Activate Site Collection Feature If Not Activated
$nintexWFCollectionFeature = "0561d315-d5db-4736-929e-26da142812c5"
(Enable-SPFeature -Identity $nintexWFCollectionFeature -ErrorAction SilentlyContinue -Url $url) -$null


#Loops Through the SubSites and Activates Nintex Workflow Web Feature
function EnableNintexWorkflowSiteFeature( $w )
{
    $ws = $w.Webs;
    foreach( $subweb in $ws)
    {
        EnableNintexWorkflowSiteFeature($subweb)
    }
    echo 'Enabling Nintex Workflow on site : ' $w.Url
    (Enable-SPFeature NintexWorkflowWeb -ErrorAction SilentlyContinue -url $w.Url) -ne $null
}

echo 'Enabling Nintex Workflow on site : ' + $web.Url
EnableNintexWorkflowSiteFeature $web

PowerShell: Loop Through All Sub Sites in a Site Collection

So this post will be extremely short, but hopefully useful.  The below script is a good starting point if you have a repetitive task that you need to complete on all of your sub sites within a particular site collection.  This script will loop through all of the sub sites, including sub sites of sub sites and print out the site's title and URL.  To use the script you simply need to run the script and input the site’s root URL without quotes.  So if my site collection was http://danssandbox.com/sites/dansSand , I would simply need to input that URL when prompted by the script for the “Root Site Collection Path.”  

This script is meant as a starting point to include actual logic rather than the Write-Hosts, but it should get you on the right track!  Enjoy the loops!


Dan


param($url = $(Read-Host -prompt "Root Site Collection Path"))

#Get the PowerShell Snapin
Add-PSSnapin "Microsoft.SharePoint.PowerShell"

#Get Root Site
$root = Get-SPSite $url

#If site was found / valid
if($root -ne $null)
{

     foreach($subSite in $root.AllWebs)
      {
       $subSiteTitle = $subSite.Title
                Write-Host $subSiteTitle -ForegroundColor Magenta
                $subSiteURL = $subSite.Url
                Write-Host $subSiteURL -ForegroundColor Cyan
       $subSite.Dispose()
      }

        $root.Dispose()

}

Thursday, November 19, 2015

SharePoint 2013 Result Source Only Include Content From Current Site NOT Sub Sites

Hello Blog Readers,

Sorry for the delay in posts recently things at work in terms of projects, but I did have an extremely helpful Result Source Syntax you should know!  From the title of this blog article you have probably guessed what I will discuss, how to actually only search on the current site!

I can’t believe how many copy and paste blogs are out there since everyone’s “solution” of Path:{Site} or Path:{Site.URL} does not work!  I was so frustrated after clicking through pages of Google links (dear god I made it to page 3… where results go to die) that I just sat down and tried to figure out a solution myself.
For those of you who don’t want the journey or explanation the key to your success is to include this as part of your query:
WebId={Site.ID}

Why is The Path Solution Incorrect?
The Path solution is incorrect since Path:{Site} will include any content from your sub sites which matches the criteria.  This is because the child sites includes the path of the parent site, which means you will always get the data from the sub sites which inherit from the parent’s Path.  You may be thinking well Dan, why don’t you just use Path={Site} instead of contains.  If you have tried this syntax on its own, you most likely only received the Home Page of the site from which you were running the query from.  Content within the site obviously does not have the URL of the homepage, so using equals will only return the one page which matches the path exactly.  In short, you can use Path:{Site} when you are trying to exclude contents from parent sites, but include content from the current site and it’s children.

How to Return Results ONLY From the Current Site:
To only obtain results from the site from which the query is being run, WebId={Site.ID} should be used.  WebId is an out of the box managed property which houses the site’s GUID  / ID, so essentially what we are saying in the above is that we want to only return items where the WebId is equal to the current site’s GUID/ID from which the query is being issued from.

Simple Example Query:
SPContentType=”Item” AND WebId={Site.ID}

This query will only retrieve items which match the item content type display name and where the WebId is equal to the current sites GUID / ID.  If you were to try this same query from a top level site utilizing Path:{Site} instead of WebId, like the other blogs and sites suggest, you will retrieve Items with this content type from all of the sub sites beneath this path.  The query above will ONLY return items from the current site J
Best of Luck!

Dan