Thursday, August 24, 2017

Create User Profile Service Application using PowerShell in SharePoint 2016

We need User Profile Service Application to consume social features in SharePoint 2016 such as My Sites, User profiles, etc. This PowerShell script creates User Profile Service Application in SharePoint 2016.
Prerequisite: Create my site host prior creating user profile service application.

PowerShell to create user profile service application in SharePoint 2016
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
  
#Configuration Parameters
$ServiceAppName = "User Profile Service Application"
$ServiceAppProxyName = "User Profile Service Application Proxy"
$AppPoolAccount = "Crescent\SP16_Pool"
$AppPoolName = "Service Application App Pool"
$DatabaseServer ="SP16-SQL"
$UserProfileDB = "Farm_Profile_DB"
$UserProfileSyncDB = "Farm_Profile_Sync_DB"
$UserProfileSocialDB = "Farm_Profile_Social_DB"
$MySiteHostLocation="http://mysite.crescent.com/"

Try {
    #Set the Error Action
    $ErrorActionPreference = "Stop"
   
    #Check if Managed account is registered already
    Write-Host -ForegroundColor Yellow "Checking if the Managed Accounts already exists"
    $AppPoolAccount = Get-SPManagedAccount -Identity $AppPoolAccount -ErrorAction SilentlyContinue
    if($AppPoolAccount -eq $null)
    {
        Write-Host "Please Enter the password for the Service Account..."
        $AppPoolCredentials = Get-Credential $AppPoolAccount
        $AppPoolAccount = New-SPManagedAccount -Credential $AppPoolCredentials
    }
   
    #Check if the application pool exists already
    Write-Host -ForegroundColor Yellow "Checking if the Application Pool already exists"
    $AppPool = Get-SPServiceApplicationPool -Identity $AppPoolName -ErrorAction SilentlyContinue
    if ($AppPool -eq $null)
    {
        Write-Host -ForegroundColor Green "Creating Application Pool..."
        $AppPool = New-SPServiceApplicationPool -Name $AppPoolName -Account $AppPoolAccount
    }
   
    #Check if the Service application exists already
    Write-Host -ForegroundColor Yellow "Checking if User Profile Service Application exists already"
    $ServiceApplication = Get-SPServiceApplication -Name $ServiceAppName -ErrorAction SilentlyContinue
    if ($ServiceApplication -eq $null)
    {
        Write-Host -ForegroundColor Green "Creating User Profile Service  Application..."
        $ServiceApplication =  New-SPProfileServiceApplication -Name $ServiceAppName -ApplicationPool $AppPoolName -ProfileDBName $UserProfileDB -ProfileSyncDBName $UserProfileSyncDB -SocialDBName $UserProfileSocialDB -MySiteHostLocation $MySiteHostLocation
    }
    #Check if the Service application Proxy exists already
    $ServiceAppProxy = Get-SPServiceApplicationProxy | where { $_.Name -eq $ServiceAppProxyName}
    if ($ServiceAppProxy -eq $null)
    {
        #Create Proxy
        $ServiceApplicationProxy = New-SPProfileServiceApplicationProxy -Name $ServiceAppProxyName -ServiceApplication $ServiceApplication -DefaultProxyGroup        
    }
    #Start service instance
    $ServiceInstance  = Get-SPServiceInstance | Where-Object { $_.TypeName -eq "User Profile Service" }
   
    #Check the Service status
    if ($ServiceInstance.Status -ne "Online")
    {
        Write-Host -ForegroundColor Yellow "Starting the User Profile Service Instance..."
        Start-SPServiceInstance $ServiceInstance 
    }
   
    Write-Host -ForegroundColor Green "User Profile Service Application created successfully!"
}
catch {
    Write-Host $_.Exception.Message -ForegroundColor Red
 }
 finally {
    #Reset the Error Action to Default
    $ErrorActionPreference = "Continue"
 } 
Once the script execution completed successfully, you can go to Central Administration >> Application Management >> Service Application Page to verify user profile service application is created.
sharepoint 2016 create user profile service application powershell


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Wednesday, June 7, 2017

SharePoint Online: Site Users and Groups Report using PowerShell

Requirement: Generate a report with all site groups and members of each group of a SharePoint online site collection.

Get all Site Groups and Members of Each group:
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminName = "spadmin@crescent.com"
$SiteURL="https://crescent.sharepoint.com/sites/sales"
 
#User Names Password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Get all Site groups
$SiteGroups = Get-SPOSiteGroup -Site $SiteURL

#Get Members of each group
foreach($Group in $SiteGroups)
{
    Write-host "Group:"$Group.Title
    Get-SPOSiteGroup -Site $SiteURL -Group $Group.Title | Select-Object -ExpandProperty Users
} 
To get all users of the site collection, use:
Get-SPOUser -Site $siteURL | select DisplayName, LoginName | Where { $_.LoginName -like "*@crescent.com"}

Get Users and Groups for all SharePoint Online Site Collections:
#Variables for processing
$AdminURL = "https://Crescent-admin.sharepoint.com/"
$AdminName = "SPAdmin@Crescent.com"
 
#User Name & Password to connect
$Password = Read-host -assecurestring "Enter Password for $AdminName" 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminName, $Password

Try {
#Connect to SharePoint Online
Connect-SPOService -url $AdminURL -credential $Credential

#Get all Site collections
Get-SPOSite -Limit ALL | ForEach-Object {
Write-Host "Site Collection:"$_.URL

#Get all Site Groups 
$SiteGroups = Get-SPOSiteGroup -Site $_.URL
   
    #Get Members of each group
    foreach($Group in $SiteGroups)
    {
        Write-host "Group:"$Group.Title
        Get-SPOSiteGroup -Site $_.URL -Group $Group.Title | Select-Object -ExpandProperty Users
    }
  }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Monday, June 5, 2017

SharePoint Online: How to Change List to New Experience?

SharePoint Online's new list experience provides faster and easier user interface to lists and libraries. You can switch between classic & new experiences in SharePoint online by simply changing list settings.

To change list or library to new modern user interface:
  • Go to List or Library settings >> Click on "Advanced Settings"
  • Scroll down to the bottom, and from "List experience" section, Select "New experience" option and hit OK.
This changes the list UI to new experience in SharePoint online, From:
To: New list experience in SharePoint online


How about setting the default option for all New Lists?
To set list experience for new lists, you can specify this option globally using using SharePoint Admin Center. Here is how:
  • Navigate to your SharePoint Admin Center(typically: https://YOURCOMPANY-admin.sharepoint.com/)
  • Click on "Settings" from the Left navigation
  • On the Setting page, under "SharePoint list and libraries experience" section, Select the appropriate option, such as : Classic experience or New experience (auto detect).

PowerShell script to Switch UI experience of a List or Library in SharePoint Online:
On any existing SharePoint online lists and libraries, you can switch the UI experience either using SharePoint web UI method as explained above or with below PowerShell script. 

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
  
##Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/sales"
$ListName= "Project Documents"
$UserName="salaudeen@crescent.com"
$Password ="Password goes here"
 
#Setup Credentials to connect
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))

Try { 
    #Set up the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl) 
    $Context.Credentials = $credentials

    #Get the document library
    $List = $Context.web.Lists.GetByTitle($ListName)
    #Set list properties
    $List.ListExperienceOptions = "NewExperience" #ClassicExperience or Auto
    $List.Update()

    $context.ExecuteQuery()

    Write-host "UI experience updated for the list!" -ForegroundColor Green        
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

SharePoint online new experience not working?
In some cases, even after you changed the list experience settings from list settings, you will be still getting the same UI. This is because of the browser cookie! To fix the problem, simple clear your browse cookie and reload the page. (To be specific, the cookie is called "spInu" with a value of 0!)

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Sunday, April 16, 2017

Style SharePoint 2016 List View Web Part with Custom CSS

Requirement: Add a custom style to SharePoint 2016's standard list view web part to make it look like a dashboard!

By default, SharePoint list view web part looks dull, isn't it?
sharepoint 2013 list view web part css
So, lets add some custom CSS to the SharePoint 2016 list view, to make it look like a dashboard! How to add CSS to listview? Simple,
  • Click on Site Settings gear >> Edit Page
  • Insert web part >> Choose "Script Editor" under "Media and Content"
  • Place the below CSS code in it. Save and close!
CSS to Brand SharePoint List view:
<style type="text/css">

/* List View Header */
.ms-listviewtable > thead > tr > th {
 background-color: #5B9BD5;
}

/* List view Header Text color*/
.ms-vh-div,  .ms-headerSortTitleLink {
color:white!important; font-weight: bold;
} 

/* background color for alternate rows */
.ms-listviewtable > tbody > tr.ms-alternating {
background: #DDEBF7
}
</style>

Here is the output of the branded List view web part:
sharepoint 2013 add css to listview
Same CSS works for SharePoint 2013 as well.

You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Thursday, March 30, 2017

Find and Replace User ID in SharePoint InfoPath Forms using PowerShell

Requirement: Replace User IDs in InfoPath Forms.

Little background: A particular user's User account changed in Active directory and in SharePoint we ran Move-SPUser cmdlet to accommodate the user's SAM account name change. However, There are many InfoPath form libraries with bunch of InfoPath forms in it - with old user id. Of course, Move-SPUser has no effect on InfoPath Forms!

PowerShell script to search and replace user id in InfoPath XML Forms:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration Parameters
$SiteCollURL="http://intranet.crescent.com"

#Old and New User IDs - CASE SENSITIVE!
$OldUserID="<pc:AccountId>i:0#.w|Crescent\JohnA</pc:AccountId>"
$NewUserID="<pc:AccountId>i:0#.w|Crescent\JonhAbraham</pc:AccountId>"

#Get all webs in the site collection
$WebsColl = Get-SPSite $SiteCollURL | Get-SPWeb -Limit All

#Iterate through each web
foreach($web in $WebsColl)
{
    Write-host "Processing web:"$web.Url
    
    #Get all InfoPath Form Libraries in the web
    $InfoPathLibs = $web.lists | where { $_.BaseType -eq "DocumentLibrary" -and $_.BaseTemplate -eq "XMLForm"} 

    #Loop through each InfoPath form library and Forms (.xml)
    Foreach($Library in $InfoPathLibs)
    {
        Foreach($Item in $Library.Items)
        {
            # Load the contents of the InfoPath Form
            $File = $Item.File
            $Data = [System.Text.Encoding]::ASCII.GetString($File.OpenBinary())
            
            #Check if the File has Old ID
            if($data.Contains($OldUserID))
            {
                $Data = $Data.Replace($OldUserID, $NewUserID)
                $Data = $Data.Replace("???","") #special fix!
                $File.SaveBinary([System.Text.Encoding]::ASCII.GetBytes($Data))

                Write-host "InfoPath XML File updated at $($web.URL)/$($Item.File.URL)"
            }
        }
    }
}


You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


You might also like:

Related Posts Plugin for WordPress, Blogger...