How to add ContentTypes to a Sharepoint Web with C#?

Puneeth, C 81 Reputation points
2022-06-28T05:34:25.283+00:00

I am trying to migrate a site using a C# script and I am currently writing the code to migrate Content Types of the site. Here's what I have so far.

public static void AddContentType(SPWeb web, SPContentType ct)  
        {  
            try  
            {  
                SPContentType newCt = web.ContentTypes[ct.Name];  
                if (newCt == null)  
                {  
                        web.AllowUnsafeUpdates = true;  
                        Console.WriteLine("Does not exist");  
                        newCt = new SPContentType(ct.Parent, web.ContentTypes, ct.Name);  
                        web.ContentTypes.Add(newCt);  
                        web.Update();  
                        web.AllowUnsafeUpdates = false;  
                }  
                 
                else  
                {  
                    Console.WriteLine("Skipping as it already exists");  
                }  
            }  
            catch (Exception e)  
            {  
                Console.WriteLine(e.Message);  
            }  
        }  

However, when it hits newCt = new SPContentType(ct.Parent, web.ContentTypes, ct.Name);, , it throws the following error every time.

Microsoft.SharePoint.SPException: A content type cannot be added outside of its  
scope.  
   at Microsoft.SharePoint.SPContentType.Initialize(SPContentType parentContentT  
ype, SPContentTypeCollection collection, String name)  
   at Microsoft.SharePoint.SPContentType..ctor(SPContentType parentContentType,  
SPContentTypeCollection collection, String name)  
   at MigrateSite.Program.AddContentType(SPWeb web, SPContentType ct) in c:\User  
s\svcmmo13farm_dev\Source\Repos\MigrateSite\MigrateSite\Program.cs:line 230  

I don't know what that means. Can you please help?
Thanks in advance.

Microsoft 365 and Office SharePoint Development
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-06-28T09:37:15.327+00:00

    Hi @Puneeth, C ,
    Could you try to use ContentType class instead of SPContentType. You can refer to the following code

    string siteUrl = "http://MyServer/sites/MySiteCollection";  
    
    ClientContext clientContext = new ClientContext(siteUrl);  
    Web site = clientContext.Web;  
    ContentTypeCollection collContentType = site.ContentTypes;  
    
    // Initialize a new content type.  
    ContentTypeCreationInformation contentInfo = new ContentTypeCreationInformation();  
    contentInfo.Name = "myContentType";  
    contentInfo.Description = "My custom content type";  
    ContentType contentType = collContentType.Add(contentInfo);  
    
    clientContext.Load(collContentType);  
    clientContext.ExecuteQuery();  
    
    foreach (ContentType myType in collContentType){  
        Console.WriteLine("Content Type Name: {0}", myType.Name);  
        }  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.