0

I'm using PnpCore in AzFunctions, and I don't find a way to create a document set in a library.

I've followed the documentation and I created a Document Set Content Type, and then added it to a Document Library, but I don't find any example on how to use the created Document Set Content Type.

I also checked the "Folder" documentation, to see if it possible to change the content type, but it seems that it is not supported.

Here is the documentation I followed:

PnP Core Working with Document Sets

PnP Core Working with Folders

2 Answers 2

0
  • Document sets are part of a site collection feature.
  • Activate below feature before adding it on site.

enter image description here

  • Using below code you can add document set to library.
  • User parent document set id as "0x0120D520"
var docSetId = "0x0120D520"; 

// Load the needed fields that will serve as Shared Field or Welcome Field in the Document Set
var categoriesField = await context.Web.Fields.FirstAsync(y => y.InternalName == "Categories").ConfigureAwait(false);
var managersField = await context.Web.Fields.FirstAsync(y => y.InternalName == "ManagersName").ConfigureAwait(false);

// Load the content types that are allowed to be added to the Document Set
var documentCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Document").ConfigureAwait(false);
var formCt = await context.Web.ContentTypes.FirstAsync(y => y.Name == "Form").ConfigureAwait(false);

// Load the template files that will be used as default content for the Document Set
var file = await context.Web.GetFileByServerRelativeUrlAsync("/sites/hrsite/cv/template.docx").ConfigureAwait(false);

// Prepare the Document Set options
var documentSetOptions = new DocumentSetOptions
{
    AllowedContentTypes = new List<IContentType>
    {
        documentCt,
        formCt
    },
    ShouldPrefixNameToFile = true,
    PropagateWelcomePageChanges = true,
    SharedColumns = new List<IField>
    {
        managersField,
        categoriesField
    },
    WelcomePageColumns = new List<IField>
    {
        managersField,
        categoriesField
    },
    DefaultContents = new List<DocumentSetContentOptions>
    {
        new DocumentSetContentOptions
        {
            FileName = "template.docx",
            FolderName = "Templates",
            File = file,
            ContentTypeId = documentCt.StringId
        }
    }
};

// Create the Document Set
IDocumentSet newDocumentSet = await context.Web.ContentTypes.AddDocumentSetAsync(docSetId, "My New Document Set", "", "Custom Document Sets", documentSetOptions);
  • Please use the this Microsoft docs for your reference.

    Adding Document Set

  • Adding created content type to list use below code

// Add a list content type, start with getting a reference to the list
var list = await context.Web.Lists.GetByTitleAsync("Documents", p => p.ContentTypes);

// Ensure content type are enabled for the list
list.ContentTypesEnabled = true;
await list.UpdateAsync();

// Add the content type
await list.ContentTypes.AddAsync("0x0120D520302EF0D1F1DB4C4EBF58251BCCF5968F", "My New Document Set");
1
  • Hey Rajat, thanks for your help, but that is what I've done so far, my question was about what comes after, once I've added the DocSet Content Type to the library: I want to programmatically create a folder in the list using this content type. Oct 11, 2022 at 6:48
0

I figured one way to do it, probably not the best one:

after following all the step in the docs (activate feature, add content type and assign the content type to a document library). I've created a folder on the doc library, then loaded the items for doc library, look for the item with same unique id, changed it's contentType Id to CT Id of the new DocSet CT.

something like this:

var list = await pnpContext.Web.Lists.GetByTitleAsync("Documents",
                    p => p.RootFolder,                    
                    p => p.Fields);

var rootFolder = list.RootFolder;
// create new folder
var subFolder = await rootFolder.EnsureFolderAsync("myNewFolder");

// load items for the document library
var listWithItems = await list.GetAsync(
    p => p.Items, 
    p => p.Items.QueryProperties(p => p.UniqueId, p => p.All),
    p => p.Fields);

// find item with the same unique id
var subfolderItem = listWithItems.Items.AsRequested().FirstOrDefault(
    x => x.UniqueId == subFolder.UniqueId
);

// if exists, change its CT
if (subfolderItem != null)
{
    subfolderItem["ContentTypeId"] = "0x0120D520001cffaf6662d548f0a092f9de3d3c845a";
    await subfolderItem.UpdateAsync();
}

if you think I can improve this, please let me know!

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.