3

I'm having difficulty creating a document set programmatically from a custom list item edit form. (My goal is to create a document set linked to this list item) Instead of creating an item of content type "Document Set" I am getting a folder, albeit with the type icon that you would expect from a Document Set. When I click on this wannabe-document set-folder I get 'page not found' error. If I edit it and change the content type manually it opens fine.

Here's my code: Please note, I am getting the content type from the list, not the web, which typically gives programmers this error.

private void LaunchInvestigation()
    {
        //Get current ListItem information
        SPListItem callLogItem = SPContext.Current.ListItem;
        string title = Convert.ToString(callLogItem["Title"]);
        DateTime startDate = Convert.ToDateTime(callLogItem["StartDate"]);

        /// Find the library where Document Set will be created.
        SPWeb currentWeb = SPContext.Current.Web;
        SPDocumentLibrary list = (SPDocumentLibrary)currentWeb.Lists["Investigation Log"];

        //Find the Content Type for the Document Set you're creating
        //string docSetName = "Document Set";
        SPContentType docSetName = list.ContentTypes["Document Set"];

        //You can use a hashtable to populate properties of the document set
        var docsetProperties = new Hashtable { { "DocumentDesciption", string.Format("Document Set for {0}", title) }, { "Name", title } };
        DocumentSet documentSet = DocumentSet.Create(
            list.RootFolder,
            title,
            list.ContentTypes.BestMatch(docSetName.Id),
            docsetProperties,
            true);

        callLogItem["InvestigationDocuments"] = string.Format("{0}, {1}", documentSet.WelcomePageUrl, "Documents");
        callLogItem.Update();
    }

1 Answers1

1

I found a workaround that did the trick for me. I added a method to update the content type of the item. When I tried to do this directly following the content type creation it informed me the ContentType property was read-only.

        void SetContentType(SPListItem listItem, SPContentType contentType)
    {
        listItem["ContentTypeId"] = contentType.Id;
        listItem.Update();
    }

Now my Document Set is showing up in the list properly. Does anyone know why I had to add this code? All of the other examples I've seen didn't require this step.