Freigeben über


Code Sample - SharePoint Online – Shared With Me

#Below Script Sample emulates the Search Query that is sent out while browsing to Shared With Me page.

#The oob page will have a row limit of 30 that is hard coded - but in this script, we can alter it.

#The sample gives you Raw search results. Further development will be required to utilize these search results in a useful manner.

#Note - the results are heavily dependent on Search Freshness.

 

# replace these details (also consider using Get-Credential to enter password securely as script runs)

$username = "administrator@domain.com"

$password = "pwd"

$url = "https://domain-my.sharepoint.com"

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force

 

#Install SharePoint Online Client SDK

[system.reflection.assembly]::LoadWithPartialName('Microsoft.SharePoint.Client')

[system.reflection.assembly]::LoadWithPartialName('Microsoft.SharePoint.Client.Search')

[system.reflection.assembly]::LoadWithPartialName('Microsoft.SharePoint.Client.Runtime')

 

 

# connect/authenticate to SharePoint Online and get ClientContext object..

$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword)

$clientContext.Credentials = $credentials

 

if (!$clientContext.ServerObjectIsNull.Value)

{

Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green

}

 

$web = $clientContext.Web

$clientContext.Load($web)

$clientContext.ExecuteQuery()

 

#enter the UPN of the user for whom you need to get SharedWithMe details.

$username = 'testuser@domain.onmicrosoft.com'

 

#set RowLimit below to the number of results you want to see.

$keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($clientContext)

$keywordQuery.QueryText = 'SharedWithUsersOWSUSER:\' + $userName.ToString() + '\'

$keywordQuery.ClientType = "DocsSharedWithMe"

$keywordQuery.StartRow = 0

$keywordQuery.RowLimit = 100

 

$keywordQuery.ProcessBestBets = $false

$keywordQuery.BypassResultTypes = $true

$keywordQuery.EnableInterleaving = $false

$keywordQuery.EnableQueryRules = $false

$keywordQuery.EnableStemming = $true

 

#modify below property set as per requirement - pull down only the ones that are required and will be used down the pipeline in your code.

$keywordQuery.SelectProperties.Add("Author");

$keywordQuery.SelectProperties.Add("ContentTypeId");

$keywordQuery.SelectProperties.Add("FileExtension");

$keywordQuery.SelectProperties.Add("Filename");

$keywordQuery.SelectProperties.Add("IsMyDocuments");

$keywordQuery.SelectProperties.Add("LastModifiedTime");

$keywordQuery.SelectProperties.Add("ListID");

$keywordQuery.SelectProperties.Add("ListItemID");

$keywordQuery.SelectProperties.Add("EditorOWSUSER");

$keywordQuery.SelectProperties.Add("ServerRedirectedURL");

$keywordQuery.SelectProperties.Add("ServerRedirectedEmbedURL");

$keywordQuery.SelectProperties.Add("ServerRedirectedPreviewURL");

$keywordQuery.SelectProperties.Add("SiteID");

$keywordQuery.SelectProperties.Add("SPSiteURL");

$keywordQuery.SelectProperties.Add("Path");

$keywordQuery.SelectProperties.Add("Title");

$keywordQuery.SelectProperties.Add("ParentLink");

$keywordQuery.SelectProperties.Add("IsContainer");

 

 

$searchExecutor = new-object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($clientContext)

$results = $searchExecutor.ExecuteQuery($keywordQuery)

$clientContext.ExecuteQuery()

 

#This gives you all the documents that are shared with the user -> 'testuser@domain.onmicrosoft.com'

$results.Value

$results.Value[0].ResultRows

Comments