4

Which CSOM c# or PowerShell Code I would have to use, to pin a document or link in a modern document library to the top? I want to provision a document library, with one document which should be pinned on top by default.

1
  • Did you ever found an answer to this question, the one below from Derek is a workaround with rebuilding something that would already be available but there may be no API for it yet?
    – Shihan
    Jul 15, 2020 at 11:16

2 Answers 2

3

Create a yes/no column ("IsPinned") on the library and sort your view by that column. You don't need any code.

7
  • 1
    That's not what I meant. I want to provision a document library, with one document which should be pinned on top by default. Nov 21, 2018 at 11:39
  • Agree with @DerekGusoff - this is a good approach and "does the job". The document will always be first on top. If You want, You can do this with code (PowerShell or C#) Nov 21, 2018 at 11:47
  • The question is: what are the code lines to do that (how to PROGRAMMATICALLY...) Nov 21, 2018 at 13:40
  • Fine, create the isPinned property in code. A Google search should help you with that. It's the only way to store the pinned state of a document or list item. Nov 21, 2018 at 14:00
1

A colleague of mine was able to analyse the network traffic that happens, when a users clicks the "Pin to top" button and gave me the REST API call. As I am using PnP PowerShell I created a function that can be called to PIN an item to the top:

function Set-ListItemPinnedToTop
{
  param(
    [Parameter(Mandatory=$true)]
    [string] $ServerRelativeListUrl,
    [Parameter(Mandatory=$true)]
    [Guid] $ViewId,
    [Parameter(Mandatory=$true)]
    [int] $ItemId,
    [Parameter(Mandatory=$true)]
    [string] $FolderPath,
    [Parameter(Mandatory=$false)]
    [int] $AfterItemId=-1
  )

  $encodedDecodedUrl = [System.Web.HttpUtility]::UrlEncode("'$ServerRelativeListUrl'") # a1
  $encodedViewGuid = [System.Web.HttpUtility]::UrlEncode("'$($ViewId.ToString())'") # a2
  $encodedItemId = [System.Web.HttpUtility]::UrlEncode("'$ItemId'") # a3
  $encodedFolderPath = [System.Web.HttpUtility]::UrlEncode("'$FolderPath'") # a4
  $afterItemIdStr = $AfterItemId.ToString() # a5

  $result = Invoke-PnPSPRestMethod -Method Post `
      -Url "/_api/web/GetListUsingPath(DecodedUrl=@a1)/GetView(viewGuid=@a2)/AddToSpotlight(itemId=@a3,folderPath=@a4,afterItemId=@a5)?@a1=$encodedDecodedUrl&@a2=$encodedViewGuid&@a3=$encodedItemId&@a4=$encodedFolderPath&@a5=$afterItemIdStr" `
      -Content "{}" | Out-Null
}

Example URL (directly using the PnP method, to give you an idea, what the values look like):

Invoke-PnPSPRestMethod -Method Post `
-Url "/_api/web/GetListUsingPath(DecodedUrl=@a1)/GetView(viewGuid=@a2)/AddToSpotlight(itemId=@a3,folderPath=@a4,afterItemId=@a5)?@a1=%27%2Fsites%2FSiteXY%2DAPP%2FDossierDocs%27&@a2=%2774d11a1a%2D6502%2D43dd%2D84ab%2Dce9d2ae4489c%27&@a3=%2728%27&@a4=%27%2Fsites%2FSiteXY%2DAPP%2FDossierDocs%2FDossier%2D1%2Fggg%27&@a5=-1" `
-Content "{}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.