Programatically change the new menu in modern libraries

%3CLINGO-SUB%20id%3D%22lingo-sub-1033286%22%20slang%3D%22en-US%22%3EProgramatically%20change%20the%20new%20menu%20in%20modern%20libraries%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1033286%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20creating%20a%20lot%20of%20document%20libraries%20with%20code%20in%20our%20enterprise%20and%20each%20library%20should%20have%20different%20content%20types.%3C%2FP%3E%3CP%3EUnfortunately%20after%20updating%20the%20content%20types%20with%20CSOM%2C%20the%20default%20Word%2C%20Excel%20and%20PowerPoint%20templates%20disappear%20from%20the%20New-Menu%20in%20a%20modern%20library.%3C%2FP%3E%3CP%3EInstead%20the%20content%20types%20are%20shown.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBecause%20I%20am%20not%20able%20to%20change%20the%20edit%20menu%20in%20hundreds%20of%20libraries%20back%20to%20the%20default%2C%20I%20would%20like%20to%20archive%20this%20within%20my%20software.%3C%2FP%3E%3CP%3EI%20already%20found%20how%20to%20do%20this%3CA%20href%3D%22https%3A%2F%2Fcann0nf0dder.wordpress.com%2F2019%2F03%2F24%2Fprogrammatically-change-the-new-menu-in-sharepoint-online-using-powershell%2F%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3E%20with%20pnp%20powershel%3C%2FA%3El%20here%2C%20but%20would%20like%20to%20do%20it%20from%20withint%20C%23%20(REST%20or%20CSOM)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAny%20ideas%20how%20I%20could%20archive%20that%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1033286%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1033366%22%20slang%3D%22en-US%22%3ERe%3A%20Programatically%20change%20the%20new%20menu%20in%20modern%20libraries%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1033366%22%20slang%3D%22en-US%22%3E%3CP%3EIt%20was%20quite%20easy.%20Figured%20it%20out%20myself%3A%3C%2FP%3E%3CP%3ECSOM%20with%20C%23%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Evar%20list%20%3D%20_sharePointCsomClient._clientContext.Web.Lists.GetByTitle(currentUnit.Name)%3B%3CBR%20%2F%3Evar%20defaultView%20%3D%20list.DefaultView%3B%3CBR%20%2F%3E_sharePointCsomClient._clientContext.Load(defaultView)%3B%3CBR%20%2F%3Evar%20menu%20%3D%20defaultView.NewDocumentTemplates%20%3D%20%22%5B%7B%5C%22templateId%5C%22%3A%5C%22NewFolder%5C%22%2C%5C%22title%5C%22%3A%5C%22Folder%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewDOC%5C%22%2C%5C%22title%5C%22%3A%5C%22Word%20document%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewXSL%5C%22%2C%5C%22title%5C%22%3A%5C%22Excel%20workbook%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewPPT%5C%22%2C%5C%22title%5C%22%3A%5C%22PowerPoint%20presentation%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewONE%5C%22%2C%5C%22title%5C%22%3A%5C%22OneNote%20notebook%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewXSLSurvey%5C%22%2C%5C%22title%5C%22%3A%5C%22Excel%20survey%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewXSLForm%5C%22%2C%5C%22title%5C%22%3A%5C%22Forms%20for%20Excel%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22NewVSDX%5C%22%2C%5C%22title%5C%22%3A%5C%22Visio%20drawing%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%2C%7B%5C%22templateId%5C%22%3A%5C%22Link%5C%22%2C%5C%22title%5C%22%3A%5C%22Link%5C%22%2C%5C%22visible%5C%22%3Atrue%7D%5D%22%3B%3CBR%20%2F%3Elist.DefaultView.Update()%3B%3CBR%20%2F%3Eawait%20_sharePointCsomClient.ExcecuteQueryAsync()%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Contributor

I am creating a lot of document libraries with code in our enterprise and each library should have different content types.

Unfortunately after updating the content types with CSOM, the default Word, Excel and PowerPoint templates disappear from the New-Menu in a modern library.

Instead the content types are shown.

 

Because I am not able to change the edit menu in hundreds of libraries back to the default, I would like to archive this within my software.

I already found how to do this with pnp powershell here, but would like to do it from withint C# (REST or CSOM)

 

Any ideas how I could archive that?

1 Reply

It was quite easy. Figured it out myself:

CSOM with C#:

 

var list = _sharePointCsomClient._clientContext.Web.Lists.GetByTitle(currentUnit.Name);
var defaultView = list.DefaultView;
_sharePointCsomClient._clientContext.Load(defaultView);
var menu = defaultView.NewDocumentTemplates = "[{\"templateId\":\"NewFolder\",\"title\":\"Folder\",\"visible\":true},{\"templateId\":\"NewDOC\",\"title\":\"Word document\",\"visible\":true},{\"templateId\":\"NewXSL\",\"title\":\"Excel workbook\",\"visible\":true},{\"templateId\":\"NewPPT\",\"title\":\"PowerPoint presentation\",\"visible\":true},{\"templateId\":\"NewONE\",\"title\":\"OneNote notebook\",\"visible\":true},{\"templateId\":\"NewXSLSurvey\",\"title\":\"Excel survey\",\"visible\":true},{\"templateId\":\"NewXSLForm\",\"title\":\"Forms for Excel\",\"visible\":true},{\"templateId\":\"NewVSDX\",\"title\":\"Visio drawing\",\"visible\":true},{\"templateId\":\"Link\",\"title\":\"Link\",\"visible\":true}]";
list.DefaultView.Update();
await _sharePointCsomClient.ExcecuteQueryAsync();