Friday, 19 November 2021

Create a Document Set programmatically, using csom

 Finally with the help of code given in this post I was able to achieve the task. The code which I used is:

NetworkCredential creds = new NetworkCredential(userName, password, siteURL);
ClientContext clientContext = new ClientContext(siteURL);
clientContext.Credentials = creds;
List list = clientContext.Web.Lists.GetByTitle("TestLibrary1");
ContentTypeCollection listContentTypes = list.ContentTypes;
clientContext.Load(listContentTypes, types => types.Include(type => type.Id, type => type.Name, type => type.Parent));
var result = clientContext.LoadQuery(listContentTypes.Where(c => c.Name == "Document Set"));
clientContext.ExecuteQuery();
ContentType targetDocumentSetContentType = result.FirstOrDefault();
ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
newItemInfo.UnderlyingObjectType = FileSystemObjectType.Folder;
newItemInfo.LeafName = "Document Set1";
//newItemInfo.FolderUrl = list.RootFolder.ServerRelativeUrl.ToString();
ListItem newListItem = list.AddItem(newItemInfo);
newListItem["ContentTypeId"] = targetDocumentSetContentType.Id.ToString();
newListItem.Update();
list.Update();
clientContext.ExecuteQuery();

This code worked successfully. This is not the only solution. If any one have any other approach then they are welcome to help.

No comments:

Post a Comment