1

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)

1

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

  • I think i was missing to add the Document Set content type to my Library, before adding the Document Set. Thanks for sending me in the right direction.....Will mark this as answer after i get it working. – Amit Tyagi Feb 3 '15 at 21:31
  • Awesome...let me know if it doesn't work. – jpollar Feb 3 '15 at 21:33
0

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; 
        } 
    } 
  • Getting Object reference not set to a instance of an object. error at DocumentSet newDocSet = DocumentSet.Create(sharedDocsLib.RootFolder, nameTextbox.Text, sharedDocsLib.ContentTypes["Document Set"].Id, docsetProperties, true); – Amit Tyagi Feb 3 '15 at 20:10
  • Make sure to replace or set values to: nameTextbox.Text and descriptionTextbox.Text You'll need to replace "Shared Documents" with the name of your library. – jpollar Feb 3 '15 at 20:12
  • Yes, i did that. While debugging the code, i can see the library object is created for my Library and the values for Name and Description are hard-coded. – Amit Tyagi Feb 3 '15 at 20:18
  • You think this line could be the problem? sharedDocsLib.ContentTypes["Document Set"].Id – jpollar Feb 3 '15 at 20:20
  • 1
    I think that's the problem then. Going to post a 2nd answer. Just a sec. – jpollar Feb 3 '15 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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