xiaolizi007
1/16/2017 - 6:32 AM

Content Type

Content Type #sharepoint

 // Create a new site content type.
                  SPContentTypeCollection cts = web.ContentTypes;
                  SPContentType ct = new SPContentType(cts[parentId], // parent
                                                       cts,           // collection
                                                       ctName);       // name

                  // Add the content type to the site collection.
                  cts.Add(ct);
                  Console.WriteLine(
                      "Added {0} content type to site collection.", ct.Name);
      private static string fldName = "LastOrder";
      private static SPFieldType fldType = SPFieldType.DateTime;
                  
      // Create a site field to link to.
      SPFieldCollection fields = web.Fields;
      fldName = fields.Add(fldName, fldType, false);
      Console.WriteLine("Created {0} site column.", fldName);

      // Link the content type to the field.
      SPField field = fields.GetField(fldName);
      SPFieldLink fieldLink = new SPFieldLink(field);
      ct.FieldLinks.Add(fieldLink);
      Console.WriteLine(
                "Linked {0} content type to {1} column.",
                 ct.Name, field.InternalName);

      // Commit changes to the database.
      ct.Update();