Quantcast
Channel: SharePoint Diary
Viewing all 1058 articles
Browse latest View live

SharePoint Online: Copy Attachments from One List to Another using PowerShell

$
0
0
Requirement: Copy Attachment from one list to another in SharePoint Online.

 PowerShell to Copy Attachment to Another List in SharePoint Online:
Had a requirement to copy attachments between SharePoint Online lists. (only attachment - Not list items!). This script gets the attachments from source list, searches the matching list item based on "Mapping Column" value and attaches list attachments to the destination list items.
#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"

Function Copy-Attachments()
{
param
(
[Parameter(Mandatory=$true)] [string] $SiteURL,
[Parameter(Mandatory=$true)] [string] $SourceListName,
[Parameter(Mandatory=$true)] [string] $TargetListName,
[Parameter(Mandatory=$false)] [string] $MappingColumn="Title"
)
Try {
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get the Source List & Target Lists
$SourceList = $Ctx.Web.Lists.GetByTitle($SourceListName)
$TargetList = $Ctx.Web.Lists.GetByTitle($TargetListName)

#Get All Items
$SourceListItems = $SourceList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$TargetListItems = $TargetList.GetItems([Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery())
$Ctx.Load($SourceListItems)
$Ctx.Load($TargetListItems)
$Ctx.ExecuteQuery()

#Iterate through each list item from source
ForEach($SourceItem in $SourceListItems)
{
#Get All attachments from the List Item
$AttachmentsColl = $SourceItem.AttachmentFiles
$Ctx.Load($AttachmentsColl)
$Ctx.ExecuteQuery()

#Get Matching List item in the target list
$ListItem = $TargetListItems | Where { $_[$MappingColumn] -eq $SourceItem[$MappingColumn]}
if($ListItem -ne $null)
{
#Get attachment for each list item
ForEach($Attachment in $AttachmentsColl)
{
$AttachmentCreation = New-Object Microsoft.SharePoint.Client.AttachmentCreationInformation

#Get the Source attachment
$FileContent = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($Ctx, $Attachment.ServerRelativeUrl)
$Buffer = New-Object byte[]($FileContent.length)
$BytesRead = $FileContent.stream.Read($Buffer, 0, $Buffer.Length)
$ContentStream = New-Object -TypeName System.IO.MemoryStream ($Buffer)

$AttachmentCreation.ContentStream = $ContentStream
$AttachmentCreation.FileName = $Attachment.FileName
[void]$ListItem.AttachmentFiles.Add($AttachmentCreation)
$Ctx.ExecuteQuery()
}
}
}

write-host -f Green "List Attachments Copied from '$SourceListName' to '$TargetListName' !"
}
Catch {
write-host -f Red "Error Copying List Attachments!" $_.Exception.Message
}
}

#Set Parameters
$SiteURL= "https://crescent.sharepoint.com/"
$SourceListName="Projects"
$TargetListName="Project Temp"

#Call the function to copy list items
Copy-Attachments -siteURL $SiteURL -SourceListName $SourceListName -TargetListName $TargetListName

SharePoint Online: Create Announcement List using PowerShell

$
0
0
Requirement: Create Announcement List in SharePoint Online.

How to Create Announcement list in SharePoint Online?
Here is how to create announcement list in SharePoint Online:
  • Navigate to the SharePoint Online Site >> Click on Settings Gear >> Select "Add an App" 
    sharepoint online announcement list
  • From the Apps page, Click on "Announcements" Tile
    sharepoint online powershell to create announcement list
  • Provide the name to your announcement list and click on Create.
    create announcement list in sharepoint online
 This creates announcement list in SharePoint Online.

SharePoint Online: PowerShell to Create Announcement List
Here is the PowerShell CSOM script to create announcement list in SharePoint Online.

#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"

#Set Parameter Values
$SiteURL="https://crescent.sharepoint.com"
$ListName="Townhall"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Set creating data for announcement list
$CreationInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$CreationInfo.Title = $ListName
$CreationInfo.TemplateType = [int][Microsoft.SharePoint.Client.ListTemplateType]::Announcements
$CreationInfo.Description = "$ListName Announcements"

#Create Announcement in SharePoint Online
$List = $Ctx.web.Lists.Add($CreationInfo)
$Ctx.ExecuteQuery()

SharePoint Online: Get All Users using PowerShell

$
0
0
While my another post addresses How to get all users and groups in SharePoint Online, this post aims to get all users of the SharePoint site collection.

SharePoint Online: PowerShell Get All Users of the Site Collection
#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"

#Site collection URL
$SiteURL="https://crescent.sharepoint.com"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

#Initialize the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Credentials

#Get all users of the site collection
$Users = $ctx.Web.SiteUsers
$ctx.Load($Users)
$ctx.ExecuteQuery()

#Get User name and Email
$Users | ForEach-Object { Write-Host "$($_.Title) - $($_.Email)"}

PowerShell to Get All Users from All Site Collections in SharePoint Online:
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

$AdminSiteURL="https://crescent-admin.sharepoint.com/"

#Connect to SharePoint Online Admin
Write-host "Connecting to Admin Center..." -f Yellow
Connect-SPOService -url $AdminSiteURL -Credential (Get-Credential)

Write-host "Getting All Site collections..." -f Yellow
#Get each site collection and users
$Sites = Get-SPOSite -Limit ALL

Foreach($Site in $Sites)
{
Write-host "Getting Users from Site collection:"$Site.Url -f Yellow
Get-SPOUser -Limit ALL -Site $Site.Url | Select DisplayName, LoginName
}

Tags: sharepoint online csom get all users, sharepoint online get all users, sharepoint online powershell get all users, sharepoint online user reports, sharepoint online users list

SharePoint Online: Remove Site Collection Administrator using PowerShell

$
0
0
Requirement: Remove Site collection Administrator using PowerShell in SharePoint Online.

How to remove site collection administrator(s) in SharePoint Online?
Site collection administrators in SharePoint Online have full control for the entire site collection, including the root site and all of the sub-sites, lists and libraries in the site collection. To remove a site collection admin, follow these steps:
  • Login to SharePoint Online site as a SharePoint Online Administrator or Site collection Administrator
  • Click on Settings gear and then Site Settings
  • On the site settings page, click on "Site Collection Administrators" link
    SharePoint Online PowerShell to Remove Site Collection Administrator
  • Remove unwanted users from the site collection administrators and click on "OK" to save your changes.

SharePoint Online: PowerShell to Remove Site Collection Administrator
To remove a user from site collection administrators rights for a particular SharePoint Online site collection, use this PowerShell script in SharePoint Online Management Shell:
#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminAccount="salaudeen@crescent.com"

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

#Get the Site Collection
$Site = Get-SPOSite "https://crescent.sharepoint.com/sites/marketing"

#Remove user from site collection admin
Set-SPOUser -site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $False

Remove Site Collection Admin from All Site Collections:
Lets scan all site collections and remove a particular user from site collection admin.
Import-Module Microsoft.Online.Sharepoint.PowerShell -DisableNameChecking

#Variables for processing
$AdminURL = "https://crescent-admin.sharepoint.com/"
$AdminAccount="salaudeen@crescent.com"

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

#Get All Site Collections
$Sites = Get-SPOSite -Limit ALL

#Loop through each site and add site collection admin
Foreach ($Site in $Sites)
{
Write-host "Scanning site:"$Site.Url -f Yellow
#Get All Site Collection Administrators
$Admins = Get-SPOUser -Site $site.Url | Where {$_.IsSiteAdmin -eq $true}

#Iterate through each admin
Foreach($Admin in $Admins)
{
#Check if the Admin Name matches
If($Admin.LoginName -eq $AdminAccount)
{
#Remove Site collection Administrator
Write-host "Removing Site Collection Admin from:"$Site.URL -f Green
Set-SPOUser -site $Site -LoginName $AdminAccount -IsSiteCollectionAdmin $False
}
}
}

Site Collection Administrator in SharePoint Online Modern Team sites and Groups
In SharePoint Online Modern team sites & Groups Sites, "Site collection Administrators" link in hidden from UI, However you can access it by URL: https://tenant.sharepoint.com/sites/sitename/_layouts/15/mngsiteadmin.aspx and remove site collection admin or use PowerShell as given above!

SharePoint Online: Change Group Permissions using PowerShell

$
0
0
Requirement: SharePoint Online Change Group Permissions

How to Update permissions for a SharePoint group?
To edit group permissions in SharePoint Online, following these steps:
  • Login to your SharePoint Online site as a administrator >> On the site collection Home page, click on Settings icon >> Click Site settings.
  • On the Site Settings page, under Users and Permissions, click on Site permissions.
  • Select the check box of the group to which you want to change permissions (either to grant additional rights or to revoke existing permissions).
  • In the Modify section of the ribbon, click on "Edit User Permissions" button.
    sharepoint online edit group permissions
  • On the Edit Permissions page, select/deselect the group permission check boxes according to your requirement. You can simply tick a checkbox next to permission levels such as "Contribute" to grant permission or uncheck to remove permission from the group.
    SharePoint Online Change Group Permissions using powershell
  • Click OK to save permission changes to the group.

Now, Lets edit group permissions using PowerShell.

PowerShell to change group permissions in SharePoint Online:
For the members group of the site, lets remove "Edit" permissions and add "Contribute"
#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/marketing"
$GroupName="Marketing Team Site Members"
$PermissionToRemove="Edit"
$PermissionToAdd="Contribute"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get all groups of the site
$Groups = $Ctx.Web.SiteGroups
$Ctx.load($Groups)
$Ctx.ExecuteQuery()

#Get Group Names
$GroupNames = $Groups | Select -ExpandProperty Title

#Check if the given group exists
If($GroupNames -contains $GroupName)
{
#Get the Group
$Group = $ctx.Web.SiteGroups.GetByName($GroupName)

#Get Permission Levels to add and remove
$RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
$RoleDefToRemove = $Ctx.web.RoleDefinitions.GetByName($PermissionToRemove)

#Get the Group's role assignment on the web
$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($Group)

#Add/remove permission levels to the role assignment
$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
$RoleAssignment.RoleDefinitionBindings.Remove($RoleDefToRemove)
$RoleAssignment.Update()
$Ctx.ExecuteQuery()

write-host -f Green "User Group permissions updated Successfully!"
}
else
{
Write-host -f Yellow "Group Doesn't exist!"
}
}
Catch {
write-host -f Red "Error Changing Group Permissions!" $_.Exception.Message
}

SharePoint Online: Change User Permissions using PowerShell

$
0
0
Requirement:  Edit user permissions in SharePoint Online

How to change user permissions  in SharePoint Online?
You want to change user's permission on a particular site/library/item in SharePoint online. E.g. Once a particular project has completed and you no longer want the team member to add or edit the project's supporting documents but only view! SharePoint provides a flexible way to manage permission changes to a user or a group. Here is how to update user permissions in SharePoint online.
  • To edit site permissions for a user, Navigate to the SharePoint Online site where the user has access. Click on Settings gear and then site settings.
  • On the Site Settings page, click on "Site Permissions" link under Users and Permissions group.
  • On the site permissions page, Select the check box next to the user which you want to edit permissions. Click on "Edit User Permissions" button from the ribbon.
    edit user permissions sharepoint online
  • Select-Unselect the relevant permissions check boxes. In this case, you have to untick "Edit" and tick "Read". Click OK to save your changes.
    sharepoint online change user permissions
SharePoint online permissions will default to the highest level of security. E.g. If a user has both "Edit" and "Read" access, SharePoint considers "Edit"!

Please note, if the site or library is inheriting permissions from the parent, You may have to Stop Inheriting Permissions in order to provide unique permission to the item.

Edit user permissions in sharepoint online using PowerShell:
Lets script the above task of editing the user permissions. Remove "Edit" and add "Read" permission to the user at site level.
#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/marketing"
$UserAccount="i:0#.f|membership|Salaudeen@crescent.com"
$PermissionToRemove="Edit"
$PermissionToAdd="Read"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
#Setup the context
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Ctx.Credentials = $Cred

#Get all Users of the site
$Users = $Ctx.Web.SiteUsers
$Ctx.Load($Users)
$Ctx.ExecuteQuery()

#Get user accounts
$UserAccounts = $Users | Select -ExpandProperty LoginName

#Check if the given user exists in the site
If($UserAccounts -Contains $UserAccount)
{
#Get the User
$User = $ctx.Web.SiteUsers.GetByLoginName($UserAccount)

#Get Permission Levels to add and remove
$RoleDefToAdd = $Ctx.web.RoleDefinitions.GetByName($PermissionToAdd)
$RoleDefToRemove = $Ctx.web.RoleDefinitions.GetByName($PermissionToRemove)

#Get the User's role assignment on the web
$RoleAssignment = $Ctx.web.RoleAssignments.GetByPrincipal($User)

#Add/remove permission levels to the role assignment
$RoleAssignment.RoleDefinitionBindings.Add($RoleDefToAdd)
$RoleAssignment.RoleDefinitionBindings.Remove($RoleDefToRemove)
$RoleAssignment.Update()
$Ctx.ExecuteQuery()

write-host -f Green "User permissions updated Successfully!"
}
else
{
Write-host -f Yellow "User Doesn't exist in the site!"
}
}
Catch {
write-host -f Red "Error Updating User Permissions!" $_.Exception.Message
}

Compare SharePoint List Item Version History using PowerShell

$
0
0
Requirement: Compare version history data of SharePoint List items and update a metadata column based on a particular field's value change.

Bit background: We've a project tracking list with "Status" column. We wanted to have a insight on when a particular item's status was set to "Rejected".

PowerShell to Compare Version History Data and update a Column value:
As far solution to the above requirement, lets compare list item versions to capture when a particular item's "status" column was changed to "rejected", and get the created date of the specific version to update "Rejected Date" column of the list item. Lets compare version history in SharePoint using PowerShell:

Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction SilentlyContinue

#Parameters
$SiteURL="http://intranet.crescent.com"
$ListName = "Projects"

#Get web and List
$Web = Get-SPWeb $SiteURL
$List = $web.Lists.TryGetList($ListName)

#Get all list items
$ListItems = $List.Items

#Iterate through each list item
Foreach($Item in $ListItems)
{
#Iterate through each version
ForEach($Version in $Item.Versions)
{
#Check if the status column is "Rejected"
If($($version['Status']) -eq "Rejected")
{
#Update Rejected Date value of the item from version
$Item["RejectedDate"] = $($version.Created)
$Item.SystemUpdate()
Write-host "ID:$($item.id), Version:$($version.VersionLabel), CreatedBy:-$($version.CreatedBy.User.DisplayName), Status:-$($version['Status'])"
Break
}
}
}
This script compares each version of the list item and updates "RejectedDate" column value.

Fix "Get-SPOSite: The Site 'https://tenant.sharepoint.com/' is not properly formed" Error in SharePoint Online

$
0
0
Problem: When trying to connect to SharePoint Online site collection, got this error message
"Get-SPOSite The Site Tenant-URL is not properly formed."
Get-SPOSite: The Site "https://tenant.sharepoint.com/" is not properly formed

Solution:
Solution is simple! Just remove the trailing "/" from the URL!
E.g. In my case, instead of "https://crescent.sharepoint.com/sites/marketing/", I had to removing the backslash "/ "after marketing.

So the right format of my site collection is: "https://crescent.sharepoint.com/sites/marketing"

Set Global Search Center in SharePoint 2016

$
0
0
So, you have your search center site ready for SharePoint 2013 (Use this article: Create Enterprise Search Center using PowerShell) as a next step, you should configure global search center from SharePoint central admin site to set default search center site for all site collections.

SharePoint 2016 default search center
Once the search center site collection is created, navigate:
  • Central Administration >> Application Management >> Service Applications
  • Click on the Search Service Application >> In Search Administration page, Click on "Set a Search Center URL" link
    sharepoint 2013 change global search center url
  • Enter the URL of your Enterprise Search Center Site collection that you created and add "/pages" at the end. In my case, its "http://searchcenter.crescent.com/pages"
    sharepoint 2013 default search center
By this, we've instructed the SharePoint Search Service Application to use this site to submit search queries and view search results.

Change Global Search Center URL with PowerShell
You can also change the search center URL by using PowerShell
#Get the Search Service Application
$SSA = Get-SPEnterpriseSearchServiceApplication

#Set global search center
$SSA.SearchCenterUrl = "http://Search-Center-URL/pages"

$SSA.Update()
Here the "Search-Center-URL" is the URL to your Site Collection created with Enterprise Search Center template.

How to Delete a Host Named Site Collection in SharePoint using PowerShell?

$
0
0
Requirement: Delete host named site collection using PowerShell in SharePoint. How to Delete host named Site Collection? Generally, Host named site collections in SharePoint are managed through...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Remove SharePoint Web Application Policy using PowerShell

$
0
0
Requirement: Remove Web Application Policy using PowerShell How to remove a web application Policy in SharePoint? To remove a user or group from SharePoint web application Policy, follow these...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Enable Blank Site Template in SharePoint 2016

$
0
0
Problem: Blank site template is not available in SharePoint 2013 and SharePoint 2016. If you try to create a site collection from SharePoint Central Administration site or try to create a subsite,...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Find All OneDrive Site Collections in SharePoint Online using PowerShell

$
0
0
Requirement: Get a list of all OneDrive site collections from SharePoint Online. I was asked to get the list of all OneDrive sites to get the usage of OneDrive for Business in our Office 365 tenant....

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Get-Set OneDrive Site Collection Storage Quota Size using PowerShell

$
0
0
Get OneDrive Site collection Storage Quota Information: To get the storage quota data for a particular OneDrive site, use this PowerShell cmdlet in SharePoint Online Management Shell Get-SPOSite...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

How to Gain Owner Access to OneDrive for Business Sites in Office 365?

$
0
0
By default, When SharePoint My Site or OneDrive site collection is created by the user, SharePoint assigns primary site collection administrator rights to the user. In some cases you might need to...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

There was a problem deleting Web site "Site-URL". Sites that have subsites or certain apps can't be deleted. Please try again after deleting all subsites and removing the apps.

$
0
0
Problem: If you try to delete a SharePoint Online site which has subsites in it, SharePoint throws error message saying "There was a problem deleting Web site "<subsite-url>". Sites that have...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

How to Set Secondary Admin for All OneDrive for Business Sites?

$
0
0
When SharePoint My Site or OneDrive for business site collection are created by the user, SharePoint assigns primary site collection administrator rights to the user. At times, you may have to gain ...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

How to Delete OneDrive for Business Site Collection in SharePoint Online?

$
0
0
Requirement: Delete OneDrive for Business Site Collection! After a business decision made to disable OneDrive in organization wide, We configured the settings to disable OneDrive. However,  All...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

SharePoint Online: Site Collections Storage Size - Quota Usage Report using PowerShell

$
0
0
Requirement: Need to generate a report to analyze all SharePoint Online Site collection storage consumption.   How to get the Storage Metrics of a SharePoint Online Site Collection? To find...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]

Limit Search Results to a Specific Content Source in SharePoint 2016

$
0
0
Requirement:  HR team wants to setup a dedicated search portal to search a confidential file share. If its SharePoint 2010, we would have created a search scope and added rules to include only...

[[ This is a content summary only. Visit my website for full links, other content, and more! ]]
Viewing all 1058 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>