Forum Discussion
PnP PowerShell - PnP Provisioning package (pnp) with folders
Hi
Is it possible to create a pnp provisioning file with a folder structure for a library by using Convert-PnPFolderToProvisioningTemplate or doesn't the current provisioning enging support pnp packages with folders? I have tried with several options but with no success.
I would like the whole package to contain the folders from the beginning avoiding to have to handle that separately when the template has been applied to the site.
/Niclas
- According to a comment in this post, it seems possible:
http://nickvandenheuvel.eu/2016/03/04/extract-and-apply-templates-with-office-dev-pnp-provisioning-powershell/
How are you referencing the Files?- Deleted
You could create a extention in your template and use this code:
Snippet
private void CreateFolders(ClientContext context, Web web, List list, Project.Provisioning.Models.ListConfigurationListFolder[] folders) { foreach (var folder in folders) { ListItem newItem; try { // Create folder ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation(); itemCreateInfo.UnderlyingObjectType = FileSystemObjectType.Folder; itemCreateInfo.LeafName = folder.Title; newItem = list.AddItem(itemCreateInfo); newItem["Title"] = folder.Title; newItem.Update(); context.ExecuteQueryRetry(); } catch (Exception e) { TraceHelper.WriteInformationToListener("LogFile", $"Couldn't create folder: {folder.Title}."); continue; } if (folder.ObjectSecurity != null) { // Set item security var securitySettings = folder.ObjectSecurity.BreakRoleInheritance; if (securitySettings.RoleAssignment.Length == 0) continue; try { var groups = context.LoadQuery(context.Web.SiteGroups.Include(g => g.LoginName)); var webRoleDefinitions = context.LoadQuery(context.Web.RoleDefinitions); context.ExecuteQueryRetry(); newItem.BreakRoleInheritance(securitySettings.CopyRoleAssignments, securitySettings.ClearSubscopes); foreach (var roleAssignment in securitySettings.RoleAssignment) { Principal principal = groups.FirstOrDefault(g => g.LoginName == roleAssignment.Principal); if (principal == null) { principal = context.Web.EnsureUser(roleAssignment.Principal); } var roleDefinitionBindingCollection = new RoleDefinitionBindingCollection(context); var roleDefinition = webRoleDefinitions.FirstOrDefault(r => r.Name == roleAssignment.RoleDefinition); if (roleDefinition != null) { roleDefinitionBindingCollection.Add(roleDefinition); } newItem.RoleAssignments.Add(principal, roleDefinitionBindingCollection); } context.ExecuteQueryRetry(); } catch (Exception e) { TraceHelper.WriteInformationToListener("LogFile", "Error occured when setting itemlevel security on folder."); continue; } } } }
- Nigel PriceIron Contributor
Hi Paul
What would the provisioning Template entries look like to support the folder hierarchy?
Regards
Nigel
- Niclas CarlssonBrass Contributor
Hi
I have used the sample ProvisioningSchema-2016-05-DirectorySample.xml provided here and it doesn't seem like the Convert-PnPFolderToProvisioningTemplate take the files in consideration when the package is made. I have referenced the files like this in the xml file.
<pnp:Files> <pnp:Directory src="c:\temp\DirectoryBulkLoadSample" Folder="BulkLoaded" Overwrite="true" Recursive="true" IncludedExtensions="*.docx,*.pdf" ExcludedExtensions="*.xml,*.txt" MetadataMappingFile="c:\BulkLoadedMetadata.json" /> </pnp:Files>