This topic has not yet been rated - Rate this topic

SPContentTypeReadOnlyException Class

Represents an exception that occurs when an attempt is made to modify an SPContentType object that is read-only.

System.Object
  System.Exception
    System.ApplicationException
      Microsoft.SharePoint.SPException
        Microsoft.SharePoint.SPContentTypeReadOnlyException

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Available in Sandboxed Solutions: No
[SerializableAttribute]
public class SPContentTypeReadOnlyException : SPException

If an SPContentType object is read-only, the value of its ReadOnly property is true.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SPContentTypeReadOnlyException
Description

The Microsoft.SharePoint.SPContentTypeReadOnlyException class inherits from the Microsoft.SharePoint.SPException class which denotes an exception in SharePoint. This in turn inherits from System.ApplicationException so the error is thrown by a user program rather than the runtime.

The SPContentTypeReadOnlyException contains three public, and one protected constructor. The constructors have very specific purposes either:

1) Type-specific logic
2) Type-specific logic and the default message
3) Type-specific logic for inner exceptions
4) Type-specific serialization logic

Usage Scenario

The most ordinary usage of the SPContentTypeReadOnlyException class is when performing operations that might fail if SPContentType.ReadOnly is set to true. This test is most customary when performing sensitive operations such as updating or deleting content types.

In the following example, I hydrate a SPContentType object by specifying the name "My Content Type". Following, I check the SPContentType.ReadOnly property. If it is true, I throw a new SPContentTypeReadOnlyException with a literal message. If it is not, I can perform an arbitary operation on the content type.

It is important to note that if you wanted to use the inherent SharePoint .NET resource files you could instead use:SPResource.GetString("ContentTypeReadOnly", new object[] { this.Name, this.Scope })

C# Code Example

using (SPSite site = new SPSite("<SharePoint Url>"))
{
using (SPWeb web = site.OpenWeb())
{
SPContentType contentType = web.ContentTypes["My Content Type"];
if (contentType.ReadOnly)
{
throw new SPContentTypeReadOnlyException(string.Format("{0} is read only!", contentType.Name));
}
else
{
contentType.Delete();
}
}
}
Visual Basic .NET Code Example

Using site As New SPSite("<SharePoint Url>")
Using web As SPWeb = site.OpenWeb()

Dim contentType As SPContentType = web.ContentTypes("My Content Type")
If contentType.[ReadOnly] Then
Throw New SPContentTypeReadOnlyException(String.Format("{0} is read only!", contentType.Name))
Else
contentType.Delete()

End If
End Using
End Using

Adam Buenz
SharePoint Foundation MVP - http://www.sharepointsecurity.com