Question

I am writing code for creating DocumentSets dynamically in one of my Document libraries in a SP2013 Community site.

SPContentType docSetCT = web.ContentTypes["Document Set"];

The above line always returns Null and in turn the next line of codes error out.

I have activated the Document Set feature at the site collection level. The Community site is created by the same code, prior to the code line in concern. The Document Library is also created in the same code, prior to the code line in concern.

I am able to get the object SPContentType docSetCT, if i use AvailableContentTypes["Document Set"] instead of ContentTypes["Document Set"]. But, then i receive another error while creating the DocumentSet at following line of code.

docSet = DocumentSet.Create(docLibrary, docTitle, docSetCT.Id, docSetProperties);

I receive following error,

The content type "Folder" at "/" is sealed.

at Microsoft.SharePoint.SPContentType.Update(Boolean updateChildren, Boolean ignoreSealedOrReadOnly, Boolean throwOnSealedOrReadOnly, IList`1 exceptions) at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.ProvisionCTEventReceivers(SPContentType contentType) at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.<>c__DisplayClass7.b__5() at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass5.b__3() at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param) at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode) at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Provision(Boolean bProvisionDefaultContent, SPLongOperationState longOperationState)
at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Create(SPFolder parentFolder, String name, SPContentTypeId ctid, Hashtable properties, Boolean bProvisionDefaultContent, SPUser user, SPLongOperationState longOperationState) at Microsoft.Office.DocumentManagement.DocumentSets.DocumentSet.Create(SPFolder parentFolder, String name, SPContentTypeId ctid, Hashtable properties, Boolean bProvisionDefaultContent) at JiveContentMigration.JiveContentMigrationWebpart.JiveContentMigrationWebpartUserControl.uploadDocumentToLibrary(SPWeb web, String docLibraryName, String folderName, String subFolderName, String jiveDocumentSummary, String jiveDocumentBodyText, DateTime createdOn, String docTitle)

Was it helpful?

Solution

You need to enable content types and add the Document Set content type for this to work.

using (SPSite siteCollection = new SPSite("http://localhost"))
            {
                using (SPWeb site = siteCollection.OpenWeb())
                {

                    // Get a content type.
                    SPContentType ct = site.AvailableContentTypes["Financial Document"];
   if (ct != null)
                    {

                        SPList list = site.Lists["Shared Documents"]; /
                        list.ContentTypesEnabled = true;
                        list.ContentTypes.Add(ct);

                        SPContentType[] orderedContentTypes = new SPContentType[list.ContentTypes.Count];

                        list .EnableFolderCreation = false;
                        list .Update();

                        SPFolder rootFolder = list .RootFolder;
                        SPContentTypeCollection oContentTypes = list.ContentTypes;

                        int CTCount = 0;
                        foreach (SPContentType oContentType in oContentTypes)
                        {                        
                            orderedContentTypes[CTCount] = oContentType;
                            CTCount++;

                         }
                       rootFolder.UniqueContentTypeOrder = orderedContentTypes;
                       rootFolder.Update();
                     }
                 }
               }

http://alancejacob.blogspot.com/2012/09/programically-add-site-content-type-and.html

OTHER TIPS

Try something like this...

https://code.msdn.microsoft.com/windowsapps/SharePoint-2010-Creating-41d2aa7a/sourcecode?fileId=49432&pathId=648538632

protected void createDocSetButton_Click(object sender, EventArgs e) 
    { 
        //Get the Shared Documents document library 
        SPWeb currentWeb = SPContext.Current.Web; 
        SPDocumentLibrary sharedDocsLib = (SPDocumentLibrary)currentWeb.Lists["Shared Documents"]; 
        //You can use a hashtable to populate properties of the document set 
        Hashtable docsetProperties = new Hashtable(); 
        docsetProperties.Add("Name", nameTextbox.Text); 
        docsetProperties.Add("Description", descriptionTextbox.Text); 
        //Create the document set 
        try 
        { 
            DocumentSet newDocSet = DocumentSet.Create(sharedDocsLib.RootFolder, 
                nameTextbox.Text, sharedDocsLib.ContentTypes["Document Set"].Id, 
                docsetProperties, true); 
            resultLabel.Text = "Document set created"; 
        } 
        catch (Exception ex) 
        { 
            resultLabel.Text = "An error occurred: " + ex.Message; 
        } 
    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top