Share via


SPWeb.AvailableContentTypes Property

Gets the collection of all content types that apply to the current scope, including those of the current Web site, as well as any parent Web sites.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

Public ReadOnly Property AvailableContentTypes As SPContentTypeCollection
    Get

Dim instance As SPWeb
Dim value As SPContentTypeCollection

value = instance.AvailableContentTypes
public SPContentTypeCollection AvailableContentTypes { get; }

Property Value

Type: Microsoft.SharePoint.SPContentTypeCollection
An SPContentTypeCollection object that represents the content types.

Remarks

You can apply any content type that is returned in this collection to lists in the current Web site. However, you cannot add content types to the collection that is returned in this property. It is a read-only property.

If you want to make a new content type available, you must add it to the collection that is returned by the ContentTypes property. That collection includes only the content types that exist in the current Web site.

Examples

The following example shows a console application that gets a content type from the collection that is returned by the AvailableContentTypes property, and then applies it to a list in the current Web site.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
   Sub Main()
      Using site As SPSite = New SPSite("http://siteUrl")
         Using web As SPWeb = site.OpenWeb()

            ' Get a content type
            Dim ct As SPContentType = web.AvailableContentTypes(SPBuiltInContentTypeId.Document)

            If ct IsNot Nothing Then ' We have a content type

               Try ' Get a list
                  Dim list As SPList = web.Lists("Test List") ' Throws exception if does not exist

                  ' Make sure we can add content types
                  list.ContentTypesEnabled = True

                  ' Add the content type to the list
                  If Not list.IsContentTypeAllowed(ct) Then
                     Console.WriteLine("The {0} content type is not allowed on the {1} list", _
                                       ct.Name, list.Title)
                  ElseIf list.ContentTypes(ct.Name) IsNot Nothing Then
                     Console.WriteLine("The content type name {0} is already in use on the {1} list", _
                                       ct.Name, list.Title)
                  Else
                     list.ContentTypes.Add(ct)
                  End If

               Catch ex As ArgumentException ' No list
                  Console.WriteLine("The list does not exist.")
               End Try

            Else ' No content type
               Console.WriteLine("The content type is not available in this site.")
            End If

         End Using
      End Using
      Console.Write(vbCrLf + "Press ENTER to continue...")
      Console.ReadLine()
   End Sub
End Module
using System;
using Microsoft.SharePoint;

namespace Test
{
   class ConsoleApp
   {
      static void Main(string[] args)
      {
         using (SPSite site = new SPSite("http://siteUrl"))
         {
            using (SPWeb web = site.OpenWeb())
            {
               // Get a content type
               SPContentType ct = web.AvailableContentTypes[SPBuiltInContentTypeId.Document];

               if (ct != null) // We have a content type
               {
                  try // Get a list
                  {
                     SPList list = web.Lists["Test List"]; // Throws exception if does not exist

                     // Make sure we can add content types
                     list.ContentTypesEnabled = true;

                     // Add the content type to the list
                     if (!list.IsContentTypeAllowed(ct))
                        Console.WriteLine("The {0} content type is not allowed on the {1} list",
                                           ct.Name, list.Title);
                     else if (list.ContentTypes[ct.Name] != null)
                        Console.WriteLine("The content type name {0} is already in use on the {1} list",
                                           ct.Name, list.Title);
                     else
                        list.ContentTypes.Add(ct);
                  }
                  catch (ArgumentException ex) // No list
                  {
                     Console.WriteLine("The list does not exist.");
                  }
               }
               else // No content type
               {
                  Console.WriteLine("The content type is not available in this site.");
               }
            }
         }
         Console.Write("\nPress ENTER to continue...");
         Console.ReadLine();
      }
   }
}

See Also

Reference

SPWeb Class

SPWeb Members

Microsoft.SharePoint Namespace

ContentTypes