May 22, 2019

SharePoint: Determining if library root level folders have broken permission inheritance using PnP PowerShell

This simple script can be used to determine status of permission inheritance of document library root level folders.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$siteUrl = "https://mytenant.sharepoint.com/sites/somesitecollection"
Connect-PnPOnline -Url $siteUrl -UseWebLogin
$context = Get-PnPContext 

$list = Get-PnPList "SomeLibrary"

$folders = $list.RootFolder.Folders
$context.Load($folders)
$context.ExecuteQuery()

foreach($folder in $folders)
{
  if($folder.ItemCount -gt 0)
  {    
    $f = Get-PnPFolder -Url $folder.ServerRelativeUrl -Includes ListItemAllFields.RoleAssignments, ListItemAllFields.HasUniqueRoleAssignments

    $context.Load($f)
    $context.ExecuteQuery()

    Write-Host $f.ServerRelativeUrl -> $f.ListItemAllFields.HasUniqueRoleAssignments
  }
}

May 20, 2019

SharePoint: Update List Content Type via REST from SPFx web part

Task

I needed to set ReadOnly property of a SPList Content Type from my SPFx web part. I’m using @pnp/sp library, but it doesn’t support modifying existing Content Types.

Solution

Changing the ReadOnly property of an existing list content type is possible using REST, but I had some troubles finding out correct set of HTTP body and header payloads. Working code can be found below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
const spOpts: ISPHttpClientOptions = {
  headers: { 'Accept': 'application/json;odata=verbose', 'X-HTTP-Method': 'MERGE', 'odata-version': '3.0' },
  body: JSON.stringify({
    __metadata: {
      type: 'SP.ContentType'
    },            
    ReadOnly: false
  })
};

let oldCt: ContentType = list.contentTypes.getById('0x01CONTENTTYPEID');

await this.props.context.spHttpClient.post(
  oldCt.toUrlAndQuery(),
  SPHttpClient.configurations.v1,
  spOpts
);


Thoughts

For later reference when investigating similar issues, here’s a collection of error messages depending what Header parameter is missing or invalid.

Without this header propertyYou got error
'Accept': 'application/json;odata=verbose' The property '__metadata' does not exist on type 'SP.ContentType'. Make sure to only use property names that are defined by the type.

Note: This is not required, as odata will be verbose by default unless you have manually set it to, e.g., nometadata. I usually set it to nometadata to improve performance as verbose metadata results of REST calls are not usually required. odata=minimalmetadata is not enough.
'X-HTTP-Method': 'MERGE' The parameter __metadata does not exist in method GetById.
'odata-version': ‘3.0’ Parsing JSON Light feeds or entries in requests without entity set is not supported. Pass in the entity set as a parameter to ODataMessageReader.CreateODataEntryReader or ODataMessageReader.CreateODataFeedReader method.

Note: By default, odata-version will be 4.0, and it doesn’t work here.

May 2, 2019

SharePoint: spHttpClient search query returns HTTP 500 when requesting Created field

Problem

When making SharePoint search query programmatically via REST API and including Created in selectProperties, query returns HTTP 500.

1
2
3
4
this.webPartContext.spHttpClient.get(
  `${this._searchUrl}?querytext='${query}'&selectProperties='Title,Path,Created'`,
  SPHttpClient.configurations.v1
)

When making the query via browser, it works nicely with and without Created in selectProperties.

Solution

Include specific ISPHttpOptions in the get call makes it work as described below.

In any case, it is good idea to include the odata=nometadata option in your REST calls to enable JSON Light whenever applicable to minimize the return payload. Usually you’re not interested in the metadata anyway.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// define _noMetaOpt
private _nometaOpt: ISPHttpClientOptions = {
  headers: { 'Accept': 'application/json;odata=nometadata', 'odata-version': '' }
};

// and call
this.webPartContext.spHttpClient.get(
  `${this._searchUrl}?querytext='${query}'&selectProperties='Title,Path,Created'`,
  SPHttpClient.configurations.v1,
  this._nometaOpt
)