Last active
December 20, 2015 20:59
-
-
Save vgrem/6193971 to your computer and use it in GitHub Desktop.
Creates a content type based on the specified schema in SharePoint 2010
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Deployment.ContentType | |
{ | |
/// <summary> | |
/// Content Type | |
/// </summary> | |
public static class SPContentTypeExtensions | |
{ | |
/// <summary> | |
/// Creates a content type based on the specified schema. | |
/// </summary> | |
/// <returns> | |
/// An instance of the new content type. | |
/// </returns> | |
/// <param name="contentTypes">Content Type collection</param> | |
/// <param name="schemaXml">A Collaborative Application Markup Language (CAML) string that contains the schema.</param> | |
public static SPContentType AddContentTypeAsXml(this SPContentTypeCollection contentTypes, string schemaXml) | |
{ | |
SPContentType contentType; | |
using (var xrdr = new XmlTextReader(new StringReader(schemaXml))) | |
{ | |
xrdr.ProhibitDtd = true; | |
contentType = contentTypes.CreateContentType(); | |
LoadXmlInternal(contentType, xrdr); | |
contentTypes.Add(contentType); | |
} | |
return contentType; | |
} | |
/// <summary> | |
/// Create content type | |
/// </summary> | |
/// <param name="contentTypes"></param> | |
/// <returns></returns> | |
private static SPContentType CreateContentType(this SPContentTypeCollection contentTypes) | |
{ | |
var constructor = (typeof(SPContentType)).GetConstructor( | |
BindingFlags.NonPublic | BindingFlags.Instance, | |
null, | |
Type.EmptyTypes, | |
null); | |
var contentType = (SPContentType)constructor.Invoke(new object[0]); | |
contentType.SetWeb(contentTypes.GetWeb()); | |
return contentType; | |
} | |
/// <summary> | |
/// Load schema for content type | |
/// </summary> | |
/// <param name="contentType"></param> | |
/// <param name="xmlReader"></param> | |
private static void LoadXmlInternal(SPContentType contentType, XmlReader xmlReader) | |
{ | |
var loadMethod = contentType.GetType().GetMethod("Load", | |
BindingFlags.NonPublic | BindingFlags.Instance, | |
null, | |
new[] { typeof(XmlReader) }, | |
null); | |
loadMethod.Invoke(contentType, new object[] { xmlReader }); | |
} | |
private static SPWeb GetWeb(this SPContentTypeCollection contentTypes) | |
{ | |
var webProp = typeof(SPContentTypeCollection).GetProperty("Web", BindingFlags.NonPublic | BindingFlags.Instance); | |
return (SPWeb)webProp.GetValue(contentTypes, null); | |
} | |
private static void SetWeb(this SPContentType contentType,SPWeb web) | |
{ | |
var webProp = typeof(SPContentType).GetProperty("Web", BindingFlags.NonPublic | BindingFlags.Instance); | |
webProp.SetValue(contentType, web, null); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment