Indeed, at least i'm experiencing at the moment the same issue (Microsoft.SharePoint.Client.UnknownError
exception) while trying to create a document set via CSOM API in SharePoint Online.
How to reproduce?
While creating a document set via Microsoft.SharePointOnline.CSOM
library:
var list = ctx.Web.Lists.GetByTitle("Documents");
var docSetContentType = ctx.Site.RootWeb.ContentTypes.GetById("0x0120D520");
ctx.Load(docSetContentType);
ctx.Load(list.RootFolder);
ctx.ExecuteQuery();
var result = DocumentSet.Create(ctx, list.RootFolder, docSetName, docSetContentType.Id);
ctx.ExecuteQuery();
the exception of Microsoft.SharePoint.Client.UnknownError
occurs in SharePoint Online.
Note: the error has been detected in the latest and previous versions
of Microsoft.SharePointOnline.CSOM
library. That makes me think it is not
related with CSOM library but rather with SharePoint Online CSOM
service itself.
Workaround
But there is a workaround that allows to create a Document Set (without Microsoft.SharePoint.Client.DocumentSet
namespace involved)
public static void CreateDocumentSet(List list, string docSetName)
{
var ctx = list.Context;
if (!list.IsObjectPropertyInstantiated("RootFolder"))
{
ctx.Load(list.RootFolder);
ctx.ExecuteQuery();
}
var itemInfo = new ListItemCreationInformation();
itemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
itemInfo.LeafName = docSetName;
itemInfo.FolderUrl = list.RootFolder.ServerRelativeUrl;
var item = list.AddItem(itemInfo);
item["ContentTypeId"] = "0x0120D520";
item["HTML_x0020_File_x0020_Type"] = "SharePoint.DocumentSet";
item.Update();
ctx.ExecuteQuery();
}
Has been verified against SharePoint 2010
, 2013
and Online
versions
Usage
var list = ctx.Web.Lists.GetByTitle("Documents");
CreateDocumentSet(list,docSetName);