Tuesday, August 20, 2019

Remove user permission from all documents using powershell

Environment: SharePoint Server 2016 On Premise

Requirement: To remove specific user permission from all folder, documents from a document library.

Code:

$myweb =  Get-SPWeb "https://sharepoint.domain.com/SM"
$mylist = $myweb.Lists | Where-Object { $_.Title -eq 'Documents' }
#foreach ($item in $mylist.Items ){

$account = $myweb.EnsureUser("c:0+.w|s-1-5-21-790525478-117609710-839522115-1145")
$Folder = $mylist.RootFolder

 foreach ($SubFolder in $mylist.Folders)
        {
                             
            if ($SubFolder.HasUniqueRoleAssignments)
            {
                if($SubFolder.Name -ne "Forms")
                { 
                    write-host "Removing account" $user "from FOLDER " $SubFolder.Name
                    $SubFolder.RoleAssignments.RemoveById($account.ID)

                            $query = New-Object -Type 'Microsoft.SharePoint.SPQuery'
                        $query.Folder = $SubFolder.Folder
                        $folderItems = $mylist.GetItems($query)
                        foreach($item in $folderItems)
                        {
                            if ($item.HasUniqueRoleAssignments)
                            {
                               write-host "Removing account" $user "from FILE" $item.Url
                              $item.RoleAssignments.RemoveById($account.ID)
                          }
                        }
                }
               
            }
        }

"#######################################################"
#}

Sunday, August 4, 2019

File not found when sharing document in SharePoint

Environment: SharePoint Server 2016 On Premise Farm

IssueUsers are getting file not found error when they try to share a document in a document library. The Share URL is similar to below
  • This issue is only for some of the documents in the library.
  • Multiple site collections having this issue
  • Recently we changed the web application site URL
x


https://newhostname.domain.com/http://oldhostname.domain.com/SC1/_layouts/15/DocIdRedir.aspx?ID=VQ47HSUWTZ35-2043059275-2304

Error message: something when wrong file not found

Troubleshoot: 

Fix:
Since the host name was recently renamed, the document properties were still referencing to the old URL, (not sure why). Following PowerShell code is to update affected documents with new host name.





$spSite = Get-SPSite -Identity  https://newhostname.domain.com/LIT

$website = $spSite.OpenWeb()

$list = $website.Lists["Documents"]
$items = $list.GetItems()
foreach($item in $items)
{
    if($item["_dlc_DocIdUrl"].Contains('oldhostname.domain.com') )
    {
        $item.File.CheckOut()
        $item["_dlc_DocIdUrl"] = $item["_dlc_DocIdUrl"].Replace('http://oldhostname.domain.com','https://newhostname.domain.com')
        $item.update()
        $item.File.CheckIn("DocIdUrl updated")
        write-host $item.Name  " updated"
    }
}