Last active
January 24, 2016 18:25
-
-
Save vgrem/6197581 to your computer and use it in GitHub Desktop.
Creates a content type based on the specified schema in SharePoint 2010 (CSOM)
This file contains hidden or 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.Client | |
{ | |
/// <summary> | |
/// Creates a content type based on the specified schema (CSOM) | |
/// </summary> | |
public static class ContentTypeExtensions | |
{ | |
/// <summary> | |
/// Creates a content type based on the specified schema. | |
/// </summary> | |
/// <returns> | |
/// A newly created client object of a content type. | |
/// </returns> | |
/// <param name="clientContext">Client Context </param> | |
/// <param name="schemaXml">A Collaborative Application Markup Language (CAML) string that contains the schema.</param> | |
public static ContentType AddContentTypeAsXml(ClientContext clientContext, string schemaXml) | |
{ | |
ContentType contentType; | |
using (var xmlTextReader = new XmlTextReader(new StringReader(schemaXml))) | |
{ | |
xmlTextReader.ProhibitDtd = true; | |
contentType = Load(clientContext, clientContext.Web.ContentTypes, xmlTextReader); | |
} | |
return contentType; | |
} | |
private static ContentType Load(ClientContext clientContext, ContentTypeCollection cts, XmlReader xrdr) | |
{ | |
var contentTypeCreation = new ContentTypeCreationInformation { }; | |
xrdr.MoveToContent(); | |
if (xrdr.MoveToAttribute("Name")) | |
contentTypeCreation.Name = xrdr.Value; | |
if (xrdr.MoveToAttribute("Group")) | |
contentTypeCreation.Group = xrdr.Value; | |
if (xrdr.MoveToAttribute("Description")) | |
contentTypeCreation.Description = xrdr.Value; | |
contentTypeCreation.ParentContentType = GetParentContentType(clientContext, xrdr.GetAttribute("ID")); | |
var contentType = cts.Add(contentTypeCreation); | |
//Additional properties | |
if (xrdr.MoveToAttribute("Hidden")) | |
contentType.Hidden = xrdr.Value == "TRUE"; | |
bool flagReadOnly = xrdr.MoveToAttribute("ReadOnly"); | |
contentType.ReadOnly = flagReadOnly && xrdr.Value == "TRUE"; | |
xrdr.Read(); | |
while (xrdr.LocalName != "ContentType") | |
{ | |
switch (xrdr.LocalName) | |
{ | |
case "FieldRefs": | |
LoadFieldLinks(clientContext, contentType, xrdr); | |
break; | |
} | |
if (!xrdr.Read()) | |
break; | |
} | |
return contentType; | |
} | |
private static void LoadFieldLinks(ClientContext clientContext, ContentType contentType, XmlReader xrdr) | |
{ | |
xrdr.Read(); | |
while (xrdr.LocalName == "FieldRef" || xrdr.LocalName == "RemoveFieldRef") | |
{ | |
if (xrdr.LocalName == "RemoveFieldRef") | |
{ | |
xrdr.Read(); | |
} | |
else | |
{ | |
LoadFieldLink(clientContext, contentType.FieldLinks, xrdr); | |
contentType.Update(false); | |
clientContext.ExecuteQuery(); | |
} | |
} | |
} | |
internal static FieldLink LoadFieldLink(ClientContext clientContext, FieldLinkCollection fieldLinks, XmlReader xrdr) | |
{ | |
var linkCreationInfo = new FieldLinkCreationInformation(); | |
var hostField = GetField(clientContext, xrdr); | |
linkCreationInfo.Field = hostField; | |
var fieldLink = fieldLinks.Add(linkCreationInfo); | |
xrdr.MoveToContent(); | |
if (xrdr.MoveToAttribute("Required")) | |
{ | |
fieldLink.Required = xrdr.Value == "TRUE" || xrdr.Value == "-1"; | |
} | |
if (xrdr.MoveToAttribute("Hidden")) | |
{ | |
fieldLink.Hidden = xrdr.Value == "TRUE" || xrdr.Value == "-1"; | |
} | |
xrdr.Read(); | |
if (xrdr.LocalName == "Default") | |
{ | |
xrdr.Read(); | |
} | |
return fieldLink; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="clientContext"></param> | |
/// <param name="xrdr"></param> | |
/// <returns></returns> | |
private static Field GetField(ClientContext clientContext, XmlReader xrdr) | |
{ | |
var field = clientContext.Web.AvailableFields.GetById(new Guid(xrdr["ID"])); | |
clientContext.Load(field); | |
clientContext.ExecuteQuery(); | |
return field; | |
} | |
private static ContentType GetParentContentType(ClientContext clientContext, string id) | |
{ | |
var ctId = new SPContentTypeId(id); | |
var parentCtId = ctId.Parent.ToString(); | |
return GetContentTypeById(clientContext, parentCtId); | |
} | |
public static ContentType GetContentTypeById(ClientContext clientContext, string ctId) | |
{ | |
var ct = clientContext.Web.AvailableContentTypes.GetById(ctId); | |
clientContext.Load(ct); | |
clientContext.ExecuteQuery(); | |
return ct; | |
} | |
public static ContentType GetContentTypeByName(ClientContext clientContext, string name) | |
{ | |
var cts = clientContext.Web.ContentTypes; | |
clientContext.Load(cts); | |
clientContext.ExecuteQuery(); | |
foreach (var ct in cts) | |
{ | |
if (ct.Name == name) | |
return ct; | |
} | |
return null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I added the JavaScript edition of this.
https://gist.github.com/patrickbussmann/21104e655d11dedc916d#file-contenttypeextensions-js
Thank you. Helped me a lot.