Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to change the content type of list items

$
0
0

Im trying to change the content type of all my Root Folders in a document library. I am not even sure if that is possible. When i run the code below i get the message that ListItem.ContentType is Writeprotected...

My Question is, is it possible to change the content type at all? If yes how do i do it with CSOM?

Thanks

ContentType ct = list.ContentTypes.GetById("0x0120D520008AE499F0AEB1C647B9D6F0C9D3B7F9F100B56E2AEF9C715540BE5E87A04F54476E");
                context.ExecuteQuery();

                foreach (ListItem item in items)
                {
                    context.Load(item, i => i.DisplayName);
                    context.Load(item, i => i.ContentType);
                    context.Load(ct, i => i.Id);
                    context.ExecuteQuery();

                    if (item.ContentType.Name == "Folder")
                    {
                        Console.WriteLine("Name: " + item.DisplayName + " ContentType:" + item.ContentType.Name);
                        if (item.ContentType.Sealed = true)
                        {
                            item.ContentType.Sealed = false;
                            item.Update();
                            context.ExecuteQuery();

                        }

                        item.ContentType = ct.Id;
                        item.Update();
                        context.ExecuteQuery();
                    }
                }


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>