Поделиться через


Метод SPContentType.Update (Boolean)

Обновление определения типа содержимого, хранящихся в базе данных и при необходимости обновляет все типы контента, наследующие от этого типа контента.

Пространство имен:  Microsoft.SharePoint
Сборка:  Microsoft.SharePoint (в Microsoft.SharePoint.dll)

Синтаксис

'Декларация
Public Sub Update ( _
    updateChildren As Boolean _
)
'Применение
Dim instance As SPContentType
Dim updateChildren As Boolean

instance.Update(updateChildren)
public void Update(
    bool updateChildren
)

Параметры

  • updateChildren
    Тип: System.Boolean

    true для принудительной отправки изменений, внесенных в этот тип контента на типы содержимого, наследующие от него; в противном случае — false.

Исключения

Исключение Условие
SPContentTypeSealedException

Sealed свойство данного типа содержимого или потомка этого типа контента имеет значение true.

SPContentTypeReadOnlyException

ReadOnly свойство данного типа содержимого или потомка этого типа контента имеет значение true.

Замечания

После внесения изменений в тип содержимого узла, через объектную модель, код фактически делает эти изменения в памяти представление типа контента сайта. Только при вызове метода UpdateSharePoint Foundation сделать эти изменения постоянными, путем их фиксации в определении типа содержимого, хранящегося в базе данных узла.

Дополнительные сведения содержатся в разделе Updating Content Types.

После изменения типа контента сайта, вы можете распространить или свалить, изменения, внесенные в родительский тип контента для дочернего сайта и списка типы содержимого.

Дополнительные сведения содержатся в разделе Updating Child Content Types.

Примеры

Следующий пример является консольным приложением, которое создает поле ссылку с помощью существующего столбца сайта, добавляет его в тип содержимого узла, а затем обновляет тип содержимого без распространения изменения на дочерние типы содержимого.

Imports System
Imports Microsoft.SharePoint

Module ConsoleApp
    Sub Main()
        Dim site As SPSite = New SPSite("https://localhost")
        Try
            Dim web As SPWeb = site.OpenWeb()
            Try
                Dim ctName As String = "Contact" ' Content type to modify
                Dim fldName As String = "Birthday" ' Site field to link to

                ' Get the content type to modify.
                Dim ct As SPContentType = web.ContentTypes(ctName) ' Null if not found
                If ct Is Nothing Then
                    Console.WriteLine("{0} is not a site content type.", ctName)
                End If

                ' Get the field to link to.
                Dim fld As SPField = Nothing
                Try
                    fld = web.Fields.GetField(fldName) ' Throws exception if not found
                Catch ex As ArgumentException
                    Console.WriteLine("{0} is not a site column.")
                End Try

                ' Add a field link to the content type. 
                If Nothing IsNot fld AndAlso Nothing IsNot ct Then
                    Dim lnk As New SPFieldLink(fld)
                    If Nothing Is ct.FieldLinks(lnk.Id) Then ' Does it exist in collection?
                        'No, so add it.
                        ct.FieldLinks.Add(lnk)

                        ' Update the content type.
                        Try
                            ct.Update(False) ' Do not push down
                            Console.WriteLine("A link to {0} has been added to content type {1}.", fldName, ctName)
                        Catch ex As SPException
                            Console.Write("Update failed. ")
                            Console.WriteLine(ex.Message)
                        End Try
                    Else ' We have a duplicate link
                        Console.WriteLine("Content type {0} already has a link to {1}.", ctName, fldName)
                    End If
                End If
            Finally
                web.Dispose()
            End Try
            Finally
                site.Dispose()
            End Try
        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("https://localhost"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string ctName = "Contact";   // Content type to modify
                    string fldName = "Birthday"; // Site field to link to

                    // Get the content type to modify.
                    SPContentType ct = web.ContentTypes[ctName]; // Null if not found
                    if (ct == null)
                        Console.WriteLine("{0} is not a site content type.", ctName);

                    // Get the field to link to.
                    SPField fld = null;
                    try
                    {
                        fld = web.Fields.GetField(fldName); // Throws exception if not found
                    }
                    catch (ArgumentException ex)
                    {
                        Console.WriteLine("{0} is not a site column.");
                    }

                    // Add a field link to the content type.
                    if (null != fld  && null != ct)
                    {
                        SPFieldLink lnk = new SPFieldLink(fld);
                        if (null == ct.FieldLinks[lnk.Id]) // Does it exist in collection?
                        {
                            //No, so add it.
                            ct.FieldLinks.Add(lnk);

                            // Update the content type
                            try
                            {
                                ct.Update(false); // Do not push down
                                Console.WriteLine("A link to {0} has been added to content type {1}.", fldName, ctName);
                            }
                            catch (SPException ex)
                            {
                                Console.Write("Update failed. ");
                                Console.WriteLine(ex.Message);
                            }
                        }
                        else // We have a duplicate link.
                        {
                            Console.WriteLine("Content type {0} already has a link to {1}.", ctName, fldName);
                        }
                    }
                }
            }
            Console.Write("\nPress ENTER to continue...");
            Console.ReadLine();
        }
    }
}

См. также

Справочные материалы

SPContentType класс

Элементы SPContentType

Перегрузка Update

Пространство имен Microsoft.SharePoint

Другие ресурсы

Updating Content Types

Updating Child Content Types

Introduction to Content Types

Site and List Content Types

Base Content Type Hierarchy